-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestFileReader.java
More file actions
31 lines (29 loc) · 848 Bytes
/
testFileReader.java
File metadata and controls
31 lines (29 loc) · 848 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
27
28
29
30
31
/*
*In Terminal/Commandline, enter these two commands:
*javac testFileReader.java
*java testFileReader
*/
import java.io.*;
import java.util.StringTokenizer;
public class testFileReader {
public static void main(String[] args) throws Exception {
int numOfTokens = 3;
try {
FileReader file = new FileReader("testfile.txt");
BufferedReader reader = new BufferedReader(file);
String line = reader.readLine();
while (line != null) {
StringTokenizer tokens = new StringTokenizer(line);
if (tokens.countTokens() == numOfTokens) {
for (int i = 1; i <= numOfTokens; i++) {
System.out.println(tokens.nextToken());
}
}
line = reader.readLine();
}
} catch (Exception e) {
System.err.println("Exception");
// do something
}
}
}