FileNotFoundException Java

时间:2011-01-14 09:38:39

标签: java filenotfoundexception

我正在尝试为扫雷游戏制作一个简单的高分系统。但是,我一直得到一个文件未找到异常,我已经尝试使用该文件的完整路径。

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

}

3 个答案:

答案 0 :(得分:1)

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

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

Scanner input = new Scanner(f); 
PrintStream output = new PrintStream(f);

答案 1 :(得分:0)

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

答案 2 :(得分: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");