java读文件几种方式_JAVA读取文件的几种方式
发布日期:2021-06-24 07:34:10 浏览次数:5 分类:技术文章

本文共 1092 字,大约阅读时间需要 3 分钟。

{

File file1 = new File("/Users/tao/Downloads/酒店id.txt"); // 创建File类对象

FileInputStream fis = null; // 创建FileInputStream类对象读取File

InputStreamReader isr = null; // 创建InputStreamReader对象接收文件流

BufferedReader br = null; // 创建reader缓冲区将文件流装进去

try {

// fis = new FileInputStream(file1);

// isr = new InputStreamReader(fis);

// br = new BufferedReader(isr);

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

//前三行和这一行等同,FileReader()继承自InputStreamReader

String lineTxt = null;

// 从缓冲区中逐行读取代码,调用readLine()方法

while ((lineTxt = br.readLine()) != null) {

System.out.println(lineTxt); // 逐行输出文件内容

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

// 关闭数据流

if (br != null) {

try {

br.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (isr != null) {

try {

isr.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (fis != null) {

try {

fis.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

标签:fis,JAVA,读取,几种,printStackTrace,br,catch,new,null

来源: https://blog.csdn.net/qq_38484961/article/details/101198514

转载地址:https://blog.csdn.net/weixin_31163527/article/details/114510456 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java arraylist常用方法_C#中数组、ArrayList、List、Dictionary的用法与区别浅析(存取数据)...
下一篇:java泛型改进_编写高质量代码:改善Java程序的151个建议(第7章:泛型和反射___建议93~97)...

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年04月18日 07时06分51秒