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


//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);  
            }  
        }  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM