Java: Write File
By Xah Lee. Date: .
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class WriteStringToFile {
public static void main(String[] args) {
String content = "i have 3 cats.";
String fileName = "output.txt";
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
writer.write(content);
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}