讀寫檔案操作

2021-08-31 06:05:41 字數 1205 閱讀 1126

一、txt檔案讀寫:

位元組流寫入檔案:file、fileoutputstream、outputstreamwriter、bufferedwirter

字元流寫入檔案:file、filewirter

位元組流讀取檔案:file、fileinputstream、inputstreamreader、bufferedreader

字元流讀取檔案:file、filereader、bufferedreader

1.將資料寫入檔案:

//位元組流寫入檔案

file file = new file("d:\\new_selfcard\\cardfile\\yangli1025-21-01-20100716-110757.txt");

fos = new fileoutputstream(file);//覆蓋原檔案

osw = new outputstreamwriter(fos);

bw = new bufferedwriter(osw);

bw.write(str); //str是指需要寫入的內容

bw.newline();

//字元流寫入檔案

public static void writetxtfile2(string filename,string str)

fw = new filewriter(f);

fw.write(str);

}catch(ioexception e)finallycatch(ioexception e2)}}

2.從檔案中讀取資料:

//讀取字元檔案

public static void readtxtfile(string filename)

}catch(ioexception e)finallycatch(ioexception e2)}}

//讀取位元組流檔案

public static void readtxtfile(string filename)

}catch(ioexception e)finallycatch(ioexception e2)}}

public static void main(string args)

二、得到目錄下檔案數及檔名

**如下:

file f = new file(bxtkpath); //bxtkpath是目錄

if(f.exists())

}

檔案操作 讀寫檔案

對檔案的讀寫操作應該是最重要的檔案操作,system.io命名空間提供了諸多檔案讀寫操作類,對檔案內容進行操作常見有3種方式 文字模式 二進位制模式以及非同步模式。1 文字模式 streamreader 和streamwriter類提供了按文字模式讀寫資料的方法。1.1 streamreader 類...

讀寫檔案操作

匯入領導簡歷資訊 return public string leaderresumeimport throws ioexceptionelse if fr null fr.close if flag else fw.close string sjson outputclient sjson retu...

檔案讀寫操作

函式原型 file fopen const char filename,const char mode 返回值 檔案順利開啟後,指向該流的檔案指標就會被返回。如果檔案開啟失敗則返回null,並把錯誤 存在errno中 引數 mode 說明 r read,已唯讀方式開啟檔案,檔案必須存在 w writ...