-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileReader.java
More file actions
46 lines (36 loc) · 875 Bytes
/
FileReader.java
File metadata and controls
46 lines (36 loc) · 875 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.io.*;
import java.util.Scanner;
public class FileReader {
File file;
Scanner scanner;
String filename;
public FileReader(String address) throws FileNotFoundException {
filename = address;
file = new File(filename);
scanner = new Scanner(file);
}
String nextLine() {
try {
return scanner.nextLine();
} catch (java.util.NoSuchElementException e) {
return null;
}
}
int fileLength() {
int lines = 0;
Scanner new_scanner = null;
try {
new_scanner = new Scanner(file);
while (true) {
new_scanner.nextLine();
lines++;
}
} catch (FileNotFoundException e) {
System.out.println("Failed to find file: " + filename);
e.printStackTrace();
return 0;
} catch (java.util.NoSuchElementException e) {}
new_scanner.close();
return lines;
}
}