Home  >  Article  >  Java  >  Several methods of writing files in java

Several methods of writing files in java

黄舟
黄舟Original
2016-12-12 13:20:401232browse

One, FileWritter writes files

FileWritter, character stream writes characters to files. By default it will replace all existing content with new content, however, when a true is specified (Boolean) value as the second argument to the FileWritter constructor, which retains the existing content and appends the new content at the end of the file.

1. Replace all existing content with new content.

new FileWriter(file);2. Keep the existing content and append the new content to the end of the file.

代码如下:
new FileWriter(file,true);

Append file example
A text file named "javaio-appendfile.txt" and containing the following content.

ABC Hello append new content 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();
     }
    }
}

Result
Now, the content of the text file "javaio-appendfile.txt" is updated as follows:

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


Second, BufferedWriter writes files

Buffer characters (BufferedWriter ) is a character stream class to handle character data. Unlike byte streams (data converted into bytes), you can write string, array or character data directly to a file.

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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn