Java 文件未找到异常

发布于 2024-10-11 16:30:49 字数 1189 浏览 6 评论 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");
 }  

}

I'm trying to make a simple highscore system for a minesweeper game. However i keep getting a file not found exception, and i've tried to use the full path for the file aswell.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生活了然无味 2024-10-18 16:30:49

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

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

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

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:

Scanner input = new Scanner(f); 
PrintStream output = new PrintStream(f);
最偏执的依靠 2024-10-18 16:30:49

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

Have you checked that the file exists and you have access rights for it?

人生百味 2024-10-18 16:30:49

你试过这个吗?

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

Have you tried this?

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文