beginner text file writing/reading

KevinT

New member
Joined
Oct 21, 2009
Messages
3
Programming Experience
10+
Hello and thank you in advance everyone.
I am new to the forum and to vb.net, I am trying to use filestream and streamwriter / streamreader to have a simple text box where the text entered is written to a text file, and then a button can be clicked to read the file and present the text back to the textbox. It works, but the problem I'm having is that if i change the text to something smaller in length (less text) than what is currently in the file, then press the button to read the file, it still contains some of the old text that was previously in the file.

I can only presume that I am not clearing something out properly, but I may be wrong. Here below is how I have it coded. Can anyone help please?

here is the reader portion

Dim strmReadFile As New FileStream(txtPath.Text, FileMode.Open, FileAccess.Read, FileShare.Read)

Dim readHandle As New StreamReader(strmReadFile)

'read the entire text, and set it to a string
Dim streamFileContents As String = readHandle.ReadToEnd()

'put the files contents out to the textbox
txtNotes.Text = streamFileContents

'close stream and reader
strmReadFile.Close()
readHandle.Close()

below is the writer portion

Dim strmWriteFile As New FileStream(txtPath.Text, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read)

Dim writeHandle As New StreamWriter(strmWriteFile)


writeHandle.WriteLine(txtNotes.Text)

writeHandle.Flush()

'close writer and stream
writeHandle.Close()
strmWriteFile.Close()
 
so you want to take a text file, read it. display it in a text box. then shorten the length by X amount?

so you'd read 10 characters. and then you only want to save 5 characters?
 
text file

yes, the form has a multi line text box, and a read file button, and a save file button, and a textbox with a place for a path.

I type text into the textbox, click the save button.. Lets say it is 10 chars long. Then, I click the read button, the text from the file is displayed in the textbox. Then, I change the text, shortening it to 5 characters. Then click save. Then, when I click the read button, the new text is there along with some of the old text. I just tried doing a file delete before I write, in other words if the file I am trying to write already exists, I delete it before the write, and that seems to work.. although, there must be a better way...
 
well, i dont like stream readers... but im pretty sure i could make this program pretty easily for you. You can delete and then remake the file, but thats too much of a pain. and timing is no good.
Try this instead.

all i had is 2 buttons and a mutli line textbox... Easy as that... :p

Public Class Form1

Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
IO.File.WriteAllText("C:\Example.txt", textbox1.Text)
End Sub

Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
MsgBox(IO.File.ReadAllText("C:\Example.txt"))
End Sub
End Class
 
text file reading writing

thanks much, I should have mentioned that I am required to use a filestream and streamwrite/reader, this is for a class, but it is good to know other methods like what you provided, thank you
 
Okay, this is how i set it up. same as before. textbox1 = multi line text box.
2 buttons, 1 to write, one to read...
This overwrites the old text files text with whats new in the text box. for me i didnt get any of the old text.

Public Class Form1

Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
Dim SaveTheFile As New IO.StreamWriter("C:\test.txt")
SaveTheFile.Write(TextBox1.Text)
SaveTheFile.Close()
End Sub

Private Sub btnread_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnread.Click
Dim ReadTheFile As New IO.StreamReader("C:\test.txt")
MsgBox(ReadTheFile.ReadToEnd)
readthefile.close() //sorry, forgot to add this in here xD

End Sub
End Class
 
You need to .Dispose those objects or use the Using/End Using.
VB.NET:
Using SaveTheFile As New IO.StreamWriter("C:\test.txt")
   SaveTheFile.Write(TextBox1.Text)
End Using
This will dispose the objects for you.
 
For me, it just overwrote the old text no problems at all... didnt get any of the old text in there... but i dont know much about stream writers and readers. i perfer io.file.readalltext and io.file.writealltext

but thats just me... Hope this helped kevint
 
Back
Top