Java - Write text to file

By xngo on June 14, 2019

/**
 * Show how to write text to file.
 * @author Xuan Ngo
 */
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
 
public class WriteToFile
{
  public static void main(String[] args)
  {
    try
    {
      FileWriter oFileWriter = new FileWriter("filename.txt");
      BufferedWriter oBufferedWriter = new BufferedWriter(oFileWriter);
      oBufferedWriter.write("Java");
      oBufferedWriter.close();
    }
    catch(IOException ex)
    {
      System.out.println(ex.getMessage());
    } 
  }
 
}

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.