C 中File類的簡單實用

2021-08-31 06:46:37 字數 1072 閱讀 6832

在c#中,操作檔案時,有streamreader   ,   streamwriter  ,  以及filestream等類,在操作小檔案時,還是file類提供的方法更為簡便快捷。

就幾個簡單的file類方法的使用做小案例(注:file類是乙個靜態類,是通過類名直接呼叫的方法名

//開啟乙個檔案,使用執行的編碼讀取檔案的所有的行   ,encoding.default 保證讀取中文時不亂嗎

string strs  =  file.readalllines(@"f:\test.txt", encoding.default);

string  strs1  =  file.readalltext(@"f:\test.txt", encoding.default);

/ / readallbytes  讀取檔案並返回乙個位元組陣列

byte   buffer =   file.readallbytes(@"f:\test.txt");

string   str   =   encoding.utf8.getstring(buffer);

console.writeline(str);

//上面的是讀,下面的是寫

file.writealllines(@"f:\new.txt", new string);//會覆蓋原始檔內容

file.writealltext(@"f:\new.txt", "today");//會覆蓋原始檔內容

string str = "今天天氣好晴朗,處處好風光";

byte bte =  encoding.default.getbytes(str);

file.writeallbytes(@"f:\new.txt", bte);//也會覆蓋原始檔內容

C 中file類的應用

現在就其中幾個常用的進行介紹 create 一般使用此過載方法,file.create string string是乙個路徑名,表示檔案的完整路徑,返回值是乙個filestream例項 copy 將現有檔案複製到新檔案,有兩個過載方法,file.copy string,string 第乙個引數是原始...

File 類的簡單使用

分隔符 test public void test01 檔案的相關屬性 test public void test02 建立檔案 test public void test03 判斷該檔案是否存在 boolean flag false if file.exists catch ioexception...

File類簡單使用

file string pathname 根據乙個路徑得到file物件 file string parent,string child 根據乙個目錄和乙個子檔案 目錄得到file物件 file file parent,string child 根據乙個父file物件和乙個子檔案 目錄得到file物件...