How to insert lines of text into an existing text file?
-
Currently i am using the below coding to create a text file and write something on it..
Dim wfile As New System.IO.StreamWriter("c:\test.txt") wfile.WriteLine("Hello" & vbcrlf & "Are" & vbcrlf & "You") wfile.Close()
The text file content should be like this Hello Are You What if i want to insert "How" into the text file to modify the text file become like this.. Hello How Are You Anyone can provide the coding on how to get the specific line in a text file and insert lines of text into the text file?Any help is greatly appreciated... Thanks.. -
Currently i am using the below coding to create a text file and write something on it..
Dim wfile As New System.IO.StreamWriter("c:\test.txt") wfile.WriteLine("Hello" & vbcrlf & "Are" & vbcrlf & "You") wfile.Close()
The text file content should be like this Hello Are You What if i want to insert "How" into the text file to modify the text file become like this.. Hello How Are You Anyone can provide the coding on how to get the specific line in a text file and insert lines of text into the text file?Any help is greatly appreciated... Thanks..it can be done. i dont know how, tho.. :(( i do know how to append text though.. that is, to add text to the end of a text file. i dont know how to go about inserting text onto the specific line/location into an existing file, sry. however if the appending text method is suitable, i can explain it. ------------------------ Jordan. III
-
it can be done. i dont know how, tho.. :(( i do know how to append text though.. that is, to add text to the end of a text file. i dont know how to go about inserting text onto the specific line/location into an existing file, sry. however if the appending text method is suitable, i can explain it. ------------------------ Jordan. III
-
Currently i am using the below coding to create a text file and write something on it..
Dim wfile As New System.IO.StreamWriter("c:\test.txt") wfile.WriteLine("Hello" & vbcrlf & "Are" & vbcrlf & "You") wfile.Close()
The text file content should be like this Hello Are You What if i want to insert "How" into the text file to modify the text file become like this.. Hello How Are You Anyone can provide the coding on how to get the specific line in a text file and insert lines of text into the text file?Any help is greatly appreciated... Thanks..You can't "insert" text without rewriting the file from the point where you are inserting. There are a couple of ways to do it: 1.) Seek to the point where you want to insert text, read the text from that point to the end of the file and store it. Seek back to the point where you want to insert text and write what you wanted to insert, then write everything you read in and stored previously. Done... 2.) Create a second file (text2.txt). Read everything from your original file (text1.txt), up to the point where you want to insert, and write it out to text2.txt. Write out what you want to insert. Then finished reading text1.txt and write everything out to text2.txt. Then delete text1.txt and rename text2.txt to text1.txt. Done... RageInTheMachine9532
-
thanks for replying me.... can u tell me ur method how to append text as well? thanks for help..
Open the file for Append and start writing.
Dim myFile As New FileStream( "filename.txt", FileMode.Append )
myFile.WriteLine( "Text I want to append..." )
myFile.Close()RageInTheMachine9532