FileNotFoundException Java

FileNotFoundException Java,java,filenotfoundexception,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 norm

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

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

}

您是否已检查该文件是否存在,以及您是否拥有该文件的访问权限?

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

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

Scanner input = new Scanner(f); 
PrintStream output = new PrintStream(f);
你试过这个吗

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

stacktrace不是一个坏主意。两者都返回true。表面上看,只是从文件中读取不起作用,但在调试时,它显示了扫描仪和printstream的filenotfoundexception,这让我很困惑。谢谢你的回答,但是我有什么办法可以让阅读文件有效吗?看看。点击链接。还有:可能就是这样,谢谢!所以在打开prinstream之前,我必须以某种方式关闭扫描仪?@Troels,没错。关闭很容易-看到:-)这就解决了:)非常感谢!