1. 程式人生 > 實用技巧 >二、File 靜態類的基本操作

二、File 靜態類的基本操作

1.ReadAllLines 傳入指定路徑讀取檔案中的所有行

 1         /// <summary>
 2         /// 讀取檔案txt文字中每一行資料,返回字串
 3         /// </summary>
 4         public static void ReadAllLines()
 5         {
 6             //讀取文字檔案每一行 ReadAllLines 按行分割成字串陣列
 7             string[] txtArr = File.ReadAllLines("TextFile1.txt");
 8 
 9
for (int i = 0; i < txtArr.Length; i++) 10 { 11 Console.WriteLine("第{0}行==>" + txtArr[i], i + 1); 12 } 13 14 string txt = File.ReadAllText("TextFile1.txt"); 15 Console.WriteLine("ReadAllText讀取文字檔案==>" + txt); 16 }
View Code

2.ReadAllBytes 傳入指定檔案讀取並返回位元組陣列

 1         /// <summary>
 2         /// 讀取該檔案中的所有直接 返回直接陣列
 3         /// </summary>
 4         public static void ReadAllBytes()
 5         {
 6 
 7             byte[] getBytes = File.ReadAllBytes("AddPage.png");
 8 
 9             Console.ForegroundColor = ConsoleColor.Red;
10 11 Console.WriteLine(string.Join(",",getBytes)); 12 13 Console.ResetColor(); 14 }
View Code

3.WriteAllText 寫入所有文字,檔案已存在,則覆蓋原有內容

 1         /// <summary>
 2         /// 建立並寫入txt檔案,如果檔案已存在,會覆蓋原內容
 3         /// </summary>
 4         public static void WriteAllText()
 5         {
 6 
 7 
 8             StringBuilder stringBuilder = new StringBuilder();
 9 
10             stringBuilder.AppendFormat("千山鳥飛絕,{0}萬徑人蹤滅,{0}孤舟蓑笠翁。{0}獨釣寒江雪。{0}{0}",Environment.NewLine);
11             stringBuilder.AppendFormat("李白乘舟將欲行,{0}忽聞岸上踏歌聲。{0}桃花潭水深千尺,{0}不及汪倫贈我情。{0}{0}", Environment.NewLine);
12             stringBuilder.AppendFormat("千古風流八詠樓,{0}江山留與後人愁。{0}水通南國三千里,{0}氣壓江城十四州。{0}{0}", Environment.NewLine);
13             stringBuilder.AppendFormat("秋來相顧尚飄蓬,{0}未就丹砂愧葛洪。{0}痛飲狂歌空度日,{0}飛揚跋扈為誰雄。{0}{0}", Environment.NewLine);
14             stringBuilder.AppendFormat("千里鶯啼綠映紅,{0}水村山郭酒旗風。{0}南朝四百八十寺,{0}多少樓臺煙雨中。{0}{0}", Environment.NewLine);
15             stringBuilder.AppendFormat("萬里家山一夢中,{0}吳音漸已變兒童。{0}每逢蜀叟談終日,{0}便覺峨嵋翠掃控。{0}君已忘言真有道,{0}我除搜句百無功,{0}明年採藥天台去,{0}更欲提詩滿浙東。{0}{0}", Environment.NewLine);
16             File.WriteAllText("IWillWrite.txt", stringBuilder.ToString());
17 
18             Console.ForegroundColor = ConsoleColor.Magenta;
19             Console.WriteLine(File.ReadAllText("IWillWrite.txt"));
20             Console.ResetColor();
21         }
View Code

4.WriteAllLines 指定字串陣列寫入檔案中,寫完自動關閉檔案

 1         /// <summary>
 2         /// 寫入所有行
 3         /// </summary>
 4         public static void WriteAllLines()
 5         {
 6             string[] myArr = {
 7                 "千山鳥飛絕,","萬徑人蹤滅。","孤舟蓑笠翁,","獨釣寒江雪。\n",
 8                 "千古風流八詠樓,","江山留與後人愁。","水通南國三千里,","氣壓江城十四州。\n",
 9                 "千里黃雲白日曛,","北風吹雁雪紛紛。","莫愁前路無知己,","天下誰人不識君。\n",
10                 "辛苦遭逢起一經,","干戈寥落四周星。","惶恐灘頭說惶恐,","零丁洋裡嘆零丁。\n",
11                 "魚海蕭條萬里霜,","西風一哭斷人腸。","勸君休望零支塞,","木葉山頭是故鄉。",
12                 "此去流人路幾千,","長虹亭外草連天。","不知黑水西風霜,","可有江南問渡船。\n",
13             };
14 
15             File.WriteAllLines("YesIWillWriteAllLines.txt",myArr);
16 
17              string [] readArr= File.ReadAllLines("YesIWillWriteAllLines.txt");
18 
19             for (int i = 0; i < readArr.Length; i++)
20             {
21               
22                 Console.WriteLine(readArr[i]);
23             }
24         }
View Code

5.WriteAllBytes 指定位元組陣列寫入檔案,若已存在,覆蓋原有內容

 1         /// <summary>
 2         /// 以位元組陣列形式寫入檔案
 3         /// </summary>
 4         public static void WriteAllBytes()
 5         {
 6             byte[] getBytes = File.ReadAllBytes("AddPage.png");
 7 
 8             File.WriteAllBytes("WriteAllBytes.png", getBytes);
 9 
10             byte[] newBytes = File.ReadAllBytes("WriteAllBytes.png");
11 
12             foreach (var item in newBytes)
13             {
14                 Console.WriteLine(item);
15             }
16         }
View Code