java 基本的檔案讀寫

2021-06-08 17:03:28 字數 1403 閱讀 2756

讀入

1. bufferedreader in = new bufferedreader(new inputstreamreader(new fileinputstream("d:/cplus/norm/norm/exp"), "utf-8"));

2. bufferedreader br = new bufferedreader(new filereader("file.txt"));

方法2不能設定解碼的編碼集,只能使用平台預設的,有所限制。

寫入1. bufferedwriter out = new bufferedwriter(new outputstreamwriter(new fileoutputstream("t3"), "utf-8"));

2. bufferedwriter bw = new bufferedwriter(new filewriter("file.txt"));

3. printwriter pw = new printwriter("t2","utf-8");

方法1的效率很高,但是輸出的方法只有bw.write,換行符只能用bw.newline()

方法3除了效率有略微的差,相比方法1。優勢在於,簡單,可以pw.printf格式輸出,可以不寫try catch,可以直接println輸出根據平台變化的換行符

方法3還有乙個缺點是如果指定是追加模式,就不能指定編碼

filewriter = new filewriter(file,true);  

printwriter = new printwriter(filewriter);  

printwriter.println(str);  

還是用方法1實現追加模式 bufferedwriter bw = new bufferedwriter(new outputstreamwriter(new fileoutputstream("td", true), "utf-8"));

printwriter out = new printwriter(new outputstreamwriter(

new fileoutputstream("exp.log"), "utf-8"));

string line;

while ((line = in.readline()) != null)

out.println(normalizer.normalizeline(line, langid, punct));

in.close();

out.close();

讀ascii碼檔案,只用

bufferedreader groupfile = new bufferedreader(new filereader(groupin));

printstream fo = new printstream(new fileoutputstream(fileout));

java 檔案讀寫

一.獲得控制台使用者輸入的資訊 獲得控制台使用者輸入的資訊 return throws ioexception public string getinputmessage throws ioexception.五.轉移檔案目錄 轉移檔案目錄不等同於複製檔案,複製檔案是複製後兩個目錄都存在該檔案,而轉...

java 檔案讀寫

response.reset 非常重要 url u new url file filepath response.setcontenttype u.openconnection getcontenttype response.setheader content disposition inline ...

Java檔案讀寫

file file new file text.txt 只是file本身,不能進行讀寫 寫入 printwriter printwriter null try catch filenotfoundexception e finally 讀取 寫入 fileoutputstream fileoutpu...