read line by line
-
Hi, I have a string variable and it has over 300000 chars.... it is like it: text text1 txt2 it has many lines, and so, I have to read one by one. I tryed to use System.IO.StreamReader, but it does not accept over 260 chars... If someone has any ideas it would be very helpfulll Thanks in advance
-
Hi, I have a string variable and it has over 300000 chars.... it is like it: text text1 txt2 it has many lines, and so, I have to read one by one. I tryed to use System.IO.StreamReader, but it does not accept over 260 chars... If someone has any ideas it would be very helpfulll Thanks in advance
Maybe this isn't the best way to handle it, but how about using
.Split()
on the string, supplying the newline character?Dim lines() as String = _
myBigString.Split(System.Environment.NewLine)Dim i as integer
For i = 0 to lines.Length - 1
'--do something with the line
DoSomething(lines(i))
Next -
Maybe this isn't the best way to handle it, but how about using
.Split()
on the string, supplying the newline character?Dim lines() as String = _
myBigString.Split(System.Environment.NewLine)Dim i as integer
For i = 0 to lines.Length - 1
'--do something with the line
DoSomething(lines(i))
Next -
Hi, Yes it should works, but the problem is that I will make a copy of my big string, and it will use more memory. I am thinking about use your method, but them clear my big string... I will try to get a better way... Thanks, it could be usefull
How about using a
StringReader
then?Dim sr as System.IO.StringReader = _
new System.IO.StringReader(myBigString)Dim line as String
'--read the first line from the string
line = sr.ReadLine()
Do While Not (line Is Nothing)
'--do something with the line
DoSomething(line)
'--read the next line from the string
line = sr.ReadLine()
Loop -
How about using a
StringReader
then?Dim sr as System.IO.StringReader = _
new System.IO.StringReader(myBigString)Dim line as String
'--read the first line from the string
line = sr.ReadLine()
Do While Not (line Is Nothing)
'--do something with the line
DoSomething(line)
'--read the next line from the string
line = sr.ReadLine()
Loop -
Hi, Yes it should works, but the problem is that I will make a copy of my big string, and it will use more memory. I am thinking about use your method, but them clear my big string... I will try to get a better way... Thanks, it could be usefull
Hi, Since String are immutable, any changes you make to the String, like removing a line from it, will result in a copy of the string being created that includes the change you made, then the original string is abandoned and, later, cleaned up by the garbage collector. Holding a 300K string in memory doesn't sound like a good use of resources. Why the large string? RageInTheMachine9532
-
Hi, Since String are immutable, any changes you make to the String, like removing a line from it, will result in a copy of the string being created that includes the change you made, then the original string is abandoned and, later, cleaned up by the garbage collector. Holding a 300K string in memory doesn't sound like a good use of resources. Why the large string? RageInTheMachine9532
Hi, I have a .txt file, and it is very big, my application needs to change this file at any time, and to open edit and save it will result in a long time token, so I think it is better to copy it to a variable, make the changes to the variable, and in the end of the program, it could write again. I do not know if there is a way to rewrite the file just where it has changed, and do not write all the file again, them it will be much better. If you could help me... Thanks
-
Hi, I have a .txt file, and it is very big, my application needs to change this file at any time, and to open edit and save it will result in a long time token, so I think it is better to copy it to a variable, make the changes to the variable, and in the end of the program, it could write again. I do not know if there is a way to rewrite the file just where it has changed, and do not write all the file again, them it will be much better. If you could help me... Thanks
What's in this file? Is it a text document? flat database? Worlds Largest Configuration (.INI) file? Maybe there is a better way to do it, but it depends on the what the data is and what your doing to manipulate it. RageInTheMachine9532
-
What's in this file? Is it a text document? flat database? Worlds Largest Configuration (.INI) file? Maybe there is a better way to do it, but it depends on the what the data is and what your doing to manipulate it. RageInTheMachine9532
-
Hi, It is a text document, so i read it, edit it and them it write again, but jsut write wher it has changed, the it wil be faster... I do not know with there is a direct way to edit it, or if I have to put it in a variable anyway... Any ideas...
OK. It looks like that a String() is the best method then. It sounds like the problem you have now is the speed at which the string is read from or written to the file. I've come up with a partial implementation of a text editor with some code that is the fastest text file reader/writer that I can come up with. It reads/writes 500K in less than 0.05 seconds on my machine. Here's the code snippet for reading and writing the file:
Private Sub mnuFileOpen\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileOpen.Click If ofdOpenFile.ShowDialog = DialogResult.OK Then fileName = ofdOpenFile.FileName Dim openFile As New StreamReader(fileName) txtEdit.Text = openFile.ReadToEnd() openFile.Close() End If End Sub Private Sub mnuFileSave\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileSave.Click If Not fileName.Equals(String.Empty) Then Dim saveFileWriter As StreamWriter = File.CreateText(fileName) Dim strReader As New StringReader(txtEdit.Text) saveFileWriter.Write(strReader.ReadToEnd) saveFileWriter.Flush() saveFileWriter.Close() strReader.Close() End If End Sub
If you want the entire sample project, send me an email with an address to reply to. RageInTheMachine9532