這篇文章主要講解了JAVA.io讀寫文件的方法,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的撫寧網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
一、Java把這些不同來源和目標(biāo)的數(shù)據(jù)都統(tǒng)一抽象為數(shù)據(jù)流。
Java語言的輸入輸出功能是十分強(qiáng)大而靈活的。
在Java類庫中,IO部分的內(nèi)容是很龐大的,因?yàn)樗婕暗念I(lǐng)域很廣泛:標(biāo)準(zhǔn)輸入輸出,文件的操作,網(wǎng)絡(luò)上的數(shù)據(jù)流,字符串流,對(duì)象流,zip文件流。
這里介紹幾種讀寫文件的方式
二、InputStream、OutputStream(字節(jié)流)
//讀取文件(字節(jié)流)
InputStream in = new FileInputStream("d:\\1.txt");
//寫入相應(yīng)的文件
OutputStream out = new FileOutputStream("d:\\2.txt");
//讀取數(shù)據(jù)
//一次性取多少字節(jié)
byte[] bytes = new byte[2048];
//接受讀取的內(nèi)容(n就代表的相關(guān)數(shù)據(jù),只不過是數(shù)字的形式)
int n = -1;
//循環(huán)取出數(shù)據(jù)
while ((n = in.read(bytes,0,bytes.length)) != -1) {
//轉(zhuǎn)換成字符串
String str = new String(bytes,0,n,"GBK"); #這里可以實(shí)現(xiàn)字節(jié)到字符串的轉(zhuǎn)換,比較實(shí)用
System.out.println(str);
//寫入相關(guān)文件
out.write(bytes, 0, n);
}
//關(guān)閉流
in.close();
out.close();三、BufferedInputStream、BufferedOutputStream(緩存字節(jié)流)使用方式和字節(jié)流差不多,但是效率更高(推薦使用)
//讀取文件(緩存字節(jié)流)
BufferedInputStream in = new BufferedInputStream(new FileInputStream("d:\\1.txt"));
//寫入相應(yīng)的文件
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("d:\\2.txt"));
//讀取數(shù)據(jù)
//一次性取多少字節(jié)
byte[] bytes = new byte[2048];
//接受讀取的內(nèi)容(n就代表的相關(guān)數(shù)據(jù),只不過是數(shù)字的形式)
int n = -1;
//循環(huán)取出數(shù)據(jù)
while ((n = in.read(bytes,0,bytes.length)) != -1) {
//轉(zhuǎn)換成字符串
String str = new String(bytes,0,n,"GBK");
System.out.println(str);
//寫入相關(guān)文件
out.write(bytes, 0, n);
}
//清楚緩存
out.flush();
//關(guān)閉流
in.close();
out.close();四、InputStreamReader、OutputStreamWriter(字節(jié)流,這種方式不建議使用,不能直接字節(jié)長度讀寫)。使用范圍用做字符轉(zhuǎn)換
//讀取文件(字節(jié)流)
InputStreamReader in = new InputStreamReader(new FileInputStream("d:\\1.txt"),"GBK");
//寫入相應(yīng)的文件
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("d:\\2.txt"));
//讀取數(shù)據(jù)
//循環(huán)取出數(shù)據(jù)
byte[] bytes = new byte[1024];
int len = -1;
while ((len = in.read()) != -1) {
System.out.println(len);
//寫入相關(guān)文件
out.write(len);
}
//清楚緩存
out.flush();
//關(guān)閉流
in.close();
out.close();五、BufferedReader、BufferedWriter(緩存流,提供readLine方法讀取一行文本)
//讀取文件(字符流)
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("d:\\1.txt"),"GBK"));#這里主要是涉及中文
//BufferedReader in = new BufferedReader(new FileReader("d:\\1.txt")));
//寫入相應(yīng)的文件
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:\\2.txt"),"GBK"));
//BufferedWriter out = new BufferedWriter(new FileWriter("d:\\2.txt"));
//讀取數(shù)據(jù)
//循環(huán)取出數(shù)據(jù)
String str = null;
while ((str = in.readLine()) != null) {
System.out.println(str);
//寫入相關(guān)文件
out.write(str);
out.newLine();
}
//清楚緩存
out.flush();
//關(guān)閉流
in.close();
out.close();六、Reader、PrintWriter(PrintWriter這個(gè)很好用,在寫數(shù)據(jù)的同事可以格式化)
//讀取文件(字節(jié)流)
Reader in = new InputStreamReader(new FileInputStream("d:\\1.txt"),"GBK");
//寫入相應(yīng)的文件
PrintWriter out = new PrintWriter(new FileWriter("d:\\2.txt"));
//讀取數(shù)據(jù)
//循環(huán)取出數(shù)據(jù)
byte[] bytes = new byte[1024];
int len = -1;
while ((len = in.read()) != -1) {
System.out.println(len);
//寫入相關(guān)文件
out.write(len);
}
//清楚緩存
out.flush();
//關(guān)閉流
in.close();
out.close();七、基本的幾種用法就這么多,當(dāng)然每一個(gè)讀寫的使用都是可以分開的。為了更好的來使用io。流里面的讀寫,建議使用BufferedInputStream、BufferedOutputStream
看完上述內(nèi)容,是不是對(duì)JAVA.io讀寫文件的方法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文標(biāo)題:JAVA.io讀寫文件的方法
網(wǎng)站URL:http://chinadenli.net/article38/gspisp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、軟件開發(fā)、網(wǎng)站制作、外貿(mào)建站、手機(jī)網(wǎng)站建設(shè)、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)