package com.study.Day7_27.IO;
//1. 导入包
import java.io.*;
public class ReadFile3 {
public static void main(String[] args) {
//第1步:声明文件输入流对象
BufferedReader reader = null;
try {
//构建文件输入流对象
File fis = new File("data/1.txt");
//InputStreamReader 是从字节流到字符流的桥梁 转换流
reader = new BufferedReader(new InputStreamReader(new FileInputStream(fis)),8192);
//2. 读取指定文件中的内容
//创建字符串缓冲区
//创建数据缓冲区
StringBuffer sb = new StringBuffer();
String line = "";
//使用循环读取文本文件中的内容 -1表示读取文件的末尾,结束标记
while ((line = reader.readLine()) != null){
sb.append(line);
sb.append("\r\n");
}
System.out.println(""+sb);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 第3步:释放资源(关闭字节输入流对象)
if(reader != null ) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
java读取文件,使用BufferedReader输入流对象读取文件内容
最新推荐文章于 2024-05-03 17:32:28 发布