缓冲文件阅读器的问题

时间:2010-09-15 19:26:03

标签: java

我被要求编写一个文件阅读器方法,我已经做了一个工作正常,但无法让第二个工作,并在我打开booms.txt文件后不断收到此错误

  

错误:java.util.NoSuchElementException

公共实例变量

  

公共名单繁荣;

我正在使用的代码用于文件阅读器

  

    {
        int x;
        int y;
        double boomTime;
        boolean isAHit;
        Scanner lineScanner;
        bufferedFileReader = new BufferedReader(new FileReader(aFile));
        String currentLine = bufferedFileReader.readLine();
        while (currentLine != null)
        {
            lineScanner = new Scanner(currentLine);
            lineScanner.useDelimiter(",");
            x = lineScanner.nextInt();
            y = lineScanner.nextInt();
            boomTime = lineScanner.nextDouble();
            isAHit = lineScanner.nextBoolean();
            booms.add(new Boom(x,y,boomTime));
            currentLine = bufferedFileReader.readLine();
        }
    }
    catch (Exception anException)
    {
        System.out.println("Error:"+anException);
    }
    finally
    {
        try
        {
            bufferedFileReader.close();
        }
        catch (Exception anException)
        {
            System.out.println("Error:" +anException);
        }

}

}

请帮助

4 个答案:

答案 0 :(得分:2)

文件末尾可能是一个空行?

答案 1 :(得分:0)

正如maurice所说,文件中可能是空白或不正确的数据行。

{p> Scanner.next...调用抛出异常(尽管您尚未发布堆栈跟踪)。

Scanner.nextInt

  

NoSuchElementException - 如果输入是   用尽

如果您包含堆栈跟踪,这对您和此论坛的成员来说会更加明显。下次尝试将anException.printStackTrace();放入你的catch区块。

答案 2 :(得分:0)

得到它排序我有一个布尔变量,它不在它正在阅读的文件中:),它只是休息一下再看一遍。

答案 3 :(得分:0)

我正在使用另一种并不复杂的方法。这样做:

Scanner diskScanner = new Scanner(new File("pathname"));

例如从类型

中读取指定文件中的Integer
int blahblahblah = diskScanner.nextInt();