<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    中文JAVA技術平等自由協作創造

    Java專題文章博客和開源

    常用鏈接

    統計

    最新評論

    java寫入文件的幾種方法小結

      java寫入文件的幾種方法小結
      一,FileWritter寫入文件
      FileWritter, 字符流寫入字符到文件。默認情況下,它會使用新的內容取代所有現有的內容,然而,當指定一個true (布爾)值作為FileWritter構造函數的第二個參數,它會保留現有的內容,并追加新內容在文件的末尾 www.jx-jf.com
      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);
      BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
      bufferWritter.write(data);
      bufferWritter.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寫入文件 www.jx-jf.com 
      文件輸出流是一種用于處理原始二進制數據的字節流類。為了將數據寫入到文件中,必須將數據轉換為字節,并保存到文件。請參閱下面的完整的例子。
      代碼如下
      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例如,www.111cn.net使用新的"嘗試資源關閉"的方法來輕松處理文件。
      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();
      }
      }
      }

    posted on 2014-03-23 09:54 好不容易 閱讀(219) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    PK10開獎 PK10開獎
    主站蜘蛛池模板: 一级毛片**不卡免费播| 日韩少妇内射免费播放| 午夜视频免费在线观看| 亚洲精品国产精品乱码不99| 三上悠亚在线观看免费| 亚洲精品白浆高清久久久久久 | 亚洲中文字幕视频国产| 一级做受视频免费是看美女| 亚洲一区视频在线播放 | 亚洲国产日韩一区高清在线| 日韩免费观看一区| 亚洲黄色免费在线观看| 久久不见久久见中文字幕免费| 亚洲最大的成人网| 免费中文字幕不卡视频| 一级毛片免费播放视频| 久久精品国产亚洲av麻豆 | 欧洲美熟女乱又伦免费视频| 亚洲AV无码国产剧情| 亚洲精品成人a在线观看| 中文字幕视频在线免费观看| 久久精品夜色国产亚洲av| 国产四虎免费精品视频| 亚洲av综合av一区二区三区| 亚洲精品网站在线观看不卡无广告 | 国产亚洲婷婷香蕉久久精品| 少妇人妻偷人精品免费视频| 亚洲综合成人婷婷五月网址| 亚洲第一成人影院| 午夜不卡久久精品无码免费| 亚洲欧洲另类春色校园网站| 免费看国产曰批40分钟| 久久爰www免费人成| 亚洲妇女熟BBW| 国产亚洲精久久久久久无码77777 国产亚洲精品成人AA片新蒲金 | 国产成人亚洲综合无码| 精品成在人线AV无码免费看 | 免费观看无遮挡www的小视频| 亚洲国产成人久久精品软件| 亚洲熟妇av一区二区三区| 24小时日本电影免费看|