C 寫入和讀出文字檔案

2022-03-27 18:25:01 字數 813 閱讀 7132

class

writetextfile

;system.io.file.writealllines(

@"c:\testdir\test.txt

", lines, encoding.utf8);

//如果檔案不存在,則建立;存在則覆蓋

string strtest = "

該例子測試乙個字串寫入文字檔案。";

system.io.file.writealltext(

@"c:\testdir\test1.txt

", strtest, encoding.utf8);

//在將文字寫入檔案前,處理文字行

//streamwriter乙個引數預設覆蓋

//streamwriter第二個引數為false覆蓋現有檔案,為true則把文字追加到檔案末尾

using (system.io.streamwriter file = new system.io.streamwriter(@"

c:\testdir\test2.txt

",true

)) }}

}}

class

readtextfile

//從頭到尾以流的方式讀出文字檔案

//該方法會一行一行讀出文字

using (system.io.streamreader sr = new system.io.streamreader(@"

c:\testdir\test.txt"))

}console.read();

}}

C 寫入和讀出文字檔案

2017.07.24 class writetextfile system.io.file.writealllines c testdir test.txt lines,encoding.utf8 如果檔案不存在,則建立 存在則覆蓋 string strtest 該例子測試乙個字串寫入文字檔案。sy...

C 寫入和讀出文字檔案

class writetextfile system.io.file.writealllines c testdir test.txt lines,encoding.utf8 如果檔案不存在,則建立 存在則覆蓋 string strtest 該例子測試乙個字串寫入文字檔案。system.io.fil...

C 寫入和讀出文字檔案

class writetextfile system.io.file.writealllines c testdir test.txt lines,encoding.utf8 如果檔案不存在,則建立 存在則覆蓋 string strtest 該例子測試乙個字串寫入文字檔案。system.io.fil...