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
|
private
static
String encoding =
"utf-8"
;
public
static
void
readTxt(String filePath)
throws
IOException {
File file =
new
File(filePath);
if
(file.isFile() && file.exists()) {
InputStreamReader read =
new
InputStreamReader(
new
FileInputStream(file), encoding);
BufferedReader bfr =
new
BufferedReader(read);
String lineTxt =
null
;
while
((lineTxt = bfr.readLine()) !=
null
) {
System.out.println(lineTxt);
}
read.close();
bfr.close();
}
}
public
static
String readFile(String path)
throws
IOException {
FileReader fileReader =
new
FileReader(path);
BufferedReader bufferedReader =
new
BufferedReader(fileReader);
String lineTxt =
null
;
StringBuffer sb =
new
StringBuffer();
while
((lineTxt = bufferedReader.readLine()) !=
null
) {
sb.append(lineTxt);
}
bufferedReader.close();
fileReader.close();
return
sb.toString();
}
public
static
void
writeTxt(String path, String data)
throws
IOException {
FileWriter fileWriter =
new
FileWriter(path);
BufferedWriter bufferedWriter =
new
BufferedWriter(fileWriter);
bufferedWriter.write(data);
bufferedWriter.close();
fileWriter.close();
}
|
本文转自Ryan.Miao博客园博客,原文链接:http://www.cnblogs.com/woshimrf/p/5436816.html,如需转载请自行联系原作者