java寫入檔案的幾種方法分享

來源:互聯網
上載者:User

標籤:

轉自:http://www.jb51.net/article/47062.htm

 

一,FileWritter寫入檔案

FileWritter, 字元流寫入字元到檔案。預設情況下,它會使用新的內容取代所有現有的內容,然而,當指定一個true (布爾)值作為FileWritter建構函式的第二個參數,它會保留現有的內容,並追加新內容在檔案的末尾。

1. 替換所有現有的內容與新的內容。

  new FileWriter(file); 

2. 保留現有的內容和附加在該檔案的末尾的新內容。

new FileWriter(file,true);

 

 

追加檔案樣本 
一個文字檔,命名為“javaio-appendfile.txt”,並包含以下內容。

ABC Hello追加新內容 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);      fileWritter.write(data);      fileWritter.close();      System.out.println("Done");     }catch(IOException e){      e.printStackTrace();     }    }}

 

結果 
現在,文字檔“javaio-appendfile.txt”內容更新如下:

ABC Hello This content will append to the end of the file


二,BufferedWriter寫入檔案

緩衝字元(BufferedWriter )是一個字元流類來處理字元資料。不同於位元組流(資料轉換成位元組),你可以直接寫字串,數組或字元資料儲存到檔案。

複製代碼代碼如下:
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();  } }}

 

三,FileOutputStream寫入檔案


檔案輸出資料流是一種用於處理原始位元據的位元組流類。為了將資料寫入到檔案中,必須將資料轉換為位元組,並儲存到檔案。請參閱下面的完整的例子。

 

複製代碼代碼如下:
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();  } }}

 

 

java寫入檔案的幾種方法分享

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.