JAVA.io讀寫文件方式匯總
一、Java把這些不同來源和目標(biāo)的數(shù)據(jù)都統(tǒng)一抽象為數(shù)據(jù)流。
Java語言的輸入輸出功能是十分強大而靈活的。
在Java類庫中,IO部分的內(nèi)容是很龐大的,因為它涉及的領(lǐng)域很廣泛:標(biāo)準(zhǔn)輸入輸出,文件的操作,網(wǎng)絡(luò)上的數(shù)據(jù)流,字符串流,對象流,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'); #這里可以實現(xiàn)字節(jié)到字符串的轉(zhuǎn)換,比較實用 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這個很好用,在寫數(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)然每一個讀寫的使用都是可以分開的。為了更好的來使用io。流里面的讀寫,建議使用BufferedInputStream、BufferedOutputStream
以上就是JAVA.io讀寫文件方式匯總的詳細(xì)內(nèi)容,更多關(guān)于JAVA.io讀寫文件的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
