Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.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 使用scanner连接文件_Java_File Io - Fatal编程技术网

Java 使用scanner连接文件

Java 使用scanner连接文件,java,file-io,Java,File Io,我有以下两个文件: 1.txt first|second|third fourth|fifth|sixth 2.txt first1|second1|third1 fourth1|fifth1|sixth1 现在我想加入他们两个: first|first1|second1|third1|second|third fourth|fourth1|fifth1|sixth1|fifth|sixth 我正在尝试使用扫描仪,但无法加入他们。任何建议 Scanner scanner = new Sca

我有以下两个文件:

1.txt
first|second|third
fourth|fifth|sixth

2.txt
first1|second1|third1
fourth1|fifth1|sixth1
现在我想加入他们两个:

first|first1|second1|third1|second|third
fourth|fourth1|fifth1|sixth1|fifth|sixth
我正在尝试使用扫描仪,但无法加入他们。任何建议

Scanner scanner = new Scanner(new File(("F:\\1.txt")));
Scanner scanner2 = new Scanner(new File(("F:\\2.txt")));

while(scanner.hasNext()) {
   while(scanner2.hasNext()) {
   system.out.println(scanner.next() + "|" + scanner2.next() + "|");
}
//输出

first|second|third|first1|second1|third1|
fourth|fifth|sixth|fourth1|fifth1|sixth1|


你能解释一下你得到的错误结果是什么吗?请查看我的编辑。
    Scanner scanner = new Scanner(new File(("F:\\1.txt")));
    Scanner scanner2 = new Scanner(new File(("F:\\2.txt")));
    String[] line1, line2, res;
            while (scanner.hasNext() && scanner2.hasNext()) {
                line1 = scanner.next().split("\\|");
                line2 = scanner2.next().split("\\|");
                int len = Math.min(line1.length,line2.length);
                res= new String[line1.length + line2.length];
                for(int index = 0, counter = 0; index < len; index++){
                    res[counter++] = line1[index];
                    res[counter++] = line2[index];
                }
                if(line1.length > line2.length){
                    for(int jIndex = 2*line2.length, counter = 0;jIndex < (line1.length+line2.length);jIndex++  ){
                        res[jIndex] = line1[line2.length + (counter++)];
                    }
                }else if(line2.length > line1.length){
                    for(int jIndex = 2*line1.length, counter = 0;jIndex < (line1.length+line2.length);jIndex++  ){
                        res[jIndex] = line2[line1.length + (counter++)];
                    }
                }
                String result = Arrays.asList(res).toString().replaceAll("(^\\[|\\]$)", "").replace(", ", "|");
                System.out.println(result);
            }
            scanner.close();
            scanner2.close();
first|first1|second|second1|third|third1
fourth|fourth1|fifth|fifth1|sixth|sixth1
String[] line1, line2, res;
            while (scanner.hasNext() && scanner2.hasNext()) {
                line1 = scanner.next().split("\\|");
                line2 = scanner2.next().split("\\|");
                res= new String[line1.length + line2.length];
                int counter = 0;
                res[counter++] = line1[0];
                for(int index = 0; index < line2.length; index++){
                    res[counter++] = line2[index];
                }
                for(int index = 1; index < line1.length; index++){
                    res[counter++] = line1[index];
                }
                String result = Arrays.asList(res).toString().replaceAll("(^\\[|\\]$)", "").replace(", ", "|");
                System.out.println(result);
            }
            scanner.close();
            scanner2.close();
first|first1|second1|third1|second|third
fourth|fourth1|fifth1|sixth1|fifth|sixth