-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp2.java
More file actions
26 lines (22 loc) · 703 Bytes
/
Copy pathApp2.java
File metadata and controls
26 lines (22 loc) · 703 Bytes
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
package tutorial30;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class App2 {
// cleaner implementation of reading a file only available in java 7 or later
public static void main(String[] args) {
File file = new File("test.txt");
try (BufferedReader br = new BufferedReader(new FileReader(file))){
String line;
while ((line = br.readLine()) != null){
System.out.println(line);
}
} catch (FileNotFoundException e) {
System.out.println("File not found: " + file);
} catch (IOException e) {
System.out.println("Unable to read file: " + file);
}
}
}