Java - Character Write to File

Description

Character Write to File

Demo

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

public class Main {
  public static void main(String[] args) {
    String destFile = "asdf.txt";

    try (BufferedWriter bw = new BufferedWriter(new FileWriter(destFile))) {
      // Write the text to the writer
      bw.append("line 1");
      bw.newLine();//from   w w  w  . j  a  v  a  2 s .c  om
      bw.append("line 2");
      bw.newLine();
      bw.append("line 3");
      bw.newLine();
      bw.append("from book2s.com");

      // Flush the written text
      bw.flush();

      System.out.println("Text was written to "
          + (new File(destFile)).getAbsolutePath());
    } catch (FileNotFoundException e1) {
      System.out.println(destFile);
    } catch (IOException e2) {
      e2.printStackTrace();
    }
  }
}

Result