JAVA while同时遍历两个文件的坑

背景

每一次踩坑,都是一次成长!

有两个文件,第一个文件长这样:

110000,北京市
110101,东城区

第二个文件长这样:

沪L,上海市
京M,北京市

需求:遍历两个文件,第1个文件内的城市名如果在第2个文件里找到,那么在第1个文件内行后添加上车牌号,比如:

110000,北京市,京M

代码使用的是两个while循环,同时遍历每一行,若是城市名一致,则将车牌号加到一个文件行后。直到两个文件遍历完毕。问题是,每一次都只遍历到了第1个文件的第一行。象这种有成千上万行的文件,如果要调试的话,也比较麻烦,问题找到好久都没有找到(基础不行啊!!!)。具体代码省略,可以看下面的示例代码。

发现问题

先创建两个文件one.txt,two.txt
文件内容如下:

A in file1
B in file1
C in file1

α in file2
β in file2
γ in file2
θ in file2

package com.amos.learn;

import java.io.*;
import java.net.URL;
import java.text.MessageFormat;

/**
 * @author chenjun
 */
public class TraverseFile {

    private static File fileInResourceInstance(String fileName) {

        URL url = TraverseFile.class.getClassLoader().getResource(fileName);
        if (null == url) {
            throw new IllegalArgumentException(MessageFormat.format("file-[{0}] not found", fileName));
        }
        return new File(url.getPath());
    }


    private static BufferedReader readerResourcesFile(String fileName) throws FileNotFoundException {
        // 读取源文件
        File file = fileInResourceInstance(fileName);
        BufferedReader reader;
        reader = new BufferedReader(new FileReader(file));
        return reader;
    }
	
	// 重点看这一个方法
    private static void testTwoFileTraverse() throws IOException {

        BufferedReader reader1 = readerResourcesFile("one.txt");
        BufferedReader reader2 = readerResourcesFile("two.txt");

        String tmpStr1;
        String tmpStr2;

        // 遍历第一个文件 一共3行
        while (( tmpStr1 = reader1.readLine()) != null) {
            System.out.println("file1:" + tmpStr1);
            // 遍历第二个文件 一共4行
            while ((tmpStr2 = reader2.readLine()) != null) {
                // 3 * 4 = 12 行
                System.out.println(tmpStr1 + " * " + tmpStr2);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        testTwoFileTraverse();
    }
}

打印结果:

file1:A in file1
A in file1 * α in file2
A in file1 * β in file2
A in file1 * γ in file2
A in file1 * θ in file2
file1:B in file1
file1:C in file1

可以看出来,在第一个文件遍历到第二行的时候,第二个while循环就已经没有再执行了!
因为reader2.readLine()已经遍历完,结果为null了!!

解决问题

修改testTwoFileTraverse方法,读取文件1, while每循环一次都要遍历一次文件2.

private static void testTwoFileTraverse() throws IOException {

        BufferedReader reader1 = readerResourcesFile("one.txt");
        String tmpStr1;
        // 遍历第一个文件 一共3行
        while ((tmpStr1 = reader1.readLine()) != null) {
            System.out.println("file1:" + tmpStr1);

            BufferedReader reader2 = readerResourcesFile("two.txt");
            String tmpStr2;
            // 遍历第二个文件 一共4行
            while ((tmpStr2 = reader2.readLine()) != null) {
                // 3 * 4 = 12 行
                System.out.println(tmpStr1 + " * " + tmpStr2);
            }
        }
    }

打印结果:

file1:A in file1
A in file1 * α in file2
A in file1 * β in file2
A in file1 * γ in file2
A in file1 * θ in file2
file1:B in file1
B in file1 * α in file2
B in file1 * β in file2
B in file1 * γ in file2
B in file1 * θ in file2
file1:C in file1
C in file1 * α in file2
C in file1 * β in file2
C in file1 * γ in file2
C in file1 * θ in file2

问题解决,本章完。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值