Java中文本文件的读取(按行读取)
在之前的学习过程中,经常会遇到将文本文件中的数据读取到数组或其他数据结构中。每次遇到,总是在网上搜索代码解决,解决之后并没有总结复习,因此在下一次遇到同样的问题时,又重复之前的过程。这样周而复始,并没有将知识积累下来,其实是把自己给坑了(对此深有体会)。因此经过两天的学习,对文件读取这一方面有了一定的了解,写下博客,总结一些东西,以备后续查询。
文本文件读取的大致过程如下:
构建文件对象,
使用文件对象构造Reader对象可以是FileReader、InputStreamReader等
使用Reader对像构建BufferedReader对象(主要使用其readLine()方法,用于按行读取文件)
按行读取文件,将每行获取到的字符串进行处理。
下面给出使用FileReader实现将文本文件读取至一维数组:
public static int[] toArrayByFileReader1(String name) { // 使用ArrayList来存储每行读取到的字符串 ArrayList<String> arrayList = new ArrayList<>(); try { FileReader fr = new FileReader(name); BufferedReader bf = new BufferedReader(fr); String str; // 按行读取字符串 while ((str = bf.readLine()) != null) { arrayList.add(str); } bf.close(); fr.close(); } catch (IOException e) { e.printStackTrace(); } // 对ArrayList中存储的字符串进行处理 int length = arrayList.size(); int[] array = new int[length]; for (int i = 0; i < length; i++) { String s = arrayList.get(i); array[i] = Integer.parseInt(s); } // 返回数组 return array; }
用InputStreamReader方式:
public static int[] toArrayByInputStreamReader1(String name) { // 使用ArrayList来存储每行读取到的字符串 ArrayList<String> arrayList = new ArrayList<>(); try { File file = new File(name); InputStreamReader inputReader = new InputStreamReader(new FileInputStream(file)); BufferedReader bf = new BufferedReader(inputReader); // 按行读取字符串 String str; while ((str = bf.readLine()) != null) { arrayList.add(str); } bf.close(); inputReader.close(); } catch (IOException e) { e.printStackTrace(); } // 对ArrayList中存储的字符串进行处理 int length = arrayList.size(); int[] array = new int[length]; for (int i = 0; i < length; i++) { String s = arrayList.get(i); array[i] = Integer.parseInt(s); } // 返回数组 return array; }
使用RandomAccessFile方式:
public static int[] toArrayByRandomAccessFile(String name) { // 使用ArrayList来存储每行读取到的字符串 ArrayList<String> arrayList = new ArrayList<>(); try { File file = new File(name); RandomAccessFile fileR = new RandomAccessFile(file,"r"); // 按行读取字符串 String str = null; while ((str = fileR.readLine())!= null) { arrayList.add(str); } fileR.close(); } catch (IOException e) { e.printStackTrace(); } // 对ArrayList中存储的字符串进行处理 int length = arrayList.size(); int[] array = new int[length]; for (int i = 0; i < length; i++) { String s = arrayList.get(i); array[i] = Integer.parseInt(s); } // 返回数组 return array; }
以上给出了三种方式实现将文本文件读取至数组(当然文件肯定是有规律的),下面给出将文件读取至二维数组的方法(其实与上面的方法一样,只不过在后面处理ArrayList时采用的方式不一样。)
将文本文件读取至二维数组(以InputStreamReader为例):
public static int[][] toArrayByInputStreamReader2(String name) { // 使用ArrayList来存储每行读取到的字符串 ArrayList<String> arrayList = new ArrayList<>(); try { File file = new File(name); InputStreamReader input = new InputStreamReader(new FileInputStream(file)); BufferedReader bf = new BufferedReader(input); // 按行读取字符串 String str; while ((str = bf.readLine()) != null) { arrayList.add(str); } bf.close(); input.close(); } catch (IOException e) { e.printStackTrace(); } // 对ArrayList中存储的字符串进行处理 int length = arrayList.size(); int width = arrayList.get(0).split(",").length; int array[][] = new int[length][width]; for (int i = 0; i < length; i++) { for (int j = 0; j < width; j++) { String s = arrayList.get(i).split(",")[j]; array[i][j] = Integer.parseInt(s); } } // 返回数组 return array; }
最后附上文本文件的格式,如下图所示
原文:https://blog.csdn.net/milletguo/article/details/80144290
分类:
Java
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?