首页 > 解决方案 > 尝试在 java 中使用 FileWriter/BufferedWriter 写入时权限被拒绝。

问题描述

我真的在苦苦挣扎。基本上我和我的几个同学一起做一个项目,我们必须提供一个太空战争重拍,我们实现了一个单人模式,你可以在其中实际获得分数。问题是我可以读取我的文件,但不能在上面写字。我尝试了所有我可以在网上找到的系统,但没有设法解决错误。这是管理高分系统的类的代码。我什至尝试用代码创建文件并尝试删除它并重新创建一个空文件。该文件现在位于项目的主目录中。方法 readFile 和 getHighscores 工作,但检查总是卡在写作部分。我得到的错误是“Il privilegio richiesto non appartiene al client [Ljava.lang.StackTraceElement;@6eb60ef7”翻译为“

请记住,这一切都在我应该拥有所有权限的 git 存储库中,但这基本上是我脑海中唯一的想法。

package controller;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

/**
 *the creation of the HighscoreManager class.
 */
public class HighscoreManager {

    private static final int LAST_HIGHSCORE_INDEX = 9;
    private static final String FILE_NAME = "/Highscores.txt";
    private ArrayList<Integer> highscores = new ArrayList<Integer>();

    /**
     * reads the file and loads the highscore list.
     */
    private void readFile() {
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        String s;
        highscores.clear();
        try {
            is = this.getClass().getResourceAsStream(FILE_NAME);
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            while ((s = br.readLine()) != null) {
                highscores.add(Integer.parseInt(s));
            }
            if(br!=null && isr !=null && is!= null) {
                br.close();
                isr.close();
                is.close();
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    /**
     * checks if the passed score is an actual highscores.
     * @param score 
     */
    public void checkHighscores(final int score) {
        readFile();
        File file = new File(FILE_NAME);
        boolean newHighscoreFound = false;
        for (int i = 0; i < highscores.size(); i++) {
            if (score > highscores.get(i)) {
                newHighscoreFound = true;
                break;
            }
        }
        if (newHighscoreFound) {
            highscores.add(LAST_HIGHSCORE_INDEX, score);
            Collections.sort(highscores);
            Collections.reverse(highscores);
            for (int i : highscores) {
                System.out.println(i);
            }
            if(!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
            FileWriter fw = null;
            BufferedWriter bw = null;
            try {
                fw = new FileWriter(file);
                bw = new BufferedWriter(fw);
                for(int i : highscores) {
                    bw.write(i);
                }
            } catch(IOException e) {
                System.out.println(e.getStackTrace());
            } finally {
                if(fw != null && bw != null) {
                    try {
                        fw.close();
                        bw.close();
                    } catch (IOException e) {
                        System.out.println(e.getStackTrace());
                    }

                }
            }

        }
    }

    /**
     * returns an array list of strings containing all the current highscores.
     * @return toBeReturned
     */
    public ArrayList<String> getHighscores() {
        ArrayList<String> toBeReturned = new ArrayList<>();
        try {
            highscores.clear();
            readFile();
            for (int i : highscores) {
                toBeReturned.add(String.valueOf(i));
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            System.out.println("There you have your highscores.");
        }
        return toBeReturned;
    }
}

标签: javafilebufferedwriter

解决方案


您无需访问Class对象即可加载文件(或实际上是关联的ClassLoader. 只需使用普通的 Java IO。

要阅读您的一组高分:

highScores = Files.lines(Paths.get(FILE_NAME))
                .map(Integer::parseInt)
                .collect(Collectors.toList());

写一组新的高分:

Files.write(Paths.get(FILE_NAME), highScores.stream()
                .map(String::valueOf)
                .collect(Collectors.toList()));

(另外,为什么用“git”标记?)


推荐阅读