java寫入文件的幾種方法分享
一,F(xiàn)ileWritter寫入文件
FileWritter, 字符流寫入字符到文件。默認情況下,它會使用新的內(nèi)容取代所有現(xiàn)有的內(nèi)容,然而,當指定一個true (布爾)值作為FileWritter構(gòu)造函數(shù)的第二個參數(shù),它會保留現(xiàn)有的內(nèi)容,并追加新內(nèi)容在文件的末尾。
1. 替換所有現(xiàn)有的內(nèi)容與新的內(nèi)容。
new FileWriter(file);2. 保留現(xiàn)有的內(nèi)容和附加在該文件的末尾的新內(nèi)容。
new FileWriter(file,true);
追加文件示例
一個文本文件,命名為“javaio-appendfile.txt”,并包含以下內(nèi)容。
ABC Hello追加新內(nèi)容 new FileWriter(file,true)
package com.yiibai.file; import java.io.File; import java.io.FileWriter; import java.io.BufferedWriter; import java.io.IOException; public class AppendToFileExample { public static void main( String[] args ) { try{ String data = " This content will append to the end of the file"; File file =new File("javaio-appendfile.txt"); //if file doesnt exists, then create it if(!file.exists()){ file.createNewFile(); } //true = append file FileWriter fileWritter = new FileWriter(file.getName(),true); BufferedWriter bufferWritter = new BufferedWriter(fileWritter); bufferWritter.write(data); bufferWritter.close(); System.out.println("Done"); }catch(IOException e){ e.printStackTrace(); } } }
結(jié)果
現(xiàn)在,文本文件“javaio-appendfile.txt”內(nèi)容更新如下:
ABC Hello This content will append to the end of the file
二,BufferedWriter寫入文件
緩沖字符(BufferedWriter )是一個字符流類來處理字符數(shù)據(jù)。不同于字節(jié)流(數(shù)據(jù)轉(zhuǎn)換成字節(jié)),你可以直接寫字符串,數(shù)組或字符數(shù)據(jù)保存到文件。
package com.yiibai.iofile; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class WriteToFileExample { public static void main(String[] args) { try { String content = "This is the content to write into file"; File file = new File("/users/mkyong/filename.txt"); // if file doesnt exists, then create it if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); bw.write(content); bw.close(); System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } } }
三,F(xiàn)ileOutputStream寫入文件
文件輸出流是一種用于處理原始二進制數(shù)據(jù)的字節(jié)流類。為了將數(shù)據(jù)寫入到文件中,必須將數(shù)據(jù)轉(zhuǎn)換為字節(jié),并保存到文件。請參閱下面的完整的例子。
package com.yiibai.io; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class WriteFileExample { public static void main(String[] args) { FileOutputStream fop = null; File file; String content = "This is the text content"; try { file = new File("c:/newfile.txt"); fop = new FileOutputStream(file); // if file doesnt exists, then create it if (!file.exists()) { file.createNewFile(); } // get the content in bytes byte[] contentInBytes = content.getBytes(); fop.write(contentInBytes); fop.flush(); fop.close(); System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fop != null) { fop.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
//更新的JDK7例如,使用新的“嘗試資源關閉”的方法來輕松處理文件。
package com.yiibai.io; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class WriteFileExample { public static void main(String[] args) { File file = new File("c:/newfile.txt"); String content = "This is the text content"; try (FileOutputStream fop = new FileOutputStream(file)) { // if file doesn't exists, then create it if (!file.exists()) { file.createNewFile(); } // get the content in bytes byte[] contentInBytes = content.getBytes(); fop.write(contentInBytes); fop.flush(); fop.close(); System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } } }
下面是其他網(wǎng)友的補充
java.io的幾種讀寫文件的方式
一、java把這些不同來源和目標的數(shù)據(jù)都統(tǒng)一抽象為數(shù)據(jù)流。
Java語言的輸入輸出功能是十分強大而靈活的。
在Java類庫中,IO部分的內(nèi)容是很龐大的,因為它涉及的領域很廣泛:標準輸入輸出,文件的操作,網(wǎng)絡上的數(shù)據(jù)流,字符串流,對象流,zip文件流等等。
這里介紹幾種讀寫文件的方式
二、InputStream、OutputStream(字節(jié)流)
//讀取文件(字節(jié)流) FileInputStream in = new FileInputStream("d:\\1.txt"); //寫入相應的文件 FileOutputStream out = new FileOutputStream("d:\\2.txt"); //讀取數(shù)據(jù) //一次性取多少字節(jié) byte[] bytes = new byte[2048]; //接受讀取的內(nèi)容(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,"UTF-8"); #這里可以實現(xiàn)字節(jié)到字符串的轉(zhuǎn)換,比較實用 System.out.println(str); //寫入相關文件 out.write(bytes, 0, n); //清除緩存向文件寫入數(shù)據(jù) out.flush(); } //關閉流 in.close(); out.close();
三、BufferedInputStream、BufferedOutputStream(緩存字節(jié)流)使用方式和字節(jié)流差不多,但是效率更高(推薦使用)
//讀取文件(緩存字節(jié)流) BufferedInputStream in=new BufferedInputStream(new FileInputStream("d:\\1.txt")); //寫入相應的文件 BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream("d:\\2.txt")); //讀取數(shù)據(jù) //一次性取多少字節(jié) byte[] bytes = new byte[2048]; //接受讀取的內(nèi)容(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,"UTF-8"); System.out.println(str); //寫入相關文件 out.write(bytes, 0, n); //清除緩存,向文件寫入數(shù)據(jù) out.flush(); } //關閉流 in.close(); out.close();
四、InputStreamReader、OutputStreamWriter(字節(jié)流,這種方式不建議使用,不能直接字節(jié)長度讀寫)。使用范圍用做字符轉(zhuǎn)換
//讀取文件(字節(jié)流) InputStreamReader in = new InputStreamReader(new FileInputStream("d:\\1.txt"),"UTF-8"); //寫入相應的文件 OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("d:\\2.txt")); //讀取數(shù)據(jù) //循環(huán)取出數(shù)據(jù) char[] chars = new char[2048]; int len = -1; while ((len = in.read(chars,0,chars.length)) != -1) { System.out.println(len); //寫入相關文件 out.write(chars,0,len); //清除緩存 out.flush(); } //關閉流 in.close(); out.close();
五、BufferedReader、BufferedWriter(緩存流,提供readLine方法讀取一行文本)
FileInputStream fileInputStream = new FileInputStream("d:\\1.txt"); FileOutputStream fileOutputStream = new FileOutputStream("d:\\2.txt", true); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"UTF-8"); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,"UTF-8"); //讀取文件(字符流) BufferedReader in = new BufferedReader(inputStreamReader,"UTF-8"));#這里主要是涉及中文 //BufferedReader in = new BufferedReader(new FileReader("d:\\1.txt"))); //寫入相應的文件 BufferedWriter out = new BufferedWriter(outputStreamWriter,"UTF-8")); //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); //寫入相關文件 out.write(str); out.newLine(); //清除緩存向文件寫入數(shù)據(jù) out.flush(); } //關閉流 in.close(); out.close();
六、Reader、PrintWriter(PrintWriter這個很好用,在寫數(shù)據(jù)的同事可以格式化)
//讀取文件(字節(jié)流) Reader in = new InputStreamReader(new FileInputStream("d:\\1.txt"),"UTF-8"); //寫入相應的文件 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); //寫入相關文件 out.write(len); //清除緩存 out.flush(); } //關閉流 in.close(); out.close();
七、基本的幾種用法就這么多,當然每一個讀寫的使用都是可以分開的。為了更好的來使用io。流里面的讀寫,建議使用BufferedInputStream、BufferedOutputStream
相關文章
SpringBoot整合Redis實現(xiàn)熱點數(shù)據(jù)緩存的示例代碼
這篇文章主要介紹了SpringBoot中整合Redis實現(xiàn)熱點數(shù)據(jù)緩存,本文以IDEA?+?SpringBoot作為?Java中整合Redis的使用?的測試環(huán)境,結(jié)合實例代碼給大家詳細講解,需要的朋友可以參考下2023-03-03Mybatis之a(chǎn)ssociation和collection用法
這篇文章主要介紹了Mybatis之a(chǎn)ssociation和collection用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02SSH框架網(wǎng)上商城項目第12戰(zhàn)之添加和更新商品功能
這篇文章主要介紹了SSH框架網(wǎng)上商城項目第12戰(zhàn)之添加和更新商品功能的實現(xiàn)代碼,感興趣的小伙伴們可以參考一下2016-06-06