gpt4 book ai didi

Java访问控制异常: Access Denied when writing

转载 作者:行者123 更新时间:2023-12-01 13:52:56 26 4
gpt4 key购买 nike

读取 .csv 文件时没有任何问题。但是在代码末尾,当我尝试向其写入新值时,我收到访问被拒绝的异常,但我不完全确定原因。谁能解释一下可能是什么原因造成的?

private Path highScoreFile = Paths.get("/Applications/JavaFinalAssignment/highscore.csv");
public String checkhighScore()
{

try{
InputStream input = new BufferedInputStream(Files.newInputStream(highScoreFile));
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String s = "";
String delimiter = ",";
s = reader.readLine();

while(s!= null)
{
scoreArray = s.split(delimiter);
highScoreName = scoreArray[0];
highScoreNum = Integer.parseInt(scoreArray[1]);
s = reader.readLine();
}
reader.close();

}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "ERROR: " + e);
}
String scoreReturn = (scoreArray[0] + " " + scoreArray[1]);
return scoreReturn;
}

public void setHighScore(String name, int num)
{
try
{
String s = "";
String delimiter = ",";
OutputStream output = new BufferedOutputStream(Files.newOutputStream(highScoreFile));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output));
String numString = Integer.toString(num);
s = name + delimiter + numString;
writer.write(s);
writer.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Error saving high score file: " + e);
}

}

编辑:

添加了堆栈跟踪:

java.security.AccessControlException: access denied ("java.io.FilePermission" "/Users/Matt/Desktop/JavaFinalAssignment/highscore.csv" "write")
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:366)
at java.security.AccessController.checkPermission(AccessController.java:560)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
at java.lang.SecurityManager.checkWrite(SecurityManager.java:979)
at sun.nio.fs.UnixChannelFactory.open(UnixChannelFactory.java:240)
at sun.nio.fs.UnixChannelFactory.newFileChannel(UnixChannelFactory.java:138)
at sun.nio.fs.UnixChannelFactory.newFileChannel(UnixChannelFactory.java:150)
at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:211)
at java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:430)
at java.nio.file.Files.newOutputStream(Files.java:170)
at FinalAssignment.setHighScore(FinalAssignment.java:335)
at FinalAssignment.GameOver(FinalAssignment.java:239)
at FinalAssignment.actionPerformed(FinalAssignment.java:140)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

最佳答案

java.security.AccessControlException: access denied ("java.io.FilePermission" "/Users/Matt/Desktop/JavaFinalAssignment/highscore.csv" "write"

这不是操作系统用户权限问题,而是 Java 沙箱权限问题。您正在安全管理器下运行,并且您的 .policy 文件未授予您对该目录的写入权限。删除安全管理器,或者签署应用程序,或者调整您的 .policy 文件。每当您收到 java.security.AccessControlException 时,都意味着您没有消息中指定的权限,在本例中为 java.io.FilePermission 等。

关于Java访问控制异常: Access Denied when writing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19826507/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com