FileWriter

In this chapter you will learn:

  1. How to use FileWriter
  2. Copy a file with FileReader and FileWriter
  3. Append to a text file FileWriter

Use FileWriter

FileWriter creates a Writer that you can use characters to write to a file. Its most commonly used constructors are shown here:

  • FileWriter(String filePath)
    filePath is the full path name of a file
  • FileWriter(String filePath, boolean append)
    If append is true, then output is appended to the end of the file.
  • FileWriter(File fileObj)
    fileObj is a File object that describes the file.
  • FileWriter(File fileObj, boolean append)
    If append is true, then output is appended to the end of the file.

FileWriter will create a file before opening it if the file does not exist. Attempt to open a read-only file results an IOException.

import java.io.FileWriter;
/*j a v a  2s .  co m*/
public class Main {
  public static void main(String args[]) throws Exception {
    FileWriter fw = new FileWriter(args[0]);
    for (int i = 0; i < 12; i++) {
      fw.write("Line " + i + " java2s.com \n");
    }
    fw.close();
  }
}

The following create a FileWriter to write to LPT1.

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
// j a  v a2s .  c  om
public class MainClass {
  public static void main(String[] args) {
    try {
      FileWriter fw = new FileWriter("LPT1:");

      PrintWriter pw = new PrintWriter(fw);
      String s = "www.java2s.com";

      int i, len = s.length();

      for (i = 0; len > 80; i += 80) {
        pw.print(s.substring(i, i + 80));
        pw.print("\r\n");
        len -= 80;
      }

      if (len > 0) {
        pw.print(s.substring(i));
        pw.print("\r\n");
      }

      pw.close();
    } catch (IOException e) {
      System.out.println(e);
    }
  }
}

Copy a file with FileReader and FileWriter

FileReader and FileWriter can work together to copy a text file character by character.

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*from jav a  2 s . com*/
public class Main {
  public static void main(String[] args) throws IOException {
    File inputFile = new File("farrago.txt");
    File outputFile = new File("outagain.txt");

    FileReader in = new FileReader(inputFile);
    FileWriter out = new FileWriter(outputFile);
    int c;

    while ((c = in.read()) != -1)
      out.write(c);

    in.close();
    out.close();
  }
}

Append to a text file FileWriter

When constructing a FileWriter with given a File object, if the second argument is true, then bytes will be written to the end of the file not the beginning.

import java.io.BufferedWriter;
import java.io.FileWriter;
//  j a v a2 s.c  o  m
public class Main {
  public static void main(String[] argv) throws Exception {
    BufferedWriter out = new BufferedWriter(new FileWriter("filename", true));
    out.write("aString");
    out.close();
  }
}

Next chapter...

What you will learn in the next chapter:

  1. How to use Java BufferedWriter to write character faster
  2. Wrap System.out with BufferedWriter
  3. Write portion of a string with BufferedWriter