-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileIOStreams.java
31 lines (26 loc) · 1.15 KB
/
FileIOStreams.java
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
import java.io.*;
public class FileIOStreams {
public static void main(String[] args) {
String inputFile = "input.txt";
String outputFile = "output.txt";
try (FileInputStream fis = new FileInputStream(inputFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis))) {
System.out.println("Reading from " + inputFile + "...");
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
}
try (FileOutputStream fos = new FileOutputStream(outputFile);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos))) {
String content = "Hello, this is a test message written to the file!";
writer.write(content);
writer.newLine();
System.out.println("Data written to " + outputFile);
} catch (IOException e) {
System.out.println("Error writing file: " + e.getMessage());
}
}
}