Write into a text file
-
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() _____________________________________________________________________ :((
-
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() _____________________________________________________________________ :((
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
-
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() _____________________________________________________________________ :((
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