一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java寫入文件的幾種方法分享

java寫入文件的幾種方法分享

2019-06-17 10:10腳本之家 Java教程

這篇文章主要介紹了java寫入文件的幾種方法,需要的朋友可以參考下

一,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);
             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寫入文件


文件輸出流是一種用于處理原始二進制數據的字節流類。為了將數據寫入到文件中,必須將數據轉換為字節,并保存到文件。請參閱下面的完整的例子。

 

復制代碼代碼如下:

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();
  }
 }
}

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产自产2023最新麻豆 | 国产高清在线精品一区二区 | 99久久精品免费精品国产 | 色男人综合 | china中国小帅gayxnxx | 爱情岛论坛亚洲一号路线 | 爱情岛论坛亚洲品质自拍视频 | 亚洲高清在线视频 | 色图18p | 精品日产1区2卡三卡麻豆 | 高h辣h双处全是肉军婚 | 久久亚洲精品成人 | 91在线亚洲精品一区 | 滑进了柔佳火热紧夹的 | 免费看成年视频网页 | 白丝捆绑调教 | 欧美三级小视频 | 色鬼网| 91porny.首页| 好大好硬好深好爽想要之黄蓉 | 国产福利资源 | 国产精品久久久久无毒 | 天天做天天爱天天一爽一毛片 | 久久热国产在线视频 | jizzjizz大学生| 亚洲国产福利精品一区二区 | 亚洲看片lutube在线入口 | 天天做天天爽 | 男女男精品网站 | 99rv精品视频在线播放 | 国产精品美女久久久久网站 | 白丝憋尿 | 草逼视频网址 | 色老女人| japanese乱子mate| 国产一区国产二区国产三区 | chinese国产老太性 | 禁忌第一季第3季 | 亚洲激情自拍偷拍 | 欧美调教打屁股spank视频 | 视频一本大道香蕉久在线播放 |