Advanced Programming
Advanced Programming
REG NO : CT100/G/14130/21
1
b. Writing to Files:
• FileOutputStream and BufferedWriter:
try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) {
writer.write("Hello, World!"); } catch (IOException e) { e.printStackTrace(); }
• PrintWriter for Formatted Output:
try (PrintWriter writer = new PrintWriter("output.txt")) { writer.println("Line 1");
writer.println("Line 2"); } catch (FileNotFoundException e) { e.printStackTrace(); }
2
5. Exception Handling:
• File I/O operations can throw IOException, so it's essential to handle or
propagate these exceptions appropriately.