我正在试着为扫雷游戏做一个简单的高分系统。然而,我一直收到文件未找到异常,并且我也尝试使用文件的完整路径。
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");
}
}发布于 2011-01-14 17:43:47
您不会透露异常是在哪一行代码上发出的。(注意:没有发布所有关于这个问题的信息会降低你获得有用答案的机会。)
然而,我的直觉是它来自下面显示的第二次调用,在这种情况下,问题出在试图打开文件两次:
Scanner input = new Scanner(f);
PrintStream output = new PrintStream(f);发布于 2011-01-14 17:42:24
您是否检查了该文件是否存在,并且您是否具有该文件的访问权限?
发布于 2011-01-14 17:45:40
你试过这个吗?
if(f.isFile())
System.out.println("Yes, we have a file");
if(f.canWrite())
System.out.println("Yes, we have can write to the file");https://stackoverflow.com/questions/4689681
复制相似问题