首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FileNotFoundException Java

FileNotFoundException Java
EN

Stack Overflow用户
提问于 2011-01-14 17:38:39
回答 3查看 7.5K关注 0票数 0

我正在试着为扫雷游戏做一个简单的高分系统。然而,我一直收到文件未找到异常,并且我也尝试使用文件的完整路径。

代码语言:javascript
复制
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");
 }  

}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-01-14 17:43:47

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

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

代码语言:javascript
复制
Scanner input = new Scanner(f); 
PrintStream output = new PrintStream(f);
票数 1
EN

Stack Overflow用户

发布于 2011-01-14 17:42:24

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

票数 0
EN

Stack Overflow用户

发布于 2011-01-14 17:45:40

你试过这个吗?

代码语言:javascript
复制
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");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4689681

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档