【问题标题】:Not able to write to AppData无法写入 AppData
【发布时间】:2017-04-05 21:27:23
【问题描述】:

我正在用 JavaFX 制作一个小游戏,我想要一个高分系统。 因为我不太擅长数据库,所以我将数据写入了一个文本文件。 但这很容易编辑,所以我想将它写入 AppData 中的文本文件。 所以我尝试了这一行:

File file = new File(System.getenv("AppData") + "\\" + [Namefoler\\NameFile]);

但它一直说我无权访问该文件夹。 写入和关闭文件没有问题,所以如果我只是将它写入与我的 jar 相同目录中的文本文件,我的系统就可以正常工作。 但它不断抛出这些错误。

有人知道我可以如何防止这种情况,或者更好地解决我的问题吗?

提前致谢

【问题讨论】:

  • 两种都试过了,都不管用!
  • preferences API 可能是您更好的解决方案。
  • @jewelsea,但我只能记住 1 个高分?我想列出所有的高分!
  • 当然,看看Preferences class,您可以关联任意数量的键/值对。作为奖励,它不是特定于 Windows 的,因此它可以在没有 AppData 的非 Windows 平台上运行。

标签: java file javafx appdata


【解决方案1】:

此答案与 AppData 无关,但您确实询问了其他解决方案,因此这是基于 Preferences API 的解决方案。该示例适用于高分系统的基于首选项的 API 存储。该示例不加密数据。

您在问题中提到您担心用户会修改偏好以捏造高分。为客户端应用程序加密或隐藏数据以使用户无法修改数据是一个棘手的问题,因此我不会在这里讨论如何做到这一点。如果这是您的要求,那么您可以在 Internet 上研究其他信息以了解实现此目的的方法。

ScoreStorage.java

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

public class ScoreStorage {
    private static final int MAX_NUM_SCORES = 3;

    private static final String SCORE_PREFERENCE_KEY_PREFIX = "score-";

    private ObservableList<Integer> scores = FXCollections.observableArrayList();
    private Preferences scorePreferences = Preferences.userNodeForPackage(
        ScoreStorage.class
    );

    public ScoreStorage() throws BackingStoreException {
        for (String key: scorePreferences.keys()) {
            scores.add(scorePreferences.getInt(key, 0));
        }
    }

    public ObservableList<Integer> getUnmodifiableScores() {
        return FXCollections.unmodifiableObservableList(scores);
    }

    public void clearScores() {
        scores.clear();
        storeScores();
    }

    public void recordScore(int score) {
        int i = 0;
        while (i < MAX_NUM_SCORES && i < scores.size() && scores.get(i) >= score) {
            i++;
        }

        if (i < MAX_NUM_SCORES) {
            if (scores.size() == MAX_NUM_SCORES) {
                scores.remove(scores.size() - 1);
            }
            scores.add(i, score);
            storeScores();
        }
    }

    private void storeScores() {
        int i = 0;
        for (int score: scores) {
            scorePreferences.putInt(SCORE_PREFERENCE_KEY_PREFIX + i, score);
            i++;
        }
        while (i < MAX_NUM_SCORES) {
            scorePreferences.remove(SCORE_PREFERENCE_KEY_PREFIX + i);
            i++;
        }
    }
}

HighScoreApp.java

测试工具:

import javafx.application.Application;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

import java.util.Random;
import java.util.prefs.BackingStoreException;

public class HighScoreApp extends Application {

    private static ScoreStorage scoreStorage;

    private Random random = new Random(42);

    @Override
    public void start(Stage stage) throws Exception {
        ListView<Integer> scoreList = new ListView<>(scoreStorage.getUnmodifiableScores());
        scoreList.setPrefHeight(150);

        Label lastScoreLabel = new Label();

        Button generateScore = new Button("Generate new score");
        generateScore.setOnAction(event -> {
            int lastScore = random.nextInt(11_000);
            lastScoreLabel.setText("" + lastScore);
            scoreStorage.recordScore(lastScore);
        });

        Button clearScores = new Button("Clear scores");
        clearScores.setOnAction(event -> scoreStorage.clearScores());

        HBox scoreGenerator = new HBox(10, generateScore, lastScoreLabel);
        scoreGenerator.setAlignment(Pos.BASELINE_LEFT);

        VBox layout = new VBox(10, scoreGenerator, scoreList, clearScores);
        layout.setPadding(new Insets(10));

        stage.setScene(new Scene(layout));
        stage.show();
    }

    public static void main(String[] args) throws BackingStoreException {
        scoreStorage = new ScoreStorage();
        launch(args);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-03
    • 1970-01-01
    • 2011-07-06
    • 1970-01-01
    • 2023-02-18
    • 2011-12-14
    • 2011-10-25
    • 1970-01-01
    相关资源
    最近更新 更多