java Scanner.hasNext始终为false

java Scanner.hasNext始终为false,java,java.util.scanner,Java,Java.util.scanner,在我的代码方法中,scanner.hasNext()总是给出错误的结果。调试代码时我理解了这一点。有人知道有什么问题吗?我检查了文件。文件以字符串开头 public class TextFileImplementor { public static void main (String [] args){ System.out.println("Enter the name of file\n"); Scanner scanner = new Scanner

在我的代码方法中,scanner.hasNext()总是给出错误的结果。调试代码时我理解了这一点。有人知道有什么问题吗?我检查了文件。文件以字符串开头

public class TextFileImplementor {
    public static void main (String [] args){
        System.out.println("Enter the name of file\n");
        Scanner scanner = new Scanner(System.in);
        File file = new File(scanner.nextLine());
        try {
            scanner = new Scanner(file);
            StringBuilder stringBuilder = new StringBuilder();
            while (scanner.hasNext()){
                stringBuilder.append(scanner.nextLine());
            }
            String [] strings = stringBuilder.toString().split("\\. ");
            File newFile = new File("new_" + file.getName());
            FileWriter fileWriter = new FileWriter(newFile, true);
            for (String string :
                    strings) {
                if (string.length()<141){
                    fileWriter.write(string + "\n");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
公共类TextFileImplementor{
公共静态void main(字符串[]args){
System.out.println(“输入文件名\n”);
扫描仪=新的扫描仪(System.in);
File File=新文件(scanner.nextLine());
试一试{
扫描仪=新扫描仪(文件);
StringBuilder StringBuilder=新的StringBuilder();
while(scanner.hasNext()){
追加(scanner.nextLine());
}
String[]strings=stringBuilder.toString().split(“\\”);
File newFile=new文件(“new_quot+File.getName());
FileWriter FileWriter=newfilewriter(newFile,true);
对于(字符串:
(字符串){

如果(string.length()问题解决了!我对输入文件的编码有问题。我必须添加

fileWriter.flush();
fileWriter.close();

文件可能是空的?如果要调用
scanner.nextLine()
,请使用
scanner.hasNextLine()
。但可能不是问题所在。@AndyTurner已更改为scanner.hasNextLine()。问题没有解决。当我调试代码时,它可以很好地读取文件中的行。再看一看输出代码和/或输入文件的内容。@azurefrog,现在我尝试使用不同的文件,有时也尝试使用它的工作。我想这是编码方面的问题,我会尝试解决它。但是数据不会写入输出文件。我会我不明白为什么。