C 學習記錄 六 檔案讀寫相關操作

2022-09-28 04:33:11 字數 3589 閱讀 6063

目錄file類

filestream類

在system.io命名空間中,是乙個靜態類

path.getfilename();

獲得檔名

path.getfilenamewithoutextension();

獲得檔名但不包含副檔名

path.getextension();

獲得檔案的副檔名

path.getdirectoryname();

獲得檔案所在的資料夾的名稱

path.getfullpath();

獲得檔案的全路徑

path.combined();

連線兩個字串作為路徑

file類只能讀取小檔案,因為只能一次性讀寫,大檔案需要用filestream類。

file.create();

建立乙個檔案,如果有這個檔案,則不會建立新的檔案,但檔案修改時間被修改。

file.delete();

刪除乙個檔案

file.copy();

複製乙個檔案

編碼:將字串以怎樣的形式儲存為二進位制

亂碼:產生亂碼的原因:就是你儲存這個檔案所採用的編碼,跟你開啟這個檔案所採用的編碼格式不一樣。

編碼特點

特點asc

128ascii

256gb2312

包含簡體字

big5

包含繁體字

unicode

比較全,解析起來慢

utf-8

針對web的編碼

file.readallbytes()

字元陣列-->字串,encoding.default.getstring(位元組陣列)

file.writeallbyte()

字串-->字元陣列,encoding.default.getbyte(字串),沒有這個檔案的話,會給你建立乙個,有的話會覆蓋掉。

讀檔案方法

說明file.readallbyte()

可以讀任何檔案,按照位元組讀取,但是需要encoding.default.getstring()轉換成字串,或轉換成其他格式

file.readallline()

只能讀取txt檔案,按照行讀取,輸出string

file.readalltext()

只能讀取txt檔案,讀取全部檔案,輸出string

file.writealllines()

按照行寫txt檔案,file.writealllines(path, new string);

file.writealltext()

寫所有txt檔案,file.writealltext(path, str);

絕對路徑:通過給定的這個路徑直接能在我的電腦中找到這個檔案。

相對路徑:檔案相對於應用程式的路徑。

我們在開發中應該去盡量的使用相對路徑。

類別特點

file

一次性讀寫

filestream

按流讀寫,操作位元組的

streamreader/streamwriter

按流讀寫,操作字元的

using system.io;

using system.text;

string path = @"c:\users\徐志強\desktop";

filestream fsread = new filestream(path + @"\testfile.txt", filemode.openorcreate, fileaccess.read);

byte buffer = new byte[1024 * 1024 * 5];

//r儲存的是這次讀出來的實際有效位元組數

int r = fsread.read(buffer,0,buffer.length);

//將位元組陣列中每乙個元素按照指定的編碼格式解碼成字串

string s = encoding.default.getstring(buffer,0,r);

//關閉流

fsread.close();

//釋放流所占用的資源

fsread.dispose();

console.writeline(s);

console.readkey();

using system.io;

using system.text;

string path = @"c:\users\徐志強\desktop";

using (filestream fswrite = new filestream(path+@"\testfile.txt",filemode.openorcreate,fileaccess.write))

console.clear();

using (filestream fsread = new filestream(path+@"\testfile.txt",filemode.openorcreate,fileaccess.read))

console.readkey();

錯誤的做法:

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.threading.tasks;

using system.io;

namespace filestream類

r = fsread.read(bufferread, 0, bufferread.length);}}

}}

}

正確的做法:

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.threading.tasks;

using system.io;

namespace filestream類}}

}}

}

請注意這裡的while用法!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

當乙個檔案比較大需要迴圈讀取的時候,不能迴圈建立檔案流。

檔案流迴圈讀取過程中,會自動記錄當前讀取的byte位置,讀取的offset設定成0即可。

當讀取返回0,也就意味著什麼都沒有讀到,讀取完了。

using system.io;

string strsourcetxt = @"c:\users\徐志強\desktop\testfile.txt";

using (streamwriter sw = new streamwriter(strsourcetxt,true,encoding.default))

using (streamreader sr = new streamreader(strsourcetxt, encoding.default))

}

Python學習(八) 檔案操作 讀 寫

1 f open 歌詞 encoding utf 8 2 data f.read 3print data 4f.close 5 1126 234457 1233558 153454451515 另一種方式,不需自己close 1 with open a.txt w as f 2 f.write jg...

C學習筆記(8) 檔案讀寫

1.c 檔案讀寫 乙個檔案,無論它是文字檔案還是二進位制檔案,都是代表了一系列的位元組。c 語言不僅提供了訪問頂層的函式,也提供了底層 os 呼叫來處理儲存裝置上的檔案。a.開啟檔案 file fopen const char filename,const char mode filemame是檔名...

python學習記錄(三) 檔案操作

操作檔案必須進行的三個步驟 開啟檔案 對檔案進行操作 讀,寫 關閉檔案 python中開啟檔案使用的是open函式,需要傳入檔案開啟的模式,所有檔案開啟模式如下 檔案開啟模式描述r 以唯讀模式開啟檔案,並將檔案指標指向檔案頭 如果檔案不存在會報錯 w以只寫模式開啟檔案,並將檔案指標指向檔案頭 如果檔...