generated from SACHSTech/BasicJavaTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
30 lines (24 loc) · 825 Bytes
/
Main.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
package fileIODemo;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
displayWords("src/fileIODemo/words.txt");
}
public static void displayWords(String filename) throws IOException {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(filename));
String word;
// Read each line (word) from the file
while ((word = reader.readLine()) != null) {
System.out.println(word); // Print each word (line) to the console
}
} finally {
if (reader != null) {
reader.close(); // Ensure the reader is closed
}
}
}
}