如何在VB.NET中读取大文本文件 [英] how to read a large text file in VB.NET

查看:638
本文介绍了如何在VB.NET中读取大文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..我正在研究一个读取文本文件的项目。我使用READBLOCK打开文件。打开大文件时出错。打开10MB文件时没有问题,但打开1GB文件时出错。



这是打开/读取文件的代码部分:

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

OpenFileDialogMain.ShowDialog()
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?

Thanks!

推荐答案

如果你想将它全部读入内存,一个简单的File.ReadAllText()就可以了。如果您的文件确实非常大,那么您可以使用StreamReader类,请参阅以下方法。它有时是不可避免的,但出于风格原因应该主要避免。



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()
        ProcessLine(nextLine)
    End While
End Using





大读取的挑战在htt p://kirillosenkov.blogspot.com/2007/11/please-use-filereadalltext-and-like.html [ ^ ]


ProcessLine遇到问题,它说'' ProcessLine''未声明。由于其保护级别,它可能无法访问?
Got a problem with ProcessLine, it says ''ProcessLine'' is not declared. It may be inaccessible due to its protection level?


这篇关于如何在VB.NET中读取大文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆