Java I/O How to - Create a Writer from a file name








Question

We would like to know how to create a Writer from a file name.

Answer

/*from  w ww  .  ja  va 2  s . c  o  m*/
import java.io.FileWriter;
import java.io.IOException;

public class MainClass {
  public static void main(String[] a) {
    FileWriter writer;
    try {
      writer = new FileWriter("myFile.txt");
      char c = 'A';
      writer.write(c);
      writer.close();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }
}

The code above generates the following result.