-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReadTextFile.java
35 lines (33 loc) · 932 Bytes
/
ReadTextFile.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
package org.xwq;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class ReadTextFile {
public static void main(String[] args) throws UnsupportedEncodingException{
File file = new File("/Users/dou/utf8/1.txt");
StringBuffer contents = new StringBuffer();
BufferedReader reader = null;
try{
reader = new BufferedReader(new FileReader(file));
String text = null;
while((text = reader.readLine()) != null){
contents.append(text).append(System.getProperty("line.separator"));
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if (reader != null)
reader.close();
}catch(IOException e){
e.printStackTrace();
}
}
System.out.println(contents.toString());
}
}