Problem beim Schreiben einer Nummer in eine lesbare Klartextdatei in Java [geschlossen]
Posted: 11 Aug 2025, 05:52
Ich habe ein Problem, das Textdateien in Java generiert. Ich habe ein einfaches Spiel mit farbigen Bällen gemacht, und wenn zwei gleiche Farbbälle kollidieren, steigt die Punktzahl jedes Mal mit 5 Punkten. Ich versuche, die Punktzahl für das Laden zu speichern, wenn ich ein neues Spiel in einer "points.txt" -Datei starten kann. Java erstellt eine "points.txt" -Datei, aber sie besteht nur aus unlösbaren Symbolen.
---- DroppingStickyBalls.java ----
int points = 0;
int highScore = 0;
public DroppingStickyBalls(){
.... //other settings
loadScore();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
....// other code - in case of collision between same-colored balls ->
points += 5;
//the score is saved when the window of the game is getting closed
Main.getFrame().addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
if(points > highScore) saveScore();
e.getWindow().dispose();
}
});
g2.dispose();
}
public void saveScore(){
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("points.txt"));
bw.write(points);
bw.close();
}catch (IOException e){
e.printStackTrace();
}
}
public void loadScore(){
File file = new File("points.txt");
if(file.exists()) {
try {
BufferedReader br = new BufferedReader(new FileReader(String.valueOf(file.toPath())));
System.out.println(br.readLine());
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
---- points.txt ----
//this is the result in Notepad
// this is the result in Intellij IDEA - actually is SI, but here it is displayed again as a square...
< /code>
Ich habe versucht, die Textdatei sowohl in der Intellij -Idee als auch im Windows -Datei -Explorer zu öffnen, aber das Ergebnis ist das gleiche: Der Inhalt ist codiert und unleserlich. Gibt es eine Codierungseinstellung, die ich ändern sollte? Es ist besser zu versuchen, den Autor der Frage zu kontaktieren, um weitere Informationen zu erhalten!
---- DroppingStickyBalls.java ----
int points = 0;
int highScore = 0;
public DroppingStickyBalls(){
.... //other settings
loadScore();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
....// other code - in case of collision between same-colored balls ->
points += 5;
//the score is saved when the window of the game is getting closed
Main.getFrame().addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
if(points > highScore) saveScore();
e.getWindow().dispose();
}
});
g2.dispose();
}
public void saveScore(){
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("points.txt"));
bw.write(points);
bw.close();
}catch (IOException e){
e.printStackTrace();
}
}
public void loadScore(){
File file = new File("points.txt");
if(file.exists()) {
try {
BufferedReader br = new BufferedReader(new FileReader(String.valueOf(file.toPath())));
System.out.println(br.readLine());
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
---- points.txt ----
//this is the result in Notepad
// this is the result in Intellij IDEA - actually is SI, but here it is displayed again as a square...
< /code>
Ich habe versucht, die Textdatei sowohl in der Intellij -Idee als auch im Windows -Datei -Explorer zu öffnen, aber das Ergebnis ist das gleiche: Der Inhalt ist codiert und unleserlich. Gibt es eine Codierungseinstellung, die ich ändern sollte? Es ist besser zu versuchen, den Autor der Frage zu kontaktieren, um weitere Informationen zu erhalten!