如何在vb.net中读取1.5 gb的文本文件? [英] How do I read a text file which is 1.5 gb in vb.net?

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

问题描述

如何在vb.net中读取1.5 gb的文本文件(当然是文本框中)?

我不得不读取char中的char不是吗?

解决方案

无论你做什么,将1.5GB的文本放入TextBox都需要永远。只需1分钟即可完成10MB。



根本无法解决问题,绝对没有理由这样做。用户想要查看所有数据?没有。



现在,你为什么要把那么多数据塞入TextBox?可能还有其他方法可以做你想做的事。


System.IO.File.ReadAllText



https:// msdn .microsoft.com / zh-CN / library / ms143368(v = vs.110).aspx [ ^ ]


请注意,CLR对象的最大大小限制为2GB,如果你交叉它你得到 OutOfMemoryException

字符串对象消耗大约以下内容:

对象的20个字节+对象内部缓冲区的每个字符2个字节



所以你需要(注意那个我假设您的PI文本文件是ASCII编码的):

 20 + 2 * 1.5GB 



但是现在实际上有一种方法可以在.NET 4.5中创建这么大的字符串 gcAllowVeryLargeObjects 功能已经推出。

但是通过默认限制你只知道它根本不能顺利运行,用户体验会很糟糕。



作为替代方案,你如何对其内容进行分页,换句话说,如何显示其内容的一部分,让我们说10k字符,并允许用户移动10kB和10kB转发。

比如这样的事情:

 '  PI文本文件。 
Dim piFile As 字符串 = C:\PI.txt
' PI文本的一部分字符大小。
Dim piTextPartSize 作为 整数 = 10000
' 部分PI文本。
Dim piTextPart 作为 字符串

' 指示哪个索引器部分阅读。
' 您可以使用'Next'和'增加和减少其值返回'按钮。
Dim piPartIndex 作为 整数 = 0

' ...

' 以下是阅读特定PI文本文件的部分。
使用 piReader = StreamReader(piFile)
Dim piPartCharacters 正如 字符()= 字符(piTextPartSize - 1 ){}

piReader.BaseStream.Position = piPartIndex * piTextPartSize
piReader.Read( piPartCharacters, 0 ,piTextPartSize)

piTextPart = 字符串(piPartCharacters)
结束 使用


How do I read a text file (into a textbox of course) which is 1.5 gb in vb.net?
I would have to read it char by char wouldn't I?

解决方案

No matter what you do, putting 1.5GB worth of text into a TextBox will take forever. It takes about 1 minute just to do 10MB.

There's simply no way around it and definitely no reason to do it. What user is going to want to view all of that data? None.

Now, WHY are you trying to cram that much data into a TextBox? There may be other ways to do what you're trying to do.


System.IO.File.ReadAllText

https://msdn.microsoft.com/en-us/library/ms143368(v=vs.110).aspx[^]


Note that CLR objects have max size limit of 2GB and if you cross it you get OutOfMemoryException.
String objects consume approximately the following:

20 bytes for the object + 2 bytes per character for the object's inner buffer


So you would need (note that I presume your PI text file is ASCII encoded):

20 + 2 * 1.5GB


However nowadays there is actually a way to make such a big string, in .NET 4.5 a gcAllowVeryLargeObjects feature was introduced.
But nevertheless by passing the default limit you just know that it will not run smoothly at all, the users experience will be terrible.

So as an alternative how about you paginate its content, in other words how about you display just a part of its content, let's say 10k chars, and allow user to move 10kB back and 10kB forward.
For example something like this:

' The PI text file.
Dim piFile As String = "C:\PI.txt"
' The character size for a part of PI text.
Dim piTextPartSize As Integer = 10000
' Part of PI text.
Dim piTextPart As String

' The indexer that indicates which part to read.
' You could increment and decrement its value with 'Next' and 'Back' buttons.
Dim piPartIndex As Integer = 0

' ...

' Here is how to read a specific PI text file's part.
Using piReader = New StreamReader(piFile)
	Dim piPartCharacters As Char() = New Char(piTextPartSize - 1) {}

	piReader.BaseStream.Position = piPartIndex * piTextPartSize
	piReader.Read(piPartCharacters, 0, piTextPartSize)

	piTextPart = New String(piPartCharacters)
End Using


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

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