博客
关于我
Java基础学习总结(62)——Java中的流和Socket
阅读量:806 次
发布时间:2023-01-28

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

读取文本文件的步骤

文件处理步骤

FileInputStream fis = new FileInputStream("file.txt");        BufferedReader reader = new BufferedReader(new InputStreamReader(fis, StandardCharsets.UTF_8));        while(true) {            String line = reader.readLine();            if(line == null) break;            System.out.println(line);        }        reader.close();        fis.close();      

参数说明

文件路径为"file.txt",传输编码方式为UTF-8。本文将介绍如何通过Java读取文本文件中的内容。

异常处理

try {          FileInputStream fis = new FileInputStream("file.txt");          BufferedReader reader = new BufferedReader(new InputStreamReader(fis, StandardCharsets.UTF_8));          while(true) {              String line = reader.readLine();              if(line == null) break;              System.out.println(line);          }      } catch(FileNotFoundException e){          System.out.println("文件未找到:" + e.getMessage());      } catch(IOException e){          System.out.println("读取失败:" + e.getMessage());      } finally{          System.out.println("已关闭文件流");          reader.close();          fis.close();      }

转载地址:http://zvryk.baihongyu.com/

你可能感兴趣的文章
Python变量与运算符
查看>>
Python变量/运算符/函数/模块/string
查看>>
python发送邮件的时候出现 error (535, b‘5.7.3 Authentication unsuccessful‘) 解决方法
查看>>
python发送notes邮件_使用python在Lotus Notes中发送邮件
查看>>
Python双版本下创建一个Scrapy(西瓜皮)项目
查看>>
Python双版本下No module named 'requests'
查看>>
python及pycharm2018软件安装教程
查看>>
python去重txt文本_Python实现的txt文件去重功能示例
查看>>
python去掉列表的逗号,从Python列表项中删除标点符号
查看>>
Python卸载所有包
查看>>
python单线程下实现多个socket并发
查看>>
Python单元测试框架介绍(超详细~)
查看>>
Python单元测试框架
查看>>
python单元测试之unittest
查看>>
Python单元测试unittest实战
查看>>
Python单元测试-模拟VS补丁
查看>>
python协程实时输出_Python 协程以及事件循环的问题,怎么知道 IO 读取结束了呢?...
查看>>
Python十进制&;包提供错误的结果
查看>>
Python化身“安全老中医”:一键搞定工控系统漏洞,从零基础到精通,收藏这篇就够了!
查看>>
Python加速运行技巧
查看>>