Insert a String / Text in Large File using VB.NET


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.

The Problem

To insert string into large files at a specific position. The memory footprint should be low.

The Solution

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)
    End Using
End Sub

Sample usage:

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.


5 Responses to “Insert a String / Text in Large File using VB.NET”

  1. hammackd Says:

    This is great. I was just wondering about how you get the text inserted into the file instead of overwritten…your bottom section was a little confusing (“move the file contents toward the right in the loop”).
    Basically, how would I change this code to allow me to insert a string into the file, without overwriting.
    I’m pretty good at, just your syntax here is different from the way I learned it so I’m a little confused.

  2. hammackd Says:

    You know what? This code does insert, not overwrite. Just tried it out…so thanks a lot! You have saved me much time. You might want to update your description though…
    Thanks again.

  3. Crod Says:

    I works beautifully with a few minor adjustments, just what I needed

    thanks a lot

  4. sodah Says:

    Thanx for this code snipet!!!!

  5. Bin. Says:

    Thank you for the code. I need to insert string at the last of given line number what should be the code then?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: