-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileReader.java
36 lines (25 loc) · 1.07 KB
/
FileReader.java
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
import org.apache.commons.lang3.StringUtils;
import java.io.*;
public class FileReader {
private static Logger logger = Logger.getLogger(FileReader.class.getName());
public static String loadFile(String fileName) {
StringBuilder stringBuilder = new StringBuilder();
String line = "";
String ls = System.getProperty("line.separator");
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileName)), getCharsetDecoder()))) {
while ((line = reader.readLine()) != null) {
stringBuilder.append(ls);
}
} catch (IOException e) {
e.printStackTrace();
MiscUtil.exitWithError();
}
logger.info("file " + fileName + " loaded into a memory");
return stringBuilder.toString();
}
public static CharsetDecoder getCharsetDecoder() {
CharsetDecoder decoder = Charset.defaultCharset().newDecoder();
decoder.onMalformedInput(CodingErrorAction.IGNORE);
return decoder;
}
}