简体   繁体   English

为什么Scanner的hasNext()方法最初为非空文本文件返回false?

[英]why is Scanner's hasNext() method initially returning false for a non-empty text file?

I'm trying to write some text to a file. 我正在尝试将一些文本写入文件。 I have a while loop that is supposed to just take some text and write the exact same text back to the file. 我有一个while循环,应该只接收一些文本并将完全相同的文本写回到文件中。

I discovered that the while loop is never entered because Scanner thinks there's no more text to read. 我发现从来没有进入while循环,因为Scanner认为没有更多的文本可供阅读。 But there is. 但是还有。

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

public class WriteToFile {

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

        String whatToWrite = "";

        File theFile = new File("C:\\test.txt");

        Scanner readinput = new Scanner(theFile);

        PrintWriter output = new PrintWriter(theFile);

        while (readinput.hasNext()) { //why is this false initially?

            String whatToRead = readinput.next();

            whatToWrite = whatToRead;

            output.print(whatToWrite);
        }

        readinput.close();
        output.close();

    }

}

The text file just contains random words. 文本文件仅包含随机单词。 Dog, cat, etc. 狗,猫等

When I run the code, text.txt becomes empty. 当我运行代码时,text.txt变为空。

There was a similar question: https://stackoverflow.com/questions/8495850/scanner-hasnext-returns-false which pointed to encoding issues. 还有一个类似的问题: https : //stackoverflow.com/questions/8495850/scanner-hasnext-returns-false ,它指向编码问题。 I use Windows 7 and US language. 我使用Windows 7和美国语言。 Can I find out how the text file is encoded somehow? 我可以找出文本文件的编码方式吗?

Update: 更新:

Indeed, as Ph.Voronov commented, the PrintWriter line erases the file contents! 确实,正如Ph.Voronov所说,PrintWriter行会擦除文件内容! user2115021 is right, if you use PrintWriter you should not work on one file. user2115021是正确的,如果使用PrintWriter,则不应处理一个文件。 Unfortunately, for the assignment I had to solve, I had to work with a single file. 不幸的是,对于我必须解决的任务,我只能使用一个文件。 Here's what I did: 这是我所做的:

import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class WriteToFile {

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

        ArrayList<String> theWords = new ArrayList<String>();

        File theFile = new File("C:\\test.txt");

        Scanner readinput = new Scanner(theFile);

        while (readinput.hasNext()) {

            theWords.add(readinput.next());

        }

        readinput.close();

        PrintWriter output = new PrintWriter(theFile); //we already got all of
            //the file content, so it's safe to erase it now

        for (int a = 0; a < theWords.size(); a++) {
            output.print(theWords.get(a));
            if (a != theWords.size() - 1) {
                output.print(" ");
            }
        }

        output.close();

    }

}
PrintWriter output = new PrintWriter(theFile);

它会删除您的文件。

您正在尝试使用SCANNER读取文件并使用PRINTWRITER写入另一个文件,但是两者都在同一个文件上。PRINTWRITER清除文件的内容以写入内容。两个类都需要在不同的文件上工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM