需要帮助搜索三个文件并打印出相同的公共行

时间:2018-04-28 05:12:05

标签: java

现在,我正在搜索第一个,然后将其发送到第二个,但第二个只打印并比较第一行。

我不知道如何让第二种方法从下一行开始,依此类推。目标是使用三个文本文件执行此操作,但我甚至无法完成第一个文本文件。它必须使用bufferedreader和while循环。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;


public class scanner {

    public static String line;
    public static String line2;
    public static String line3;
    public static boolean match = false;


    public static void main (String [] args) throws IOException
    {
        BufferedReader in = new BufferedReader(new FileReader("creditCards1.txt"));
        line = in.readLine();


        while (match==false && line != null) 
        {   
                System.out.println(line);
                line = in.readLine();   
                scan2(line);
        }    

        in.close();

    }

    public static boolean scan2(String line) throws IOException
    {
        BufferedReader in2 = new BufferedReader(new FileReader("creditCards2.txt"));
        if (line2 == null || line2 == "7120-0824-9323-2825")
        {
            line2 = in2.readLine();
        }

        while(match==false && line!=null)
        {
            System.out.println(line2);
            if(line2 == line)
            {
                match = true;
                System.out.println("sdsds" + line);
                in2.close();
                break;
            }

            line2= in2.readLine();
            break;

        }


        return match;


    }
}

1 个答案:

答案 0 :(得分:-2)

package so;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Test {

    public static void main(String[] args) {

        File f1 = new File("/home/guest/Desktop/file1.txt");
        File f2 = new File("/home/guest/Desktop/file2.txt");
        File f3 = new File("/home/guest/Desktop/file3.txt");

        try {
            compareTwo(f1, f2);
            compareTwo(f1, f3);
            compareTwo(f2, f3);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void compareTwo(File f1, File f2) throws IOException {
        String l1,l2;
        try (BufferedReader r1 = new BufferedReader(new  FileReader(f1))) {
            while ((l1 = r1.readLine()) != null) {
                try (BufferedReader r2 = new BufferedReader(new  FileReader(f2))) {
                    while ((l2 = r2.readLine()) != null) {
                        if(l1.equals(l2))
                            System.out.println("line: " + l1 + " in file " + f1 + " exists in file " + f2);
                    }
                }
            }
        }  
    }
}