Split File in Two part

solutionsdxb

Member
Joined
Mar 18, 2009
Messages
8
Programming Experience
1-3
Hi,

i want to split a text file in two part , as the file size is approx 30 GB , how can i achieve this is vb.net , i am having a ram of about 4 GB.

I got one solutions of split the file but its split into various text file i just want it to split in two part the first part should contain first 9 lines and the second part contains the rest of the lines.

kindly suggest any modification can be done with the current code to achieve the same.

VB.NET:
Sub TextSplitter()

Dim fsIN, fsOut As IO.FileStream
Dim sr As IO.StreamReader
Dim sw As IO.StreamWriter
Dim OutCount As Integer

fsIN = New IO.FileStream( _
"infile.txt", IO.FileMode.Open, IO.FileAccess.Read _
)

sr = New IO.StreamReader(fsIN, System.Text.Encoding.Default)

Do
Dim Line As String
Dim LineCount As Integer

Line = sr.ReadLine()
If Line Is Nothing Then Exit Do

If fsOut Is Nothing Then
OutCount += 1

fsOut = New IO.FileStream( _
"outfile" & OutCount & ".txt", _
IO.FileMode.CreateNew, IO.FileAccess.Write _
)

sw = New IO.StreamWriter(fsOut, System.Text.Encoding.Default)
LineCount = 0
End If

sw.WriteLine(Line)
LineCount += 1

If LineCount = 20000 Then
sw.Close()
fsOut = Nothing
End If
Loop

If fsOut IsNot Nothing Then
sw.Close()
End If

fsIN.Close()

End Sub
 
Well I quickly wrote some code that will get a file, copy the first half of data into file1 and the second half into file2.

I did it like this :-

VB.NET:
Dim File1 As New System.IO.FileStream("C:\file1.txt", IO.FileMode.Create)
        Dim File2 As New System.IO.FileStream("C:\file2.txt", IO.FileMode.Create)

        Dim FSsource As New System.IO.FileStream("C:\source.txt", IO.FileMode.Open)
        Dim TotalBytes As Integer = FSsource.Length
        ' total bytes is the amount of bytes in the source file
        ' i'm simply going to copy the first half to a new file (file 1)

        Do While FSsource.Position < TotalBytes / 2

            'copy the byte over
            File1.WriteByte(FSsource.ReadByte)

        Loop

        ' now we can do the second part?

        FSsource.Position = TotalBytes / 2

        Do While FSsource.Position < TotalBytes

            'copy the byte over
            File2.WriteByte(FSsource.ReadByte)

        Loop



        FSsource.Close()
        File1.Close()
        File2.Close()


I'm not sure what other ways this can be done.
I basically copied over one byte at a time, and swapped to a new output half way through. It's probably not that quick though.

Finding a way to do it in large chunks would probably be much better!
 
Last edited:
Back
Top