- Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathReader.java
More file actions
Latest commit
66 lines (61 loc) · 1.64 KB
/
Copy pathReader.java
File metadata and controls
66 lines (61 loc) · 1.64 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
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.InputStreamReader;
importjava.util.ArrayList;
importjava.util.Arrays;
importjava.util.List;
publicclassReader {
publicstaticStringreadFile(StringfileName, Stringencoding) {
Filefile = newFile(fileName);
try {
FileInputStreaminStream = newFileInputStream(file);
BufferedReaderreader = newBufferedReader(newInputStreamReader(
inStream, encoding));
Stringline = newString();
Stringtext = newString();
while ((line = reader.readLine()) != null) {
text += line;
}
reader.close();
returntext;
} catch (Exceptione) {
// TODO Auto-generated catch block
e.printStackTrace();
}
returnnull;
}
/**
* 以非整齐的二维表的形式读取文件
*
* @param fileName
* 文件名
* @param regex
* 文件行内的分隔符
* @param encoding
* 编码方式
* @return matrix 二维表
*/
publicstaticList<String[]> readAsMatrix(StringfileName,
Stringregex, Stringencoding) {
List<String[]> matrix = newArrayList<String[]>();
Filefile = newFile(fileName);
try {
FileInputStreaminStream = newFileInputStream(file);
BufferedReaderreader = newBufferedReader(newInputStreamReader(
inStream, encoding));
Stringline = newString();
while ((line = reader.readLine()) != null) {
matrix.add(line.split(regex));
}
reader.close();
} catch (Exceptione) {
e.printStackTrace();
}
returnmatrix;
}
publicstaticvoidmain(String[] args) {
List<String[]> matrix = readAsMatrix("retail.txt", " ", "UTF-8");
System.out.println(matrix.size());
}
}