Java 文件未找到异常
我正在尝试为扫雷游戏制作一个简单的高分系统。但是,我不断收到文件未找到的异常,并且我也尝试使用该文件的完整路径。
package minesweeper;
import java.io.*;
import java.util.*;
public class Highscore{
public static void submitHighscore(String difficulty) throws IOException{
int easy = 99999;
int normal = 99999;
int hard = 99999;
//int newScore = (int) MinesweeperView.getTime();
int newScore = 10;
File f = new File("Highscores.dat");
if (!f.exists()){
f.createNewFile();
}
Scanner input = new Scanner(f);
PrintStream output = new PrintStream(f);
if (input.hasNextInt()){
easy = input.nextInt();
normal = input.nextInt();
hard = input.nextInt();
}
output.flush();
if(difficulty.equals("easy")){
if (easy > newScore){
easy = newScore;
}
}else if (difficulty.equals("normal")){
if (normal > newScore){
normal = newScore;
}
}else if (difficulty.equals("hard")){
if (hard > newScore){
hard = newScore;
}
}
output.println(easy);
output.println(normal);
output.println(hard);
}
//temporary main method used for debugging
public static void main(String[] args) throws IOException {
submitHighscore("easy");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不会透露异常是在哪一行代码上发出的。 (注意:不发布有关该问题的所有信息会降低您获得有用答案的机会。)
但是,我的预感是它来自如下所示的第二个调用,在这种情况下问题在于尝试打开文件两次:
You don't reveal on which line of code the exception is emitted. (Note: not posting all information you have about the problem reduces your chances of getting useful answers.)
However, my hunch is that it comes from the second call shown below, in which case the problem lies in trying to open the file twice:
您是否检查过该文件是否存在并且您是否具有该文件的访问权限?
Have you checked that the file exists and you have access rights for it?
你试过这个吗?
Have you tried this?