-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathShowFile3.java
More file actions
35 lines (29 loc) · 1.07 KB
/
ShowFile3.java
File metadata and controls
35 lines (29 loc) · 1.07 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
package chapter13;
/* This version of the ShowFile program uses a try with resources statement
to automatically close a file after it is no longer needed.
*/
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ShowFile3 {
public static void main(String[] args) {
int i;
//First confirm that a file name has been specified.
if (args.length != 1) {
System.out.println("Usage: ShowFile3 FileName");
return;
}
//The following code uses try with a resource statement to open
//a file and then automatically close it when the try block is left
try (FileInputStream fin = new FileInputStream(args[0])) {
do {
i = fin.read();
if (i != -1) System.out.println((char) i);
} while (i != -1);
} catch (FileNotFoundException e) {
System.out.println("File Not Found");
} catch (IOException e) {
System.out.println("An I/O Error Occurred");
}
}
}