博客
关于我
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 virtualenv
查看>>
python vue3实现大文件分段续传(断点续传)--带暂停和继续功能
查看>>
Python WebDriver如何打印整个页面源(html)
查看>>
Python WebSocket自动化测试:构建高效接口测试框架
查看>>
Python Web开发
查看>>
Redis 配置文件杂项。
查看>>
Python web自动化测试 —— 文件上传
查看>>
Python web自动化测试 —— 文件上传!
查看>>
Python Web自动化测试开发环境搭建(附安装包与虚拟机环境)
查看>>
python win32api键盘_Python win32api.keybd_event模拟键盘输入
查看>>
python Windows显示当前鼠标坐标
查看>>
python Workbook 表格宽度
查看>>
python | ggplot,一个超强的 Python 库!
查看>>
python | unoconv,一个超厉害的 Python 库!
查看>>
python | webassets,一个超强的 Python 库!
查看>>
python | werkzeug,一个不可思议的 Python 库!
查看>>
python | xlsxwriter,一个实用的 Python 库!
查看>>
python | xlwings,一个非常实用的 Excel 相关的 Python 库!
查看>>
python读取含中文的json
查看>>
python | 提升代码迭代速度的Python重载方法
查看>>