首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法写入AppData

无法写入AppData
EN

Stack Overflow用户
提问于 2017-04-05 21:27:23
回答 1查看 329关注 0票数 0

我正在JavaFX做一个小游戏,我想要一个高分的系统.因为我不太擅长数据库,所以我把数据写到了一个文本文件中。但是这很容易编辑,所以我想把它写到AppData中的文本文件中。所以我试着用这句话:

代码语言:javascript
复制
File file = new File(System.getenv("AppData") + "\\" + [Namefoler\\NameFile]);

但它一直说我没有访问那个文件夹的权限。写入和关闭一个文件是没有问题的,所以我的系统可以正常工作,只要我把它写到与我的jar目录相同的文本文件中。但它不断地抛出这些错误。

有人知道我怎样才能防止这件事,或者更好地解决我的问题吗?

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-05 23:25:44

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

您在问题中提到,您担心用户修改首选项以伪造高分。加密或隐藏客户端应用程序的数据,使用户无法修改数据,这是一个棘手的问题,因此我将在这里不讨论如何做到这一点。如果这是你的一个要求,那么你可以在互联网上研究其他信息,寻找实现这一目标的方法。

ScoreStorage.java

代码语言:javascript
复制
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

测试线束:

代码语言:javascript
复制
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);
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43241934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档