-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileIOExample.java
More file actions
65 lines (57 loc) · 1.79 KB
/
Copy pathFileIOExample.java
File metadata and controls
65 lines (57 loc) · 1.79 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
53
54
55
56
57
58
59
60
61
62
63
64
package java1.io;
import java.io.*;
/**
* Java 1.0 Basic I/O Example Demonstrates file reading and writing
*/
public class FileIOExample
{
public static void main(String[] args)
{
System.out.println("=== Java 1.0 Basic I/O ===\n");
String fileName = "java1_example.txt";
System.out.println("1. Writing to File");
System.out.println("-----------------");
try
{
FileWriter writer = new FileWriter(fileName);
writer.write("Hello, Java 1.0!\n");
writer.write("This is a file I/O example.\n");
writer.write("Java 1.0 introduced basic file operations.");
writer.close();
System.out.println("File written successfully: " + fileName);
}
catch (IOException e)
{
System.err.println("Error writing file: " + e.getMessage());
}
System.out.println("\n2. Reading from File");
System.out.println("-------------------");
try
{
FileReader reader = new FileReader(fileName);
BufferedReader br = new BufferedReader(reader);
String line;
System.out.println("File contents:");
while ((line = br.readLine()) != null)
{
System.out.println(" " + line);
}
br.close();
}
catch (IOException e)
{
System.err.println("Error reading file: " + e.getMessage());
}
System.out.println("\n3. Console Output");
System.out.println("---------------");
System.out.println("Using System.out.println()");
System.out.print("Using System.out.print() - no newline");
System.out.println(" (continued)");
System.err.println("Using System.err.println() for errors");
System.out.println("\nKey Features:");
System.out.println("- FileReader/FileWriter for character I/O");
System.out.println("- BufferedReader for efficient reading");
System.out.println("- Exception handling with try-catch");
System.out.println("- Console I/O with System.out/System.err");
}
}