自己製作一個小遊戲作爲一個小項目,我不太擅長錯誤處理等類似問題,所以我得到一個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);
}
}
這很可能是關於你的FileReader:該文件沒有找到像Java說的那樣。 – Julien 2014-09-05 13:42:31
我不認爲你的代碼將被編譯。您至少必須拋出或捕捉異常。當你使用BufferedReader時,你將不得不做異常處理。沒有它,代碼將不會被編譯,我會看到任何拋出子句或試圖捕獲塊 – Niru 2014-09-05 13:51:24