Core Java Tutorial

Core Java Tutorial

How to Writing a file using FileWriter

FileWriter

FileWriter can be used to write a character file, It can be used to write a char or String in java.

Example

 

import java.io.FileWriter;

import java.io.IOException;

public class FileWriterExample {

 public static void main(String[] args) {

  try {

   FileWriter fw = new FileWriter("sam.txt");

   fw.write("Sample content to write in a file"); // writing a string to a file

   fw.write("\n"); // create a new line

   fw.write("second line of a sam file");

   fw.flush(); // flush the content before close

   fw.close();

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }


 }

}

Run the above program and try refreshing the directory or workspace to find the changes