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

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

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

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

下面给出使用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;
	}

最后附上文本文件的格式,如下图所示

            

  • 34
    点赞
  • 199
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值