2014-09-05 59 views
0

自己製作一個小遊戲作爲一個小項目,我不太擅長錯誤處理等類似問題,所以我得到一個java.io.FileNotFoundException錯誤,我不確定如何進行。獲取Java.io.FileNotFoundException;在一個程序中,不知道如何修復

這是我在這裏的第一篇文章,所以我很抱歉如此模糊,我猜我需要拋出異常或以某種方式捕捉它?

import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) 

public class GameOver extends Actor { 
    public static Counter highScore; 
    public static int currentScore; 
    /** 
    * Act - do whatever the gameOver wants to do. This method is called whenever 
    * the 'Act' or 'Run' button gets pressed in the environment. 
    */ 
    public void act() { 
     displayScore(); 
     if(Greenfoot.isKeyDown("space")) { 
      Greenfoot.setWorld(new City()); 
     } 
    } 
    //This is where I am getting the error 
    private void displayScore() { 
     highScore = new Counter ("HIGHSCORE: "); 
     getWorld().addObject(highScore, 700, 50); //Add width/height 
     highScore.setLocation(City.WIDTH/2, City.HEIGHT/4); 
     currentScore = (0+City.score); 
     int topScore; 
     //The error is coming from this block of code 
     BufferedReader saveFile = new BufferedReader(new FileReader("TextSave.txt")); 
     topScore = Integer.parseInt(saveFile.readLine()); 
     saveFile.readLine(); 
     saveFile.close(); 
     if (topScore < currentScore) { 
      FileWriter writer = new FileWriter("topScore.txt"); 
      writer.write(currentScore); 
      writer.close(); 
     } 
     highScore.setValue(topScore); 
    } 
} 
+1

這很可能是關於你的FileReader:該文件沒有找到像Java說的那樣。 – Julien 2014-09-05 13:42:31

+0

我不認爲你的代碼將被編譯。您至少必須拋出或捕捉異常。當你使用BufferedReader時,你將不得不做異常處理。沒有它,代碼將不會被編譯,我會看到任何拋出子句或試圖捕獲塊 – Niru 2014-09-05 13:51:24

回答

0

爲了解決這個問題,我有2項建議

  1. 將像d文件的完整路徑://my_folder//TextSave.txt
  2. 將文件放在根目錄你的項目和檢查。
+0

選項1)是超越信念的 - 可怕的。如果你在沒有d:/?的計算機上運行它,該怎麼辦? – Gimby 2014-09-05 13:48:51

+0

@Gimby是的,你是對的,但它只是一個選項,以防止本地機器崩潰:) – 2014-09-05 13:51:25

+0

偉大的 - 這並不能使它成爲這個問題的答案。或者就此而言,任何問題。 – Gimby 2014-09-05 14:12:01

0

如果你想弄清楚它爲什麼不能找到該文件,把這個代碼:

System.out.println(new File("TextSave.txt").getAbsolutePath()); 

你會看到它試圖打開該文件。

如果要正確處理錯誤,在try/catch塊包裹IO操作:

try(BufferedReader saveFile = new BufferedReader(new FileReader("TextSave.txt"))) { 
    topScore = Integer.parseInt(saveFile.readLine()); 
    saveFile.readLine(); 
} catch(IOException e) { 
    // log the error or show error message here 
} 
1

你的問題是 「如何捕捉到異常」,這裏是一個答案:

//This is where I am getting the error 
    private void displayScore() { 
     highScore = new Counter ("HIGHSCORE: "); 
     getWorld().addObject(highScore, 700, 50); //Add width/height 
     highScore.setLocation(City.WIDTH/2, City.HEIGHT/4); 
     currentScore = (0+City.score); 
     int topScore; 
     try{ 
      //The error is coming from this block of code 
      BufferedReader saveFile = new BufferedReader(new FileReader("TextSave.txt")); 
      topScore = Integer.parseInt(saveFile.readLine()); 
      saveFile.readLine(); 
      saveFile.close(); 
      if (topScore < currentScore) { 
       FileWriter writer = new FileWriter("topScore.txt"); 
       writer.write(currentScore); 
       writer.close(); 
      } 
      highScore.setValue(topScore); 
     catch(FileNotFoundException e){ 
      // Handle your exception here, like print a message 'the file XXX couldn't be read' 
     } 
    } 

或者你可以將它傳遞給調用函數和處理它有:

private void displayScore() throws FileNotFoundException { 
    ... 
} 
0

替代,以及更強大的方法(仍然會涉及錯誤處理),將爲目標文件路徑創建一個文件,這將允許您驗證文件的存在,如果它實際上是一個可讀文件等,等等。

這將允許你檢查文件的狀態,並拋出你定義了舒適查明具體此時申請失敗例外:

File file = new File(System.getProperty("user.dir") 
      + "\\GameName\\Saves\\" + textsave.txt"); 
if(!f.exists()) { 
    f.getParentFile().mkdirs(); 
    try { 
     f.createNewFile(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

,並管理您的FileWriter,你以前,或更進一步,編寫代碼來處理讀取/寫入文件:

// just as an example - not complete, runnable code 
BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(file))); 

FileWriter fw = new FileWriter(file, append); // append is a boolean flag to overwrite/not overwrite the entire content of the file 
BufferedWriter bw = new BufferedWriter(fw); 
bw.write(currentScore); // using your code 

希望這有助於!可能是矯枉過正,可能不是:)

相關問題