-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSortInfoReader.java
More file actions
61 lines (53 loc) · 1.69 KB
/
SortInfoReader.java
File metadata and controls
61 lines (53 loc) · 1.69 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
package src.classSrc;
import java.io.*;
public class SortInfoReader {
private String content;
public SortInfoReader(String fileNameText) {
File file = new File(fileNameText);
this.content = readToString(file);
}
public SortInfoReader(String fileNameText, String encoding) {
if (encoding.equals("UTF-8"))
this.content = readUTF8File(fileNameText);
else
this.content = "the encoding must be UTF-8!";
}
public static String readToString(File file) {
Long filelength = file.length();
byte[] filecontent = new byte[filelength.intValue()];
try {
FileInputStream in = new FileInputStream(file);
in.read(filecontent);
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return new String(filecontent);
}
public String getContent() {
return content;
}
public static String readUTF8File(String fileNameText) {
String encoding = "utf8";
String text = "";
try {
InputStream is = new FileInputStream(fileNameText);
int iAvail = is.available();
byte[] bytes = new byte[iAvail];
is.read(bytes);
text = new String(bytes, encoding);
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return text;
}
}