C# 读取大文件 (可以读取3GB大小的txt文件)
2016-06-20 16:30
471 查看
源代码样例:
1,读取一般文件的代码
[csharp] view
plain copy
public static string ReaderFile(string path)
{
string fileData = string.Empty;
try
{ ///读取文件的内容
StreamReader reader = new StreamReader(path, Encoding.Default);
fileData = reader.ReadToEnd();
reader.Close();
}
catch (Exception ex)
{
// throw new Exception(ex.Message,ex);
} ///抛出异常
return fileData;
}
2, 读取 大文件(大到约4个GB的文本文件)
[csharp] view
plain copy
private bool ReadBigFile()
{
string sTmpFile=@"c:\tmpTest.txt";
if (File.Exists(sTmpFile))
{
File.Delete(sTmpFile);
}
if (!System.IO.File.Exists(sTmpFile))
{
FileStream fs;
fs = File.Create(sTmpFile);
fs.Close();
}
if (!File.Exists(txtFileName.Text.Trim()))
{
lblResult.Text = "File not exist!";
txtFileName.Focus();
return false;
}
FileStream streamInput = System.IO.File.OpenRead(@txtFileName.Text.Trim());
FileStream streamOutput = System.IO.File.OpenWrite(sTmpFile);
int iRowCount = 10;
int.TryParse(txtRowCount.Text.Trim(), out iRowCount);
try
{
for (int i = 1; i <= iRowCount; )
{
int result = streamInput.ReadByte();
if (result == 13)
{
i++;
}
if (result == -1)
{
break;
}
streamOutput.WriteByte((byte)result);
}
}
finally
{
streamInput.Dispose();
streamOutput.Dispose();
}
string sContent = ReaderFile(sTmpFile);
CopyToClipboard(sContent);
return true;
}
public static string ReaderFile(string path)
{
string fileData = string.Empty;
try
{ ///读取文件的内容
StreamReader reader = new StreamReader(path, Encoding.Default);
fileData = reader.ReadToEnd();
reader.Close();
}
catch (Exception ex)
{
// throw new Exception(ex.Message,ex);
} ///抛出异常
return fileData;
}
private void CopyToClipboard(string sSource)
{
Clipboard.Clear();
if (!string.IsNullOrEmpty(sSource))
{
Clipboard.SetText(sSource);
}
}
1,读取一般文件的代码
[csharp] view
plain copy
public static string ReaderFile(string path)
{
string fileData = string.Empty;
try
{ ///读取文件的内容
StreamReader reader = new StreamReader(path, Encoding.Default);
fileData = reader.ReadToEnd();
reader.Close();
}
catch (Exception ex)
{
// throw new Exception(ex.Message,ex);
} ///抛出异常
return fileData;
}
2, 读取 大文件(大到约4个GB的文本文件)
[csharp] view
plain copy
private bool ReadBigFile()
{
string sTmpFile=@"c:\tmpTest.txt";
if (File.Exists(sTmpFile))
{
File.Delete(sTmpFile);
}
if (!System.IO.File.Exists(sTmpFile))
{
FileStream fs;
fs = File.Create(sTmpFile);
fs.Close();
}
if (!File.Exists(txtFileName.Text.Trim()))
{
lblResult.Text = "File not exist!";
txtFileName.Focus();
return false;
}
FileStream streamInput = System.IO.File.OpenRead(@txtFileName.Text.Trim());
FileStream streamOutput = System.IO.File.OpenWrite(sTmpFile);
int iRowCount = 10;
int.TryParse(txtRowCount.Text.Trim(), out iRowCount);
try
{
for (int i = 1; i <= iRowCount; )
{
int result = streamInput.ReadByte();
if (result == 13)
{
i++;
}
if (result == -1)
{
break;
}
streamOutput.WriteByte((byte)result);
}
}
finally
{
streamInput.Dispose();
streamOutput.Dispose();
}
string sContent = ReaderFile(sTmpFile);
CopyToClipboard(sContent);
return true;
}
public static string ReaderFile(string path)
{
string fileData = string.Empty;
try
{ ///读取文件的内容
StreamReader reader = new StreamReader(path, Encoding.Default);
fileData = reader.ReadToEnd();
reader.Close();
}
catch (Exception ex)
{
// throw new Exception(ex.Message,ex);
} ///抛出异常
return fileData;
}
private void CopyToClipboard(string sSource)
{
Clipboard.Clear();
if (!string.IsNullOrEmpty(sSource))
{
Clipboard.SetText(sSource);
}
}
相关文章推荐
- c#判断字符串是否为汉字
- 《C#高级编程》读书笔记(一)
- C#在DataGridView当鼠标移到某行时,该行改变颜色
- leetcode 283 Move Zeroes --个人的新思路
- c#一个FTP操作封装类FTPHelper
- C# 文件转字节数组 byte[]
- .NET C# Tostring() format 格式化字符串大全
- 用C#操作office组件库实现word文档的创建及文字 表格 图片的插入
- C# 基础·算法篇
- C#开发和使用中的33个技巧
- c# DataGridViewComboBoxColumn
- c#.net 生成清晰缩略图
- c#中list容器学习
- C# Repeater 嵌套
- 利用Oledb操作Xlsx文件
- 利用Oledb操作XLS文件
- c# ReaderWriterLock类
- C#和XML
- C#软件注册和注册
- [转载]关于Debug和Release的区别 (VS C#)