ed *_*ale 1 java file java.util.scanner
我是一个Java初学者,并已阅读类似的问题,但我仍然不知道为什么我的代码显示FileNotFound异常.我的文件在同一目录中.
我的代码是:
import java.io.*;
import java.util.Scanner;
public class reader {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x = in.nextInt();
double y = in.nextDouble();
float g = in.nextFloat();
String a = in.next();
File file = new File("v.txt");
System.out.println(x + "" + y + "" + g + "" + a);
Scanner inFile = new Scanner(new FileReader(file));
String u = inFile.nextLine();
System.out.println(file.getAbsolutePath());
System.out.println(u);
}
}
Run Code Online (Sandbox Code Playgroud)
错误是:
17: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
Scanner inFile = new Scanner(new FileReader(file));
^
1 error
Run Code Online (Sandbox Code Playgroud)
您遇到编译时错误:
error: unreported exception FileNotFoundException; must be caught or declared to be thrown
Scanner inFile = new Scanner(new FileReader(file));
Run Code Online (Sandbox Code Playgroud)
这是一种修复它的简单方法:
public class reader {
public static void main(String[] args) throws Exception {
//...
}
}
Run Code Online (Sandbox Code Playgroud)
虽然使用try {...} catch(...){}是处理可能的运行时异常的更好方法.
| 归档时间: |
|
| 查看次数: |
903 次 |
| 最近记录: |