PDA

View Full Version : FNF Error: scores.dat (The system cannot find the file specified)


Cloakd
09-23-2009, 09:56 PM
I have created a highscore table loader and writer for my little game yet it cant seem to find the file let alone make or write to it.

here is the code:
package javaapplication2;

import java.util.*;
import java.io.*;

public class HighscoreManager {
private ArrayList<Score> scores;

private final String HIGHSCORE_FILE = "scores.dat";

ObjectOutputStream outputStream = null;
ObjectInputStream inputStream = null;

public HighscoreManager() {
scores = new ArrayList<Score>();
}

public ArrayList<Score> getScores() {
loadScoreFile();
sort();
return scores;
}

private void sort() {
ScoreComparator comparator = new ScoreComparator();
Collections.sort(scores, comparator);
}


public void addScore(int score) {
loadScoreFile();
scores.add(new Score(score));
updateScoreFile();
}

public void loadScoreFile() {
try {
inputStream = new ObjectInputStream(new FileInputStream(HIGHSCORE_FILE));
scores = (ArrayList<Score>) inputStream.readObject();
} catch (FileNotFoundException e) {
System.out.println("[HighScore] FNF Error: " + e.getMessage());
} catch (IOException e) {
System.out.println("[HighScore] IO Error: " + e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println("[HighScore] CNF Error: " + e.getMessage());
} finally {
try {
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
} catch (IOException e) {
System.out.println("[HighScore] IO Error: " + e.getMessage());
}
}
}

public void updateScoreFile() {
try {
outputStream = new ObjectOutputStream(new FileOutputStream(HIGHSCORE_FILE));
outputStream.writeObject(scores);
} catch (FileNotFoundException e) {
System.out.println("[Update] FNF Error: " + e.getMessage() + ",the program will try and make a new file");
} catch (IOException e) {
System.out.println("[Update] IO Error: " + e.getMessage());
} finally {
try {
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
} catch (IOException e) {
System.out.println("[Update] Error: " + e.getMessage());
}
}
}

public String getHighscoreString() {
String highscoreString = "";
int max = 3;

ArrayList<Score> scores;
scores = getScores();

int i = 0;
int x = scores.size();
if (x > max) {
x = max;
}
while (i < x) {
highscoreString += (i + 1) + "\t\t" + scores.get(i).getScore() + "\n";
i++;
}
return highscoreString;
}
}

package javaapplication2;

import java.util.Comparator;

public class ScoreComparator implements Comparator<Score> {
public int compare(Score score1, Score score2) {

int sc1 = score1.getScore();
int sc2 = score2.getScore();

if (sc1 > sc2){
return -1;
}else if (sc1 < sc2){
return +1;
}else{
return 0;
}
}
}

package javaapplication2;

import java.io.Serializable;

public class Score implements Serializable {
private int score;

public int getScore() {
return score;
}

public Score(int score) {
this.score = score;
}
}

Any help would be greatly appreciated, i have even created a scores.dat and that doesn't work :S