c#读写大文件(大于20g),是二进制大文件的读写,谢谢各位
The problem with your solution is that you recreate the streams in each iteration. Try this version:
const int MAX_BUFFER = 33554432; //32MB
byte[] buffer = new byte[MAX_BUFFER];
int bytesRead;
StringBuilder currentLine = new StringBuilder();
using (FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.Read))
using (BufferedStream bs = new BufferedStream(fs))
{
string line;
bool stop = false;
var memoryStream = new MemoryStream(buffer);
var stream = new StreamReader(memoryStream);
while ((bytesRead = bs.Read(buffer, 0, MAX_BUFFER)) != 0)
{
memoryStream.Seek(0, SeekOrigin.Begin);
while (!stream.EndOfStream)
{
line = ReadLineWithAccumulation(stream, currentLine);
if (line != null)
{
//process line
}
}
}
}
private string ReadLineWithAccumulation(StreamReader stream, StringBuilder currentLine)
{
while (stream.Read(buffer, 0, 1) > 0)
{
if (charBuffer [0].Equals('\n'))
{
string result = currentLine.ToString();
currentLine.Clear();
if (result.Last() == '\r') //remove if newlines are single character
{
result = result.Substring(0, result.Length - 1);
}
return result;
}
else
{
currentLine.Append(charBuffer [0]);
}
}
return null; //line not complete yet
}
private char[] charBuffer = new char[1];
循环读取,参考:
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
namespace IO目录管理
{
class Program
{
private string _StrSourcePath = @”E:\TestDir\Test\1.txt”; //源文件目录
private string _StrTagrgetPath = @”F:\TestDir\Test\1.txt”; //目标文件目录
public void Test()
{
//路径合法性判断
if(File.Exists(_StrSourcePath))
{
//构造读取文件流对象
using (FileStream fsRead = new FileStream(_StrSourcePath, FileMode.Open)) //打开文件,不能创建新的
{
//构建写文件流对象
using (FileStream fsWrite = new FileStream(_StrTagrgetPath,FileMode.Create)) //没有找到就创建
{
//开辟临时缓存内存
byte[] byteArrayRead = new byte[1024 * 1024]; // 1字节*1024 = 1k 1k*1024 = 1M内存
//通过死缓存去读文本中的内容
while(true)
{
//readCount 这个是保存真正读取到的字节数
int readCount = fsRead.Read(byteArrayRead, 0, byteArrayRead.Length);
//开始写入读取到缓存内存中的数据到目标文本文件中
fsWrite.Write(byteArrayRead, 0, readCount);
//既然是死循环 那么什么时候我们停止读取文本内容 我们知道文本最后一行的大小肯定是小于缓存内存大小的
if(readCount < byteArrayRead.Length)
{
break; //结束循环
}
}
}
}
}
else
{
Console.WriteLine("源路径或者目标路径不存在。");
}
}
static void Main(string[] args)
{
Program p = new Program();
p.Test();
}
}
如果写入呢,用int这种循环写入吗
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!