Writing data to a text file in Java

There are several ways, in this example we show you one of them using the FileWriter wrapped into a BufferedWriter, that is wrapped into a PrintWriter to allow you to use the println() methods that are easy to use.

import java.io.*;
 
public class Main {
   public static void main(String []args) {
      try {
         String line;
         BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"));
         PrintWriter pw = new PrintWriter(bw);
         pw.println("Esus");
         pw.println(true);
         pw.println('a');
         pw.write("Esus.com", 5, 3);
         pw.close();
      }
      catch (IOException e) {
         System.out.println(e);
      }
   }
}

Check out the API’s for more information on the print methods you can use!