java - 尝试在 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;
}
}
解决方案
您无需访问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”标记?)
推荐阅读
- javascript - 编写一个程序,将变量 num 转换为字符串。变数 = 35.36 ; 删除点以在浏览器中显示“3536”。如何执行此操作
- android - Dailymotion:我想通过 android sdk 在我的 android 应用程序中播放我的私人视频
- php - 如何在PHP中查找文本是否包含url字符串
- c++ - 将 std::smart_ptr 与 Pybind11 一起使用
- python-3.x - 第 6 行实际上做了什么来检查排列是否已经显示?
- angular - 构建 SSR 应用程序时选择环境 - Angular 9
- javascript - Aurelia bindingContext 未定义
- c# - 子类填写后在自己的类中访问时List变为null
- azure - bcp 将数据导入 Azure 数据仓库
- ios - 无法传递文本字段值