最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2014年02月20日 15:25:56   投稿:shangke  
這篇文章主要介紹了java寫入文件的幾種方法,需要的朋友可以參考下

一,F(xiàn)ileWritter寫入文件

FileWritter, 字符流寫入字符到文件。默認(rèn)情況下,它會使用新的內(nèi)容取代所有現(xiàn)有的內(nèi)容,然而,當(dāng)指定一個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例如,使用新的“嘗試資源關(guān)閉”的方法來輕松處理文件。

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把這些不同來源和目標(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é)流)
FileInputStream in = new FileInputStream("d:\\1.txt");
//寫入相應(yīng)的文件
FileOutputStream 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,"UTF-8"); #這里可以實現(xiàn)字節(jié)到字符串的轉(zhuǎn)換,比較實用
  System.out.println(str);
  //寫入相關(guān)文件
  out.write(bytes, 0, n);
  //清除緩存向文件寫入數(shù)據(jù)
  out.flush();
}
//關(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,"UTF-8");
  System.out.println(str);
  //寫入相關(guān)文件
  out.write(bytes, 0, n);
  //清除緩存,向文件寫入數(shù)據(jù)
  out.flush();
}
//關(guān)閉流
in.close();
out.close();

四、InputStreamReader、OutputStreamWriter(字節(jié)流,這種方式不建議使用,不能直接字節(jié)長度讀寫)。使用范圍用做字符轉(zhuǎn)換

//讀取文件(字節(jié)流)
InputStreamReader in = new InputStreamReader(new FileInputStream("d:\\1.txt"),"UTF-8");
//寫入相應(yīng)的文件
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);
  //寫入相關(guān)文件
  out.write(chars,0,len);
  //清除緩存
  out.flush();
}
//關(guān)閉流
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")));
//寫入相應(yīng)的文件
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);
  //寫入相關(guān)文件
  out.write(str);
  out.newLine();
  //清除緩存向文件寫入數(shù)據(jù)
  out.flush();
}
//關(guān)閉流
in.close();
out.close();

六、Reader、PrintWriter(PrintWriter這個很好用,在寫數(shù)據(jù)的同事可以格式化)

//讀取文件(字節(jié)流)
    Reader in = new InputStreamReader(new FileInputStream("d:\\1.txt"),"UTF-8");
    //寫入相應(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

相關(guān)文章

  • Java 通過mave命令下載jar包的示例代碼

    Java 通過mave命令下載jar包的示例代碼

    這篇文章主要介紹了Java 通過mave命令下載jar的示例代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • RocketMQ消息發(fā)送與消息類別詳解

    RocketMQ消息發(fā)送與消息類別詳解

    這篇文章主要介紹了RocketMQ消息發(fā)送與消息類別詳解,事務(wù)消息的生產(chǎn)者執(zhí)行本地事務(wù),并根據(jù)事務(wù)執(zhí)行的結(jié)果選擇是否提交或回滾事務(wù),
    如果事務(wù)執(zhí)行成功并選擇提交事務(wù),則產(chǎn)生注冊成功消息,進入下一步,需要的朋友可以參考下
    2023-09-09
  • Java8 Instant時間戳使用小記

    Java8 Instant時間戳使用小記

    這篇文章主要給大家介紹了關(guān)于Java8 Instant時間戳使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • java反編譯工具jd-gui使用詳解

    java反編譯工具jd-gui使用詳解

    JD-GUI是一個獨立的圖形實用程序,顯示“.class”文件的Java源代碼,本文主要介紹了java反編譯工具jd-gui使用詳解,具有一定的參考價值,感興趣的可以了解一下
    2023-09-09
  • Windows下Java調(diào)用OCR進行圖片識別

    Windows下Java調(diào)用OCR進行圖片識別

    這篇文章主要為大家詳細介紹了Windows下Java調(diào)用OCR進行圖片識別,通過Tesseract-OCR對圖片進行識別,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Java中接口和抽象類的區(qū)別與相同之處

    Java中接口和抽象類的區(qū)別與相同之處

    這篇文章主要介紹了Java中接口和抽象類的區(qū)別與相同之處,本文講解了抽象類的概念、接口的概念、接口和抽象類的區(qū)別與聯(lián)系等內(nèi)容,需要的朋友可以參考下
    2015-06-06
  • java isInterrupted()判斷線程的實例講解

    java isInterrupted()判斷線程的實例講解

    在本篇內(nèi)容里小編給大家分享的是一篇關(guān)于java isInterrupted()判斷線程的實例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2021-05-05
  • Java開發(fā)中讀取XML與properties配置文件的方法

    Java開發(fā)中讀取XML與properties配置文件的方法

    這篇文章主要介紹了Java開發(fā)中讀取XML與properties配置文件的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-01-01
  • springboot打包JAR包瘦身lib和配置文件分離方式

    springboot打包JAR包瘦身lib和配置文件分離方式

    本文介紹了如何通過優(yōu)化POM.xml配置來減小JAR包大小,提高傳輸速度,主要步驟包括:指定打包環(huán)境和跳過編譯單元測試、JAR打包排除配置文件和lib、提供全量包便于開發(fā)環(huán)境使用、將lib和配置文件單獨復(fù)制出來
    2024-11-11
  • SpringBoot項目配置文件注釋亂碼的問題解決方案

    SpringBoot項目配置文件注釋亂碼的問題解決方案

    這篇文章主要介紹了SpringBoot 項目配置文件注釋亂碼的問題解決方案,文中通過圖文結(jié)合的方式給大家講解的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-07-07

最新評論

无极县| 洛扎县| 防城港市| 巴东县| 曲阳县| 广元市| 玉树县| 新巴尔虎右旗| 霍州市| 翁牛特旗| 城固县| 繁峙县| 盐池县| 武冈市| 周至县| 翼城县| 甘孜县| 土默特右旗| 富民县| 莱州市| 滦南县| 荥经县| 稻城县| 呼伦贝尔市| 皮山县| 杂多县| 德清县| 穆棱市| 梧州市| 巴林左旗| 德兴市| 邻水| 克东县| 泸西县| 铁力市| 南丰县| 临高县| 潞城市| 顺昌县| 佛冈县| 武汉市|