I want to remove data that is in the middle of the file, because the file is large, I would like to avoid having to re-write the entire file. To move the data i am trying to: Read from end of data(byte position) to end of file --> to the beginning of data (byte position), then truncate file to file size - data size.
I have this code, but i cant get it to read and write to the same file at the same time...
' Read from end of data in file to byte position of start of data in file
Using inFile As New System.IO.FileStream(NAME, IO.FileMode.Open, IO.FileAccess.Read)
Using outFile As New System.IO.FileStream(NAME, IO.FileMode.Open, IO.FileAccess.Write)
inFile.Seek((START_POS + DATA_SIZE), IO.SeekOrigin.Begin)
outFile.Seek(START_POS, IO.SeekOrigin.Begin)
Do
If FILESIZE - CURRENT_TOTAL_READ < BUFFER_SIZE Then
ReDim BUFFER((FILESIZE - 1) - CURRENT_TOTAL_READ)
End If
BYTES_READ = inFile.Read(BUFFER, 0, BUFFER.Length)
If BYTES_READ > 0 Then
outFile.Write(BUFFER, 0, BYTES_READ)
CURRENT_TOTAL_READ += BYTES_READ
End If
Loop While BYTES_READ > 0 AndAlso CURRENT_TOTAL_READ < (DATA_SIZE- 1)
End Using
End Using
TOTAL_PROCESSED_DATA += CURRENT_TOTAL_READ
' truncate file to file size - data size
TRUNCATE(NAME, (FILESIZE - DATA_SIZE))