教會斯拉夫文翻譯翻譯社

01.public void Write(string path)
02. {
03. FileStream fs = new FileStream(path, FileMode.Create);
04. StreamWriter sw = new StreamWriter(fs);
05. //最先寫入
06. sw.Write("Hello World!!!!");
07. //清空緩衝區
08. sw.Flush();
09. //封閉流
10. sw.Close();
11. fs.Close();
12. }



(2).使用StreamReader讀取檔,然後一行一行的輸出。
2.檔的讀取

 

System.IO;

 

(2).使用FileStream類創建檔,利用StreamWriter類,將資料寫入到檔。

 

(1).利用FileStream類進行檔的讀取,並將它轉換成char陣列,然後輸出翻譯

 

以上就完成了,txt文字文件的資料讀取與寫入翻譯
1.添加命名空間

 

System.Text;
01.public void Read(string path)
02. {
03. StreamReader sr = new StreamReader(path,Encoding.Default);
04. String line;
05. while ((line = sr.ReadLine()) != null)
06. {
07. Console.WriteLine(line.ToString());
08. }
09. }



3.檔的寫入
(1).利用FileStream類建立檔,然後將資料寫入到檔裡。

 

01.public void Write()
02. {
03. FileStream fs = new FileStream("E:\\ak.txt"翻譯社 FileMode.Create);
04. //取得位元組陣列
05. byte[] data = System.Text.Encoding.Default.GetBytes("Hello World!");
06. //開始寫入
07. fs.Write(data, 0, data.Length);
08. //清空緩衝區、封閉流
09. fs.Flush();
10. fs.Close();
11. }



 

 

01.byte[] byData = new byte[100];
02. char[] charData = new char[1000];
03. public void Read()
04. {
05. try
06. {
07. FileStream file = new FileStream("E:\\test.txt"翻譯社 FileMode.Open);
08. file.Seek(0翻譯社 SeekOrigin.Begin);
09. file.Read(byData, 0, 100); //byData傳進來的位元組陣列,用以接管FileStream物件中的資料翻譯社第2個參數是位元組陣列中起頭寫入資料的位置,它平日是0,暗示從陣列的開端檔中向陣列寫資料,最後一個參數劃定從檔讀幾許字元.
10. Decoder d = Encoding.Default.GetDecoder();
11. d.GetChars(byData, 0, byData.Length翻譯社 charData, 0);
12. Console.WriteLine(charData);
13. file.Close();
14. }
15. catch (IOException e)
16. {
17. Console.WriteLine(e.ToString());
18. }
19. }





以下文章來自: http://fecbob.pixnet.net/blog/post/41199279-c%23%e8%ae%80%e5%af%abtxt%e6%aa%94%e7%9a%84%e5%85%a9%e7%有關翻譯的問題歡迎諮詢天成翻譯公司

arrow
arrow
    文章標籤
    翻譯社
    全站熱搜