0

我正在尝试为扫雷游戏制作一个简单的高分系统。但是,我不断收到未找到文件的异常,并且我也尝试使用该文件的完整路径。

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");
 }  

}
4

3 回答 3

1

您不会透露在哪一行代码上发出了异常。(注意:不发布有关问题的所有信息会降低您获得有用答案的机会。)

但是,我的预感是它来自下面显示的第二个调用,在这种情况下,问题在于尝试打开文件两次:

Scanner input = new Scanner(f); 
PrintStream output = new PrintStream(f);
于 2011-01-14T09:43:46.990 回答
0

您是否检查过该文件是否存在并且您有访问权限?

于 2011-01-14T09:42:23.807 回答
0

你试过这个吗?

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");
于 2011-01-14T09:45:40.070 回答