Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Write into a text file

Write into a text file

Scheduled Pinned Locked Moved Visual Basic
question
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Daminda
    wrote on last edited by
    #1

    I want to Write a line into a existing Text file Ex Line1 Line2 Line3 I want to replace Line2 With NewLine2 How can I seek a particular Line (move to a given line & Replace it) ____________________________________________________________________ This is append the file but i want to edit the file myProp = Value.Split(",") Dim sr As StreamWriter = File.AppendText(FILE_NAME) sr.WriteLine(myProp(0)) sr.WriteLine(myProp(1)) sr.WriteLine(myProp(2)) sr.WriteLine(myProp(3)) sr.Close() _____________________________________________________________________ :((

    D N 2 Replies Last reply
    0
    • D Daminda

      I want to Write a line into a existing Text file Ex Line1 Line2 Line3 I want to replace Line2 With NewLine2 How can I seek a particular Line (move to a given line & Replace it) ____________________________________________________________________ This is append the file but i want to edit the file myProp = Value.Split(",") Dim sr As StreamWriter = File.AppendText(FILE_NAME) sr.WriteLine(myProp(0)) sr.WriteLine(myProp(1)) sr.WriteLine(myProp(2)) sr.WriteLine(myProp(3)) sr.Close() _____________________________________________________________________ :((

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You can't replace just the one line without rewriting the entire text file. The reason for this is text files have records of varying lengths.

      Imports System.IO
      Imports System.Text
      

      Dim srcFile As New StreamReader("SourceFile.txt")
      Dim newFile As New StreamWriter("NewFile.txt")
      Dim inText As String
      Dim intLineCount As Integer = 1
       
      inText = srcFile.ReadLine()
      While Not inText Is Nothing
      If intLineCount <> 2 Then
      newFile.WriteLine(inText)
      Else
      newFile.WriteLine("New Text To Replace Line 2")
      End If
      End While
      srcFile.Close()
      newFile.Close()
       
      File.Delete("SourceFile.txt")
      File.Move("NewFile.txt", "SourceFile.txt")

      RageInTheMachine9532

      1 Reply Last reply
      0
      • D Daminda

        I want to Write a line into a existing Text file Ex Line1 Line2 Line3 I want to replace Line2 With NewLine2 How can I seek a particular Line (move to a given line & Replace it) ____________________________________________________________________ This is append the file but i want to edit the file myProp = Value.Split(",") Dim sr As StreamWriter = File.AppendText(FILE_NAME) sr.WriteLine(myProp(0)) sr.WriteLine(myProp(1)) sr.WriteLine(myProp(2)) sr.WriteLine(myProp(3)) sr.Close() _____________________________________________________________________ :((

        N Offline
        N Offline
        Nadroj
        wrote on last edited by
        #3

        i dont think u can seek to a specific spot in the file to replace just that line, however, someone else should reaffirm this, or otherwise disprove it. here, i took a little time just now and wrote a sub which will replace any line # in a file, with the string you desire: Private Sub TextLineWriter(ByVal fileName As String, ByVal lineNumberToReplace As Integer, ByVal textToWrite As String) Dim inFile As IO.StreamReader = IO.File.OpenText(fileName) Dim tempString(-1) As String Dim i As Byte 'read the file given and save each line into a new array element string Do Until inFile.Peek = -1 ReDim Preserve tempString(tempString.Length) tempString(tempString.Length - 1) = inFile.ReadLine Loop inFile.Close() 'determine if supplied 'numberToReplace' is valid with the current file, 'and correct the problem if so If lineNumberToReplace - 1 > tempString.Length - 1 Then lineNumberToReplace = tempString.Length ElseIf lineNumberToReplace - 1 < 1 Then lineNumberToReplace = 1 End If 'write the 'textToWrite' at the line number given tempString(lineNumberToReplace - 1) = textToWrite 'create same file as given (overwriting it) and rewrite the entire file Dim outFile As IO.StreamWriter = IO.File.CreateText(fileName) For i = 0 To tempString.Length - 1 outFile.WriteLine(tempString(i)) Next outFile.Close() End Sub use it like this: TextLineWriter("C:\myFile.txt", 3, "Here is my newly put text.") no need to use Imports system.io for it, it is already specified in the sub. just copy and paste this exact, as is sub and test it out. please lemme know if this helps. ------------------------ Jordan. III

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups