读取txt文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class ReadFileByLines {
public static void main(String[] args) {
try {
//1.打开一个file
File file = new File("E:/test.txt");
//2.InputStreamReader<-FileInputStream<-file
FileInputStream fis = new FileInputStream(file);
InputStreamReader is = new InputStreamReader(fis);
//3.用BufferedReader(<-InputStreamReader)的readLine()方法读取
BufferedReader br = new BufferedReader(is);

//4.输出
String txtLine = null;
while ((txtLine = br.readLine()) != null) {
System.out.println(txtLine);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class ReadFileByLines {
public static void main(String[] args) {
try {

File file = new File("E:/test.txt");

BufferedReader br = new BufferedReader(new FileReader(file));

String txtLine = null;
while ((txtLine = br.readLine()) != null) {
System.out.println(txtLine);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

评论