-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathShowFile2.java
More file actions
54 lines (49 loc) · 1.51 KB
/
ShowFile2.java
File metadata and controls
54 lines (49 loc) · 1.51 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
package chapter13;
import java.io.*;
/*
Display a text file
To use this program,specify the name
of the file that you want to see.
For example, to see a file called TEST.txt
use the following command line.
Java ShowFile2 TEST.txt
This variation wraps the code that opens and
access the file within a single try block.
The file is closed by the final block
*/
public class ShowFile2 {
public static void main(String[] args) {
FileInputStream fin = null;
int i;
//First confirm that a file name has been specified
if (args.length != 1) {
System.out.println("Usage: ShowFile2 fileName");
return;
}
//At this point , the file is open and can be read
//The following read characters until EOF is encountered.
try {
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");
}
finally {
//Close file in all cases
try {
if(fin!=null){
fin.close();
}
} catch (IOException e) {
System.out.println("Error Closing File");
}
}
}
}