I'm trying to write a parser to a file called "x". I want to use scanner. I tryied to follow actions from a tutorial: https://www.youtube.com/watch?v=3RNYUKxAgmw.
我正在尝试将解析器写入名为“x”的文件。我想用扫描仪。我尝试按照教程中的操作进行操作:https://www.youtube.com/watch?v = 3RNYUKxAgmw。
package q;
import java.io.File;
import java.util.Scanner;
public class Parser {
public static void main(String [] args) {
Scanner x = new Scanner(new File("/home/x/eclipse-workspace/q/src/q/x.txt"));
String s=x.nextLine();
System.out.print(s);
}
}
The file that I want to open is called "x", its text file. We can see it in Package Explorer on left side. I clicked right on its properties. There is visible file locatization.
我要打开的文件名为“x”,即其文本文件。我们可以在左侧的Package Explorer中看到它。我点击了它的属性。有可见的文件定位。
There appears FileNotFoundException as on the picture. I doesn't understand why this file cannot be opened.
在图片上出现FileNotFoundException。我不明白为什么这个文件无法打开。
[update] But I'm not sure if this is what
[更新]但我不确定这是不是
1 个解决方案
#1
1
There appears FileNotFoundException as on the picture. I doesn't understand why this file cannot be opened.
在图片上出现FileNotFoundException。我不明白为什么这个文件无法打开。
That's not what's happening. The error is in compilation time (the program has not executed, it doesn't know if the file -will- exist). The compiler is telling you "this method/constructor, according to its declaration, can throw an Exception (in this case: a FileNotFoundException ) at run time; you have not told me what to do in that case".
那不是正在发生的事情。错误是在编译时(程序没有执行,它不知道文件是否存在)。编译器告诉你“这个方法/构造函数,根据它的声明,可以在运行时抛出异常(在这种情况下:一个FileNotFoundException);你没有告诉我在这种情况下该做什么”。
You really need to read about how Exceptions are treated in Java.
您真的需要阅读有关如何在Java中处理异常的信息。
For a quick remedy, add a throws Exception
to your main
declaration. (Bear in mind: that is an awful thing to do if you don't really understand what are you doing)
要快速解决问题,请在主声明中添加一个抛出异常。 (请记住:如果你真的不明白你在做什么,这是一件非常糟糕的事情)
#1
1
There appears FileNotFoundException as on the picture. I doesn't understand why this file cannot be opened.
在图片上出现FileNotFoundException。我不明白为什么这个文件无法打开。
That's not what's happening. The error is in compilation time (the program has not executed, it doesn't know if the file -will- exist). The compiler is telling you "this method/constructor, according to its declaration, can throw an Exception (in this case: a FileNotFoundException ) at run time; you have not told me what to do in that case".
那不是正在发生的事情。错误是在编译时(程序没有执行,它不知道文件是否存在)。编译器告诉你“这个方法/构造函数,根据它的声明,可以在运行时抛出异常(在这种情况下:一个FileNotFoundException);你没有告诉我在这种情况下该做什么”。
You really need to read about how Exceptions are treated in Java.
您真的需要阅读有关如何在Java中处理异常的信息。
For a quick remedy, add a throws Exception
to your main
declaration. (Bear in mind: that is an awful thing to do if you don't really understand what are you doing)
要快速解决问题,请在主声明中添加一个抛出异常。 (请记住:如果你真的不明白你在做什么,这是一件非常糟糕的事情)