-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileReader.java
More file actions
93 lines (72 loc) · 2.4 KB
/
Copy pathFileReader.java
File metadata and controls
93 lines (72 loc) · 2.4 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.preisinger.util;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class FileReader {
private static final String DEFAULT_DIR = "C:\\Users\\johan\\Downloads\\";
private void validateFile(final String fileName) throws IllegalArgumentException {
if(fileName == null || fileName.length() == 0){
throw new IllegalArgumentException("The filename is either null or empty");
}
}
private String findDir(String dir){
if(dir == null || dir.length() == 0){
dir = DEFAULT_DIR;
}
return dir;
}
private Scanner openFile(final String fileName, final String dir) throws FileNotFoundException {
File file = new File(dir + fileName);
System.out.println(file.getAbsolutePath());
return new Scanner(file);
}
public List<String> lineByLine(final String fileName, String dir) throws FileNotFoundException, IllegalArgumentException {
validateFile(fileName);
List<String> lines = new ArrayList<>();
System.out.println("Reading a text file line by line: ");
Scanner sc = openFile(fileName, findDir(dir));
while (sc.hasNext()) {
String str = sc.nextLine();
lines.add(str);
}
sc.close();
return lines;
}
public List<String> wordByWord(final String fileName, String dir) throws FileNotFoundException, IllegalArgumentException {
validateFile(fileName);
List<String> words = new ArrayList<>();
System.out.println("Reading a text file word by word: ");
Scanner sc = openFile(fileName, findDir(dir));
while (sc.hasNext()) {
String str = sc.next();
words.add(str);
}
sc.close();
return words;
}
public List<List<Integer>> matrix(final String fileName, String dir) throws FileNotFoundException, IllegalArgumentException {
validateFile(fileName);
List<List<Integer>> matrix = new ArrayList<>();
System.out.println("Reading a text file as matrix: ");
Scanner sc = openFile(fileName, findDir(dir));
while (sc.hasNext()) {
String str = sc.nextLine();
String[] values = str.split(" ");
List<Integer> columns = new ArrayList<>();
for(int i = 0; i < values.length; i++){
columns.add(Integer.parseInt(values[i]));
}
matrix.add(columns);
}
sc.close();
return matrix;
}
}