If you have been a VB programmer for a while you know that inserting strings in large files is a pain.
The most usual way is to read the whole file into a variable in memory, insert your string in that variable, and then write the whole file back to disk. This works OK for small files. But is a problem with extraordinarily large files, since it would involve large memory overheads.
To insert string into large files at a specific position. The memory footprint should be low.
The method described below will read the files in chunks so that we don’t consume a lot of memory.
Copy this function to your class or a module.
Sub InsertStringInFile(ByVal fileName As String, ByVal insertString As String, ByVal insertPosition As Integer) Using fs As New IO.FileStream(fileName, IO.FileMode.Open) Dim insertBytes() As Byte = (From ch As Char In insertString Select CByte(Asc(ch))).ToArray Dim readBytes() As Byte = (From ch As Char In insertString Select CByte(0)).ToArray Dim recordLength As Integer = insertBytes.Length Dim readBytesLen As Integer While insertPosition < fs.Length fs.Position = insertPosition readBytesLen = fs.Read(readBytes, 0, recordLength) fs.Position = insertPosition fs.Write(insertBytes, 0, recordLength) Array.Copy(readBytes, insertBytes, recordLength) insertPosition += recordLength End While fs.Write(readBytes, 0, readBytesLen) fs.Close() End Using End Sub
InsertStringInFile("c:\Temp\test11.txt", "inserted text goes here", 15)
How This Works
The FileStream.Write method writes into the file at the current position. You can use the Seek method or set the Position property to set the current file position.
The FileStream.Write method overwrites whatever is in the file at the position it is writing to. So if you want to overwrite the current text, then you don’t need to do anything else other than calling this method with appropriate parameters.
But if we want the text to be inserted instead of overwritten, we will have to first create a space in the file to insert the text we want to so that nothing is overwritten. That’s why we need to move the file contents towards the right in the loop. Rest of the code is self-explanatory.