import java.io.*; public class PrintWriterExample { public static void main(String[] args) { try { PrintWriter writer = new PrintWriter("output.txt", "UTF-8"); writer.println("This is the first line."); writer.println("This is the second line."); writer.close(); } catch(Exception e) { System.out.println("Error: " + e.getMessage()); } } }
import java.io.*; public class PrintWriterExample { public static void main(String[] args) { PrintWriter writer = new PrintWriter(System.out); writer.println("Hello, World!"); writer.close(); } }In conclusion, PrintWriter is a useful class in Java for generating formatted text output to a file or console. It is part of the java.io package library.