I am trying to read in a file and store the byte position of each line read so that I can write out to another file the records in any order that I want. I am doing more manipulation of the lines, hence why I need the line position from the original file, but for simplicity sake, I am just including code to display the problem I am encountering. When I read in the file, I am getting the correct file position, but when I write out the lines, every 1024 characters it is re-writing whatever current line it is on. I am assume there is some buffer that is coming into play that I am not familiar with, so any help or advice would be appreciated.
VB.NET:
Dim srReader As StreamReader
Dim LongString As String
Dim fsReader As FileStream
Dim fsWriter As FileStream
Dim srWriter As StreamWriter
Dim LinePointer As Long
Dim IndexArray As New ArrayList
OpenFileDialog1.ShowDialog()
fsReader = New FileStream(OpenFileDialog1.FileName, FileMode.Open)
srReader = New StreamReader(fsReader)
fsWriter = New FileStream(OpenFileDialog1.FileName & ".Wrote", FileMode.Create)
srWriter = New StreamWriter(fsWriter)
'Read in each line and store the byte position for the start of each line
LinePointer = 0
Do
LongString = srReader.ReadLine
IndexArray.Add(LinePointer)
LinePointer += Len(LongString + Environment.NewLine)
Loop Until srReader.Peek = -1
'Seek to the byte position and read in a line from that point
For x As Long = 0 To IndexArray.Count - 1
fsReader.Seek(IndexArray(x), SeekOrigin.Begin)
LongString = srReader.ReadLine
srWriter.WriteLine(LongString)
Next
srReader.Close()
srWriter.Close()
fsReader.Close()
fsWriter.Close()