-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP51_ReadAndWriteTextFile.java
More file actions
52 lines (46 loc) · 1.86 KB
/
Copy pathP51_ReadAndWriteTextFile.java
File metadata and controls
52 lines (46 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package programs;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* ============================================================
* PROGRAM 51: Read and Write Text File
* ============================================================
* Problem: WAP to write multiple lines of text to a disk file
* using `BufferedWriter` and read it back line-by-line using `BufferedReader`.
* ============================================================
*/
public class P51_ReadAndWriteTextFile {
public static void main(String[] args) {
String filename = "program51_output.txt";
// 1. Write to File
System.out.println("=== 1. WRITING TO FILE ===");
try (BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) {
bw.write("Java Mastery Course - Program 51");
bw.newLine();
bw.write("File I/O operations are essential for persistence.");
bw.newLine();
bw.write("Try-with-resources prevents file lock leaks.");
bw.newLine();
System.out.println(" ✓ Successfully created and wrote to " + filename);
} catch (IOException e) {
System.out.println(" ❌ Write failed: " + e.getMessage());
}
// 2. Read from File
System.out.println("\n=== 2. READING FROM FILE ===");
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String line;
int lineNo = 1;
while ((line = br.readLine()) != null) {
System.out.printf(" Line %d: %s%n", lineNo++, line);
}
} catch (IOException e) {
System.out.println(" ❌ Read failed: " + e.getMessage());
}
// Cleanup
new File(filename).delete();
}
}