У меня есть проблема, создавая текстовые файлы в Java. Я сделал простую игру с цветными шариками, и когда сталкиваются два одинаковых шарика, счет увеличивается с 5 очками каждый раз. Я пытаюсь сохранить счет для того, чтобы быть доступным для загрузки при запуске новой игры в файле «point.txt». Java создает файл «point.txt», но он сделан только из нечитаемых символов.
---- 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>
Я попытался открыть текстовый файл как в INTELLIJ Idea, так и в Explorer File Windows, но результат одинаково: содержимое кодируется и не читается. Есть ли какая -то настройка кодирования, которую я должен изменить? Лучше пытаться связаться с автором вопроса для получения дополнительной информации!
Подробнее здесь: https://stackoverflow.com/questions/797 ... le-in-java
Problem writing a number to a readable plain text file in Java [closed] ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение