forked from hellokaton/write-readable-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample2.java
More file actions
36 lines (32 loc) · 971 Bytes
/
Copy pathExample2.java
File metadata and controls
36 lines (32 loc) · 971 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
package chapter_11;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
/**
* 纯工具代码
*
* @author biezhi
* @date 2018/8/7
*/
public class Example2 {
public void foo(String fileName) {
try {
System.out.println(readFileToString(fileName));
// TODO
} catch (Exception e) {
// Exception
}
}
public String readFileToString(String filePath) throws IOException {
StringBuilder currentLines = new StringBuilder();
File file = new File(filePath);
FileReader fr = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fr);
String sCurrentLine;
while ((sCurrentLine = bufferedReader.readLine()) != null) {
currentLines.append(sCurrentLine).append("\n");
}
return currentLines.toString();
}
}