Click here to Skip to main content
11,790,613 members (52,442 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: VB.NET
Hi .. I'm working on a project that reads a text file. I open the file using READBLOCK. I get an error when opening big files. No issues when opening a 10MB file but got an error when opening a 1GB file.

Here's the part of the code that opens/reads the file:
Const MAX_BYTES As Integer = 1048576 * 5 '=10 MB 
Dim bytesRead(MAX_BYTES) As Char
Dim numBytesRead As Integer
Dim currentPos As Integer = 0
Dim strFileName As String
Dim strm As System.IO.Stream
Dim TextLine As String
Dim FileDetail As IO.FileInfo
strm = OpenFileDialogMain.OpenFile()
strFileName = OpenFileDialogMain.FileName.ToString()
Using reader As System.IO.TextReader = System.IO.File.OpenText(strFileName)
  numBytesRead = reader.ReadBlock(bytesRead, currentPos, MAX_BYTES)
  TextLine = reader.ReadLine()
End Using
txtEditor.Text = New String(bytesRead)
txtEditor.Text += TextLine
txtEditor.Text = txtEditor.Text & vbCrLf & Now
Does anyone have any suggestion on how to open a large file?

Posted 19-Jun-12 18:48pm
Edited 19-Jun-12 18:49pm
abb_saleh at 22-Feb-13 15:10pm
very good tanks
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If you want to read it all into memory, a simple File.ReadAllText() will do just fine. If your file is indeed very large, then you can use the StreamReader class, see the below approach. It is sometimes inevitable but should mostly be avoided for style reasons.

im file As New FileInfo("path\to\file")
Using reader As StreamReader = file.OpenText()
    While Not reader.EndOfStream
        Dim nextLine As String = reader.ReadLine()
    End While
End Using

The challenges on large read, is discussed in detail at[^]
sdcruise at 20-Jun-12 0:59am
Thanks for the quick response. I tried the "ReadAllText" and "ReadLine" before and I get the same error when opening a large file.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Got a problem with ProcessLine, it says 'ProcessLine' is not declared. It may be inaccessible due to its protection level?
OneInNineMillion at 17-Jan-13 3:03am
In response to your question about ProcessLine:
ProcessLine seems to be a custom Sub here.
Crappy example to point out the obvious:

Dim TotalChars As Integer = 0
Private Sub ProcessLine(ByVal LineOfText As String)
TotalChars = TotalChars + LineOfText.Count
End Sub

In this case ProcessLine would help you count the total amount of chars in your file.
Hope that was helpful.
Jared Nathan Drake at 21-Nov-14 15:15pm
Hey there, thanks! Will do that :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 345
1 ppolymorphe 299
2 Richard MacCutchan 118
3 Maciej Los 70
4 George Swan 70
0 OriginalGriff 1,154
1 Maciej Los 825
2 KrunalRohit 686
3 ppolymorphe 660
4 CPallini 621

Advertise | Privacy | Mobile
Web03 | 2.8.1509028.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100