Writing a String to a file

Typically, Create an instance of FileWriter (specify the filename) and wrap it up in a BufferedWriter.

Main.java:

import java.io.*;
 
public class Main {
   public static void main(String args[]) {
      String s = "First line" + System.getProperty("line.separator");
      s += "Second line";
 
      try {
         BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt"));
         bw.write(s);
         bw.close();
      } catch(IOException e) {
         System.err.println(e);
      }
   }
}