C#讀取文本文件和寫文本文件


C#讀取文本文件

 1    // 讀操作
 2         public static void Read()
 3         {
 4             // 讀取文件的源路徑及其讀取流
 5             string strReadFilePath = @"../../data/ReadLog.txt";
 6             StreamReader srReadFile = new StreamReader(strReadFilePath);
 7 
 8             // 讀取流直至文件末尾結束
 9             while (!srReadFile.EndOfStream)
10             {
11                 string strReadLine = srReadFile.ReadLine(); //讀取每行數據
12                 Console.WriteLine(strReadLine); //屏幕打印每行數據
13             }
14 
15             // 關閉讀取流文件
16             srReadFile.Close();
17             Console.ReadKey();
18         }

C# 寫文本文件

 1    // 寫操作
 2         public static void Write()
 3         {
 4             // 統計寫入(讀取的行數)
 5             int WriteRows = 0;
 6 
 7             // 讀取文件的源路徑及其讀取流
 8             string strReadFilePath = @"../../data/ReadLog.txt";
 9             StreamReader srReadFile = new StreamReader(strReadFilePath);
10 
11             // 寫入文件的源路徑及其寫入流
12             string strWriteFilePath = @"../../data/WriteLog.txt";
13             StreamWriter swWriteFile = File.CreateText(strWriteFilePath);
14 
15             // 讀取流直至文件末尾結束,並逐行寫入另一文件內
16             while (!srReadFile.EndOfStream)
17             {
18                 string strReadLine = srReadFile.ReadLine(); //讀取每行數據
19                 ++WriteRows; //統計寫入(讀取)的數據行數
20 
21                 swWriteFile.WriteLine(strReadLine); //寫入讀取的每行數據
22                 Console.WriteLine("正在寫入... " + strReadLine);
23             }
24 
25             // 關閉流文件
26             srReadFile.Close();
27             swWriteFile.Close();
28 
29             Console.WriteLine("共計寫入記錄總數:" + WriteRows);
30             Console.ReadKey();
31         }

完整源代碼

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 using System.IO;   //引用輸入輸出操作的命名空間
 6 
 7 namespace ReadWriteFile
 8 {
 9     class Program
10     {
11 
12         //主函數
13         static void Main(string[] args)
14         {
15             Read();  //讀操作
16             Write(); //寫操作
17 
18         }
19 
20         //讀操作
21         public static void Read()
22         { 
23             //讀取文件的源路徑及其讀取流
24             string strReadFilePath = @"../../data/ReadLog.txt";
25             StreamReader srReadFile = new StreamReader(strReadFilePath);
26 
27             //讀取流至文件末尾結束
28             while(!srReadFile.EndOfStream)
29             {
30                 string strReadLine = srReadFile.ReadLine();   //讀取每行數據
31                 Console.WriteLine(strReadLine);  //屏幕打印每行數據
32             }
33 
34             //關閉讀取流文件
35             srReadFile.Close();
36             Console.ReadKey();
37 
38         }
39 
40         //寫操作
41         public static void Write()
42         { 
43             //統計寫入(讀取的行數)
44             int WriteRows = 0;
45 
46             //讀取文件的源路徑及其讀取流
47             string strReadFilePath = @"../../data/ReadLog.txt";
48             StreamReader srReadFile = new StreamReader(strReadFilePath);
49 
50             //寫入文件的源路徑及其寫入流
51             string strWriteFilePath = @"../../data/WriteLog.txt";
52             StreamWriter swWriteFile = File.CreateText(strWriteFilePath);
53 
54             //讀取流至文件末尾結束,並逐行寫入另一文件內
55             while(!srReadFile.EndOfStream)
56             {
57                 string strReadLine = srReadFile.ReadLine();   //讀取每行數據
58                 ++WriteRows;  //統計寫入(讀取)的數據行數
59 
60                 swWriteFile.WriteLine(strReadLine);   //寫入讀取的每行數據
61                 Console.WriteLine("正在寫入..."+strReadLine);
62             }
63 
64             //關閉流文件
65             srReadFile.Close();
66             swWriteFile.Close();
67 
68             Console.WriteLine("共計寫入記錄總數:"+WriteRows);
69             Console.ReadKey();
70         }
71 
72     }
73 }

 

參考鏈接:https://blog.csdn.net/hongkaihua1987/article/details/80432436


免責聲明!

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



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