欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2014年02月20日 15:25:56   投稿:shangke  
這篇文章主要介紹了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

相關文章

  • java線程阻塞中斷與LockSupport使用介紹

    java線程阻塞中斷與LockSupport使用介紹

    本文將詳細介紹java線程阻塞中斷和LockSupport的使用,需要了解更多的朋友可以參考下
    2012-12-12
  • Java線程的三種創(chuàng)建方式

    Java線程的三種創(chuàng)建方式

    這篇文章主要給大家分享的是ava線程的三種創(chuàng)建方式,Thread、Runnable和Thread、Runnable和Thread,想了解具體方式的小伙伴可以參考下面文章內(nèi)容,希望對你有所幫助
    2021-11-11
  • java8中的lambda表達式簡介

    java8中的lambda表達式簡介

    Lambda表達式類似匿名函數(shù),簡單地說,它是沒有聲明的方法,也即沒有訪問修飾符、返回值聲明和方法名,這篇文章主要介紹了java8?中的lambda表達式簡介,需要的朋友可以參考下
    2022-06-06
  • SpringBoot整合Redis實現(xiàn)熱點數(shù)據(jù)緩存的示例代碼

    SpringBoot整合Redis實現(xiàn)熱點數(shù)據(jù)緩存的示例代碼

    這篇文章主要介紹了SpringBoot中整合Redis實現(xiàn)熱點數(shù)據(jù)緩存,本文以IDEA?+?SpringBoot作為?Java中整合Redis的使用?的測試環(huán)境,結(jié)合實例代碼給大家詳細講解,需要的朋友可以參考下
    2023-03-03
  • Mybatis之a(chǎn)ssociation和collection用法

    Mybatis之a(chǎn)ssociation和collection用法

    這篇文章主要介紹了Mybatis之a(chǎn)ssociation和collection用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java集合包中的fail fast機制詳解

    Java集合包中的fail fast機制詳解

    這篇文章主要介紹了Java集合包中的fail fast機制詳解,當我們使用iterator迭代器遍歷一個集合的過程中,如果其它線程,或者它自己向這個集合新增或刪除了一個key-value,那么當前線程就會拋出ConcurrentModificationException異常,需要的朋友可以參考下
    2023-12-12
  • java獲取文件的inode標識符的方法

    java獲取文件的inode標識符的方法

    這篇文章主要介紹了java獲取文件的inode標識符,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • SSH框架網(wǎng)上商城項目第12戰(zhàn)之添加和更新商品功能

    SSH框架網(wǎng)上商城項目第12戰(zhàn)之添加和更新商品功能

    這篇文章主要介紹了SSH框架網(wǎng)上商城項目第12戰(zhàn)之添加和更新商品功能的實現(xiàn)代碼,感興趣的小伙伴們可以參考一下
    2016-06-06
  • 淺析JDK12的五大重要新特性(推薦)

    淺析JDK12的五大重要新特性(推薦)

    這篇文章主要介紹了JDK12的五大重要新特性,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05
  • Java?C++分別實現(xiàn)滑動窗口的最大值

    Java?C++分別實現(xiàn)滑動窗口的最大值

    這篇文章主要介紹了分別通過Java和C++實現(xiàn)滑動窗口最大值,即給定一個數(shù)組?nums?和滑動窗口的大小?k,請找出所有滑動窗口里的最大值。感興趣的可以了解一下
    2021-12-12

最新評論