C# 读取大文件 (可以读取3GB大小的txt文件)

源代码样例:

1,读取一般文件的代码

[csharp]  view plain  copy
 
  1. public static string ReaderFile(string path)  
  2.         {  
  3.             string fileData = string.Empty;  
  4.             try  
  5.             {   ///读取文件的内容      
  6.                 StreamReader reader = new StreamReader(path, Encoding.Default);  
  7.                 fileData = reader.ReadToEnd();  
  8.                 reader.Close();  
  9.             }  
  10.             catch (Exception ex)  
  11.             {  
  12.                 // throw new Exception(ex.Message,ex);    
  13.             }  ///抛出异常      
  14.             return fileData;  
  15.         }  

2, 读取 大文件(大到约4个GB的文本文件)

[csharp]  view plain  copy
 
  1. private bool ReadBigFile()  
  2.         {  
  3.             string sTmpFile=@"c:\tmpTest.txt";  
  4.             if (File.Exists(sTmpFile))  
  5.             {  
  6.                 File.Delete(sTmpFile);  
  7.             }  
  8.   
  9.             if (!System.IO.File.Exists(sTmpFile))  
  10.             {  
  11.                 FileStream fs;  
  12.                 fs = File.Create(sTmpFile);  
  13.                 fs.Close();  
  14.             }  
  15.   
  16.             if (!File.Exists(txtFileName.Text.Trim()))  
  17.             {  
  18.                 lblResult.Text = "File not exist!";  
  19.                 txtFileName.Focus();  
  20.                 return false;  
  21.             }  
  22.   
  23.             FileStream streamInput = System.IO.File.OpenRead(@txtFileName.Text.Trim());  
  24.             FileStream streamOutput = System.IO.File.OpenWrite(sTmpFile);  
  25.   
  26.             int iRowCount = 10;  
  27.             int.TryParse(txtRowCount.Text.Trim(), out iRowCount);  
  28.   
  29.             try  
  30.             {  
  31.                 for (int i = 1; i <= iRowCount; )  
  32.                 {  
  33.                     int result = streamInput.ReadByte();  
  34.                     if (result == 13)  
  35.                     {  
  36.                         i++;  
  37.                     }  
  38.                     if (result == -1)  
  39.                     {  
  40.                         break;  
  41.                     }  
  42.                     streamOutput.WriteByte((byte)result);  
  43.                 }  
  44.             }  
  45.             finally  
  46.             {  
  47.                 streamInput.Dispose();  
  48.                 streamOutput.Dispose();  
  49.             }  
  50.   
  51.             string sContent = ReaderFile(sTmpFile);  
  52.             CopyToClipboard(sContent);  
  53.   
  54.             return true;  
  55.         }  
  56.   
  57.         public static string ReaderFile(string path)  
  58.         {  
  59.             string fileData = string.Empty;  
  60.             try  
  61.             {   ///读取文件的内容      
  62.                 StreamReader reader = new StreamReader(path, Encoding.Default);  
  63.                 fileData = reader.ReadToEnd();  
  64.                 reader.Close();  
  65.             }  
  66.             catch (Exception ex)  
  67.             {  
  68.                 // throw new Exception(ex.Message,ex);    
  69.             }  ///抛出异常      
  70.             return fileData;  
  71.         }  
  72.   
  73.         private void CopyToClipboard(string sSource)  
  74.         {  
  75.             Clipboard.Clear();  
  76.             if (!string.IsNullOrEmpty(sSource))  
  77.             {  
  78.                 Clipboard.SetText(sSource);  
  79.             }  
  80.         }  

你可能感兴趣的:(c#)