【问题标题】:String longer than 2GB字符串长度超过 2GB
【发布时间】:2015-11-21 05:50:32
【问题描述】:

我需要将几个文本文件连接在一起,每个输入文件大约为 3.5GB。这些是在行尾包含 vbCrLf 的简单文件。

我以前用于从网页(如下)获取文本流的一些代码使用 Peek 函数(见下文),我想知道 Peek 是否可以以某种方式用于从文件中读取小于 2GB 的内容 --> 写入连接文件,然后继续每个文件的后半部分?

 Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")
 Dim readStream As New StreamReader(recvStream, encode)
 Dim sPage As String
 sPage = ""
 While (readStream.Peek() > -1)
   sPage += readStream.ReadLine() + vbCrLf
 End While

基本上,我不能使用 Peek、write(concatenate)、Peek、concatenate 等读取高达 1.8GB 的​​内容而不会丢失任何内容。还有什么可以让我用 StreamWriter 写的 3.5GB Stream 的东西吗?

【问题讨论】:

  • 应用程序是 64 位版本的 Windows 上的 64 位应用程序吗?
  • 你有没有看到过载StreamReader.Read Method (Char(), Int32, Int32) 允许你分块阅读?然后,无需将数据存储在字符串中,只需在读取每个块时将其写入目标文件即可。我建议以 32768 的缓冲区大小开始。
  • 感谢@Andrew - StreamReader.Read 工作并解决了问题。
  • @LEP 如果您找到了解决方案,请将其作为答案发布并接受,以便未来的读者可能会发现它有帮助
  • VB.net 和 VBA 是不同的语言。代码和问题面向 VB.net,但问题的标题和标签是 VBA。这有点令人困惑。如果不是 VBA,则应将其删除。

标签: vb.net string out-of-memory


【解决方案1】:

解决方案如下:

 Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")
 Dim readStream As New StreamReader(recvStream, encode)
 Dim str, sPage As String
 sPage = ""
 While (readStream.Peek() > -1)
   sPage += readStream.Read(str, 0, 32768) 
 End While
 'Next, split the string into lines at the carriage returns
 Dim Buff() As String = Split(sPage, vbCrLf)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多