How to change FileOutputStream to class.getResource()
Code:
public class HighScores {
//Inner class, Score.
class Score implements Comparable<Score> {
String name;
int score;
//Constructs scores from text file.
Score(StreamTokenizer stream) throws IOException {
if (stream.ttype != StreamTokenizer.TT_WORD) {
System.err.println("Expected highscore name value in highscores.txt not found. "+stream.lineno());
System.exit(1);
}
name = stream.sval;
stream.nextToken();
if (stream.ttype != StreamTokenizer.TT_NUMBER) {
System.err.println("Expected number in highscores.txt not found: "+stream.sval+", line: "+stream.lineno());
System.exit(1);
}
score = (int) stream.nval;
stream.nextToken();
}
public Score(String n, int s) {
name = n;
score = s;
}
@Override
public String toString() {
return name+" "+score;
}
@Override
public int compareTo(Score o) {
return ((Integer) o.score).compareTo((Integer) score);
}
}
ArrayList<Score> scores;
public HighScores(String filename) {
StreamTokenizer stream = null;
try {
stream = new StreamTokenizer(new BufferedReader(new InputStreamReader(new FileInputStream(filename))));
} catch (FileNotFoundException e1) {
System.err.println("File not found '"+filename+"'");
e1.printStackTrace();
System.exit(1);
}
scores = new ArrayList<Score>();
try {
parse(stream);
} catch (IOException e) {
System.err.println("IO exception while reading file "+filename);
e.printStackTrace();
System.exit(1);
}
}
//Parse the text of the highscore file.
void parse(StreamTokenizer stream) throws IOException {
stream.nextToken();
if (!(stream.ttype == StreamTokenizer.TT_WORD && stream.sval.equals("begin"))) {
System.err.println("Expected token \"begin\" not found: "+stream.sval+", line: "+stream.lineno());
System.exit(1);
}
stream.nextToken();
while (!(stream.ttype == StreamTokenizer.TT_WORD && stream.sval.equals("end"))) {
scores.add(new Score(stream));
}
}
@Override
public String toString() {
Collections.sort(scores);
String rtn = "";
for (Score s:scores)
rtn += " " +s+ "\n";
return rtn;
}
//Add a score to the highscore list if it is larger than any one of the old scores.
public void addScore(String name, int score) throws IOException{
Score scoreToReplace = null;
Boolean hasScoreReplaceValue = false;
for (Score s : scores){
if (score > s.score && hasScoreReplaceValue == false){
hasScoreReplaceValue = true;
scoreToReplace = s;}
}
if (scoreToReplace != null){
scores.add(scores.indexOf(scoreToReplace), new Score(name, score));
scores.remove(scoreToReplace);
try {
FileOutputStream out = new FileOutputStream("src/Teeter/highscores.txt");
out.write("begin".getBytes());
for (Score s : scores){
out.write((" "+s.name+" "+s.score+" ").getBytes());
}
out.write("end".getBytes());
out.flush();
} catch (FileNotFoundException e) {
System.err.println("Error. highscores.txt not found.");
e.printStackTrace();
}
}
FileOutputStream out = new FileOutputStream("src/Teeter/highscores.txt");
I know that in order to run this with jar file, I must change it to class.getResources() method but I just don't know to change this. What can be the good way to change this? Please... [when I compress this into jar file I must create the folder ('Teeter') and put jar file with src folder and then put highscores.txt into that src folder. I just want it to be compiled by just operating jar file.
Re: How to change FileOutputStream to class.getResource()
The jar archive is meant for program resources - that is things that don't change and are read by the program. I don't think there is any standard way of the program changing the contents of the jar.