java.nio.file.accessDeniedException创建bufferedReader时

时间:2016-05-03 02:09:53

标签: java file access-denied

我编写的程序读取文件并使用文件中的数据来显示tileset图像,但是当我尝试为文件创建bufferedReader时,我得到java.nio.accessDeniedException。该文件不是只读的,我知道Path是正确的。以下是所有代码,错误在第46行,readMap()方法(BufferedReader reader = Files.newBufferedReader(f, charset);)。

// imports // NIO
import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.file.Files;
// IO
import java.io.BufferedReader;
import java.io.IOException;
// Swing
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
// Util
import java.util.ArrayList;

// the running class of my second tilest renderer.  Reads the tileset from a file
public class Main2 {
    public static int[][] map;

    // main method
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            // code to be excecuted
            public void run(){
                readMap();
                createTileset();
            }// run
        });// invokeLater
    }// main method

    // creates and shows the tileset
    public static void createTileset(){
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new TilesetMaker2(map, 3, 32, 32, "Images/Mountain.png"));
        frame.pack();
        frame.setVisible(true);
    }// createTileset

    // reads the map from a file
    public static void readMap(){
        ArrayList<String> lines = new ArrayList<String>();
        // try to read the file
        try{
            Charset charset = Charset.forName("US-ASCII");
            Path f = Paths.get("Images/Map.txt");
            BufferedReader reader = Files.newBufferedReader(f, charset);
            String line = null;
            // if the line, having been read, isnt nothing (meaning the last line)
            while ((line = reader.readLine()) != null){
                lines.add(line);
            }// while
        }catch(IOException e){
            e.printStackTrace();
        }// try/catch
        map = formatMap(lines);
    }// readMap

    // takes the raw strings from a map file and turns them into an array of ints usable by the program
    public static int[][] formatMap(ArrayList<String> lines){
        int[][] ret = new int[lines.size()][];
        // loop through the lines
        for (int i = 0;i < lines.size();i++){
            // if the line holds a layer
            if (lines.get(i).substring(0,6).equalsIgnoreCase("LAYER")){
                ret[i] = layerLine(lines.get(i));
            }// if the line is a layer
        }// loop through the lines
        return ret;
    }// formatMap

    // takes in a layer as a string, turns it into an array of ints usable by the rendered
    public static int[] layerLine(String line){
        String[] strLayer = line.substring(7).split(",");
        int[] intLayer = new int[strLayer.length];
        // loop through the strLayer, add the elements to layer as ints
        for (int i = 0;i < intLayer.length;i++){
            intLayer[i] = Integer.parseInt(strLayer[i]);
        }// loop
        return intLayer;
    }// layerLine


}// class

这是错误:

java.nio.file.AccessDeniedException: 
    at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
    at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
    at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
    at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(Unknown Source)
    at java.nio.file.Files.newByteChannel(Unknown Source)
    at java.nio.file.Files.newByteChannel(Unknown Source)
    at java.nio.file.spi.FileSystemProvider.newInputStream(Unknown Source)
    at java.nio.file.Files.newInputStream(Unknown Source)
    at java.nio.file.Files.newBufferedReader(Unknown Source)
    at learning.Main2.readMap(Main2.java:48)
    at learning.Main2$1.run(Main2.java:26)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

0 个答案:

没有答案