如何在Java中检测EOF?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36515937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:57:24  来源:igfitidea点击:

How to detect EOF in Java?

javaeof

提问by Bertha Alan

I've tried some ways to detect EOF in my code, but it still not working. I've tried using BufferedReader, Scanner, and using char u001a to flag the EOF, but still not make any sense to my code. Here is my last code :

我尝试了一些方法来检测代码中的 EOF,但它仍然无法正常工作。我已经尝试使用 BufferedReader、Scanner 和使用 char u001a 来标记 EOF,但对我的代码仍然没有任何意义。这是我的最后一个代码:

    Scanner n=new Scanner(System.in);
    String input;
    int counter=0;

    while(n.hasNextLine())
    {
        input=n.nextLine();
        char[] charInput=input.toCharArray();
        for (int i = 0; i < input.length(); i++) {
            if(charInput[i]=='"')
            {
                if(counter%2==0)
                {
                    System.out.print("``");
                }
                else
                {
                    System.out.print("''");
                }
                counter++;
            }
            else
            {
                System.out.print(charInput[i]);
            } 
        }
        System.out.print("\n");
    }

The program supposed to stopped when it's already reached the EOF, but I don't know why, for some reasons it keeps running and result a runtime error. Please help. By the way I'm new here, sorry if my question is not really clear to be understood, Thank you before :)

程序应该在到达 EOF 时停止,但我不知道为什么,由于某些原因它继续运行并导致运行时错误。请帮忙。顺便说一句,我是新来的,如果我的问题不是很清楚,很抱歉,谢谢您:)

采纳答案by user207421

It keeps running because it hasn't encountered EOF. At end of stream:

它一直在运行,因为它没有遇到 EOF。在流结束时:

  1. read()returns -1.
  2. read(byte[])returns -1.
  3. read(byte[], int, int)returns -1.
  4. readLine()returns null.
  5. readXXX()for any other X throws EOFException.
  6. Scanner.hasNextXXX()returns false for any X.
  7. Scanner.nextXXX()throws NoSuchElementExceptionfor any X.
  1. read()返回 -1。
  2. read(byte[])返回 -1。
  3. read(byte[], int, int)返回 -1。
  4. readLine()返回空值。
  5. readXXX()对于任何其他 X 投掷EOFException
  6. Scanner.hasNextXXX()对任何 X 返回 false。
  7. Scanner.nextXXX()投掷NoSuchElementException任何 X。

Unless you've encountered one of these, your program hasn't encountered end of stream. NB \u001ais a Ctrl/z. Not EOF. EOF is not a character value.

除非您遇到过其中之一,否则您的程序还没有遇到流结束。NB\u001a是 Ctrl/z。不是EOF。EOF 不是字符值。

回答by Srijan Goyal

This is what I did

这就是我所做的

Scanner s = new Scanner(f); //f is the file object

while(s.hasNext())
{
String ss = s.nextLine();
System.out.println(ss);
}

Worked for me

为我工作

回答by Sadi Hurayv

You can use try and catch.

您可以使用try 和 catch

Scanner n=new Scanner(System.in);
String input;
int counter=0;
input=n.nextLine();
try{
    while(input!=null)
    { 
        char[] charInput=input.toCharArray();
        for (int i = 0; i < input.length(); i++) {
            if(charInput[i]=='"')
            {
                if(counter%2==0)
                {
                    System.out.print("``");
                }
                else
                {
                    System.out.print("''");
                }
                counter++;
            }
            else
            {
                System.out.print(charInput[i]);
            } 
        }
        System.out.print("\n");
        input=n.nextLine();
    }
}catch(Exception e){

}

Here, when you will give Ctrl+z, Scanner.nextLine()will give you NoSuchElementException. Use this exception as a condition of EOF. To handle this exception use try and catch.

在这里,当你给Ctrl+z 时Scanner.nextLine()会给你NoSuchElementException. 将此异常用作 EOF 的条件。要处理此异常,请使用 try 和 catch。