Java中文本文件的读取(按行读取)

您所在的位置:网站首页 java字符流读一行 Java中文本文件的读取(按行读取)

Java中文本文件的读取(按行读取)

2024-04-16 08:15| 来源: 网络整理| 查看: 265

在之前的学习过程中,经常会遇到将文本文件中的数据读取到数组或其他数据结构中。每次遇到,总是在网上搜索代码解决,解决之后并没有总结复习,因此在下一次遇到同样的问题时,又重复之前的过程。这样周而复始,并没有将知识积累下来,其实是把自己给坑了(对此深有体会)。因此经过两天的学习,对文件读取这一方面有了一定的了解,写下博客,总结一些东西,以备后续查询。

文本文件读取的大致过程如下:

    构建文件对象,    使用文件对象构造Reader对象可以是FileReader、InputStreamReader等    使用Reader对像构建BufferedReader对象(主要使用其readLine()方法,用于按行读取文件)    按行读取文件,将每行获取到的字符串进行处理。

下面给出使用FileReader实现将文本文件读取至一维数组:

public static int[] toArrayByFileReader1(String name) { // 使用ArrayList来存储每行读取到的字符串 ArrayList 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 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 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 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



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3