扫描仪没有发现线路异常 [英] Scanner No Line Found Exception

查看:129
本文介绍了扫描仪没有发现线路异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下异常。

java.util.NoSuchElementException:找不到行

java.util.NoSuchElementException: No line found

I在编写需要从文本文件中读取的较大程序时出现此错误,因此决定进行测试。

I got this error while writing a larger program which needed to read from a text file, and so decided to do a test.

Scanner scan = new Scanner(new File("restrictions.txt");
String s1 = scan.nextLine();
System.out.println(s1);

我仍然得到异常。我在一个名为restrictions.txt的类的文件夹中有一个文本文件,其中包含文本。我做错了吗?

And I still get the exception. I have a text file in the same folder as the class called restrictions.txt which has text in it. What am I doing wrong?

推荐答案

新文件(restrictions.txt)将在Start dir中查找该文件您的应用程序 - 如果您正在使用Eclipse,它可能是您项目的根目录。

new File("restrictions.txt") will look for the file in the "Start dir" of your app - if you're using Eclipse, it's probably the root of your project.

要打开您的类旁边的文件,您可以使用Scanner构造函数接受你得到的InputStream

To open the file next to your class, you can use the Scanner constructor which accepts an InputStream that you get by

YourClass.class.getResourceAsStream("restrictions.txt")

这篇关于扫描仪没有发现线路异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆