Java 从两个单独的文本文件中读取并写入一个

Java 从两个单独的文本文件中读取并写入一个,java,Java,需要将女孩姓名和男孩姓名的两个文本文件合并为一个文本文件。新文件必须将男孩和女孩的名字分成两个不同性别的列表,我们不知道每个文件会有多少个名字。程序运行时陷入无限循环 import java.io.FileNotFoundException; import java.io.FileReader; import java.io.PrintWriter; import java.util.Scanner; public class NameTester { public static v

需要将女孩姓名和男孩姓名的两个文本文件合并为一个文本文件。新文件必须将男孩和女孩的名字分成两个不同性别的列表,我们不知道每个文件会有多少个名字。程序运行时陷入无限循环

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;

public class NameTester 
{
    public static void main(String args[]) throws FileNotFoundException
    {
        Scanner userIn = new Scanner(System.in);
        System.out.println("Enter file name for boys name file: ");
        String boyFile = userIn.next();
        System.out.println("Enter file name for girls name file: ");
        String girlFile = userIn.next();
        userIn.close();

        Scanner boyIn = new Scanner(new FileReader(boyFile));
        Scanner girlIn = new Scanner(new FileReader(girlFile));

        PrintWriter out = new PrintWriter("names.txt");
        out.print("Boys Names:                  Girls Names: ");
        int count = 1;
        while(boyIn.hasNextLine() || girlIn.hasNextLine());
        {
            String b = boyIn.next();
            String g = girlIn.next();
            out.print(count + " " + b + "                   " + count + " " + g);
            count++;
        }

        boyIn.close();
        girlIn.close();
        out.close();
   }
}

此行是一个将永远运行的空
while
循环:

while(boyIn.hasNextLine() || girlIn.hasNextLine());
去掉结尾的分号:

while(boyIn.hasNextLine() || girlIn.hasNextLine()) // <- NO SEMICOLON
{
    ....
}

while(boyIn.hasNextLine()| | girlIn.hasNextLine())/此行是一个空的
while
循环,将永远运行:

while(boyIn.hasNextLine() || girlIn.hasNextLine());
去掉结尾的分号:

while(boyIn.hasNextLine() || girlIn.hasNextLine()) // <- NO SEMICOLON
{
    ....
}

while(boyIn.hasnetline()| | girlIn.hasnetline())//注意,现在应该使用Java NIO读/写文件,围绕类
Path
路径
文件
。还要注意的是,像
Scanner
这样的资源应该使用try-with-resources进行管理。尝试使用
nextLine()
而不是
next()
。请看这些文件是否保证具有相同的行数?请不要破坏您的帖子。注意,现在您应该使用Java NIO读/写文件,围绕类
路径
路径
文件
旋转。还要注意的是,像
Scanner
这样的资源应该使用try-with-resources进行管理。请尝试使用
nextLine()
而不是
next()
。请查看文件是否保证具有相同的行数?请不要破坏您的帖子。