c#之檔案操作

來源:互聯網
上載者:User

標籤:flush   cat   color   rem   pre   file   turn   direct   開始   

1、c#檔案操作涉及到的命名空間

using System.Text;
using System.IO;

2、路徑輸入

在上一篇中寫到c#格式化輸出,這裡提一下擷取控制台輸入。

其實瞭解一點英語的朋友都知道Write是寫那對著的Read是讀,針對的對象是控制台。

我們常用的Console.WriteLine是寫且帶斷行符號,則Console.ReadLine是從使用者輸入的地方開始讀取直到該行結束。

Console.Write是一個字元一個字元的寫,則Console.Read是從使用者輸入的地方開始一個字元一個字元的讀取。

使用者輸入時“\”不用轉義。

3、擷取檔案控制代碼

利用DirectoryInfo返回目錄控制代碼,雖然目錄是一種特殊的檔案,但是這裡的參數是目錄路徑不是檔案路徑。

後再通過該對象的GetFiles方法返回該目錄下的檔案資訊或控制代碼。

4、讀取檔案內容

使用StreamReader在記憶體中開闢一個輸入資料流,將磁碟檔案讀入到記憶體中。

然後通過該輸入資料流對象的ReadLine方法讀取檔案,然後一行一行的輸出。

注意:最後需要關閉輸入資料流。

5、寫檔案

使用FileStream類建立檔案,使用StreamWriter類,將資料寫入到檔案。

 

  1 using System;  2 using System.Collections.Generic;  3 using System.Text;  4 using System.IO;  5   6 /// <summary>  7 /// [功能描述:  8 /// 1、使用 I/O 類列出某一目錄下的所有檔案名稱,並輸出到標準控制台  9 /// 2、讀文字檔(逐行),並輸出到標準控制台 10 /// 3、建立一個檔案並向檔案中寫入字串 11 /// ]<br></br> 12 /// [創 建 者: XXX]<br></br> 13 /// [建立時間: 2017-08-28]<br></br> 14 /// <修改記錄  15 ///        修改人=‘‘  16 ///        修改時間=‘yyyy-mm-dd‘  17 ///        修改目的=‘‘ 18 ///        修改描述=‘‘ 19 ///  /> 20 /// </summary> 21 namespace ConsoleApplication1 22 { 23     class File 24     { 25         public File() 26         { 27             Console.Write("請輸入目錄路路徑:"); 28             string path = Console.ReadLine(); 29             ReadFileName(path); 30             Console.WriteLine(); 31             Console.Write("請輸入要讀取的檔案(預設為路徑為上面的路徑):"); 32             path += "\\"; 33             string path1 = path + Console.ReadLine(); 34             ReadFileToConsole(path1); 35             Console.WriteLine(); 36             Console.Write("請輸入要寫入的檔案(預設為路徑和內容分別為上面的路徑和內容):"); 37             string path2 = path + Console.ReadLine(); 38             WriteFile(path1, path2); 39         } 40  41         /// <summary> 42         /// 說明:讀取本地檔案名稱 43         /// </summary> 44         /// <param name="path">目錄路徑</param> 45         /// <returns></returns> 46         /// <remarks>2017-08-28</remarks> 47         private void ReadFileName(string path) 48         { 49             DirectoryInfo dir = new DirectoryInfo(path); 50             FileInfo[] inf = dir.GetFiles(); 51             foreach (FileInfo finf in inf) 52             { 53                 Console.WriteLine(finf); 54             } 55         } 56  57         /// <summary> 58         /// 說明:讀取本地檔案內容到控制台 59         /// </summary> 60         /// <param name="path">檔案路徑</param> 61         /// <returns></returns> 62         /// <remarks>2017-08-28</remarks> 63         private void ReadFileToConsole(string path) 64         { 65             StreamReader sr = new StreamReader(path, Encoding.Default); 66             String line = null; 67             while ((line = sr.ReadLine()) != null) 68             { 69                 Console.WriteLine(line.ToString()); 70             } 71  72             //關閉流 73             sr.Close(); 74         } 75  76         /// <summary> 77         /// 說明:讀取本地檔案內容寫到另一檔案中 78         /// </summary> 79         /// <param name="path">檔案路徑</param> 80         /// <returns></returns> 81         /// <remarks>2017-08-28</remarks> 82         private void WriteFile(string path1, string path2) 83         { 84             StreamReader sr = new StreamReader(path1, Encoding.Default); 85             FileStream fs = new FileStream(path2, FileMode.Create); 86             StreamWriter sw = new StreamWriter(fs); 87             String line = null; 88             while ((line = sr.ReadLine()) != null) 89             { 90                 sw.Write(line.ToString()); 91             } 92  93             //清空緩衝區 94             sw.Flush(); 95  96             //關閉流 97             sw.Close(); 98             sr.Close(); 99             fs.Close();100         }101     }102 }

 

c#之檔案操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.