import java.io.*; public class FileWriterExample { public static void main(String[] args) { try { FileWriter writer = new FileWriter("output.txt"); writer.write("Hello World"); writer.close(); } catch (IOException e) { e.printStackTrace(); } } }
import java.io.*; public class BufferedWriterExample { public static void main(String[] args) { try { BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt")); writer.write('H'); writer.write('e'); writer.write('l'); writer.write('l'); writer.write('o'); writer.write(' '); writer.write('W'); writer.write('o'); writer.write('r'); writer.write('l'); writer.write('d'); writer.close(); } catch (IOException e) { e.printStackTrace(); } } }
import java.io.*; public class PrintWriterExample { public static void main(String[] args) { PrintWriter printWriter = new PrintWriter(System.out); printWriter.write("Hello World"); printWriter.flush(); } }In this example, the PrintWriter class is used to write a string "Hello World" to the console. Package library: java.io