Java AccessControlException:写入时拒绝访问

时间:2013-11-07 01:48:46

标签: java exception input io output

阅读.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)

1 个答案:

答案 0 :(得分:1)

  

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

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