0

读取 .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)
4

1 回答 1

1

java.security.AccessControlException:访问被拒绝(“java.io.FilePermission”“/Users/Matt/Desktop/JavaFinalAssignment/highscore.csv”“写”

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

于 2013-11-07T02:20:20.860 回答