Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 合并两个文本文件并生成带有匹配项的单独文本文件_Java_Filereader_Filewriter - Fatal编程技术网

Java 合并两个文本文件并生成带有匹配项的单独文本文件

Java 合并两个文本文件并生成带有匹配项的单独文本文件,java,filereader,filewriter,Java,Filereader,Filewriter,我对java I/O类非常陌生,发现很难检测代码中的错误。 我想输入两个包含不同数字列表的文本文件的路径,以便程序逐行比较这两个列表,并在输入文本文件中生成一个带有常用数字的单独文本文件。 输入文件被带到程序中,但它不会从那里继续 提前谢谢 公共类列表匹配{ public static String path1,path2; //paths of the input text files public static String line1,line2 = null; //indivdual l

我对java I/O类非常陌生,发现很难检测代码中的错误。 我想输入两个包含不同数字列表的文本文件的路径,以便程序逐行比较这两个列表,并在输入文本文件中生成一个带有常用数字的单独文本文件。 输入文件被带到程序中,但它不会从那里继续

提前谢谢

公共类列表匹配{

public static String path1,path2; //paths of the input text files
public static String line1,line2 = null; //indivdual lines extracted from the files

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

    Scanner path = new Scanner(System.in);
    path1 = path.nextLine();
    path2 = path.nextLine();

    fileRead();
}


public static void fileRead () throws IOException {

    FileReader file1 = new FileReader(new File(path1));
    FileReader file2 = new FileReader(new File(path2));

    BufferedReader br1 = new BufferedReader(file1);
    BufferedReader br2 = new BufferedReader(file2);

    while ((line1 = br1.readLine())!=null){
       while((line2 = br2.readLine())!=null){

                    }
                    }
} }





 public static void writeFile() throws IOException{
    Scanner path = new Scanner(System.in);
    String outPath = path.nextLine();
    FileWriter fr = new FileWriter(new File(outPath));
    BufferedWriter br = new BufferedWriter (fr);

    br.append(line2);



 }

}你需要这样的东西:

    public class MyClass {
    private String path1;
    private String path2;
    private String resultFilePath;

    public void doCompare() throws Exception {
        BufferedReader reader1 = new BufferedReader(new InputStreamReader(new FileInputStream(path1)));
        BufferedReader reader2 = new BufferedReader(new InputStreamReader(new FileInputStream(path2)));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(resultFilePath)));

        String line1 = null;
        String line2 = null;

        while ((line1 = reader1.readLine()) != null && (line2 = reader2.readLine()) != null)
            if (line1.trim().equals(line2.trim()))
                writer.append(line1).append("\r\n");
        writer.flush();
        writer.close();
        reader1.close();
        reader2.close();
    }
}

你在while循环中没有做任何事情,所以在你的循环中,你要从br2读一行,然后从br2读所有的行-这是你想要的吗?@user2310289-是的。。我想他是在比较b1的每一行和b2的所有行。但是效率很差-n*n..是的,我计划将一个文件的一行与另一个文件的所有行进行比较,并在找到匹配项时终止循环