-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathShowFile.java
More file actions
50 lines (44 loc) · 1.3 KB
/
ShowFile.java
File metadata and controls
50 lines (44 loc) · 1.3 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
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 ShowFile TEST.txt
*/
public class ShowFile {
public static void main(String[] args) {
int i;
FileInputStream fin;
//First confirm that a file name has been specified.
if (args.length != 1) {
System.out.println("Usage: ShowFile fileName");
return;
}
//Attempt to open the file
try {
fin = new FileInputStream(args[0]);
} catch (FileNotFoundException e) {
System.out.println("Cannot open file");
return;
}
//At this point , the file is open and can be read
//The following read characters until EOF is encountered.
try {
do {
i = fin.read();
if (i != -1) System.out.println((char) i);
} while (i != -1);
} catch (IOException e) {
System.out.println("Error Reading File");
}
//Close the file
try {
fin.close();
} catch (IOException e) {
System.out.println("Error Closing File");
}
}
}