java.io.FileNotFoundException :(访问被拒绝)当我有权访问该文件时

时间:2016-08-18 22:15:22

标签: java ioexception access-denied

所以我收到了这个错误:

java.io.FileNotFoundException:C:\ Users \ \ n审查 \ Documents \ Electrocode Productions \ template \ resources \ images(访问被拒绝)

当我运行此代码时:

 package com.template;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Copy {
    static File folder = new File(System.getProperty("user.home") + "/Documents/Electrocode Productions/template");

    public static void writeSaves() {   
        File imagesFile = new File(folder + "/resources/images");
        if(!imagesFile.exists()) {
            try {
                InputStream input = (Main.class.getResourceAsStream("/resources/images"));
                BufferedInputStream buffedInput = new BufferedInputStream(input);
                File fileFolder = new File(folder + "/resources/images");
                    try {
                        fileFolder.mkdirs();
                    } catch(SecurityException e) {
                        Log.error(e);
                    }
                OutputStream output = new FileOutputStream(fileFolder);
                BufferedOutputStream buffedOutput = new BufferedOutputStream(output);
                byte[] buffer = new byte[input.available()];
                System.out.println(buffer);
                int bytesRead;
                while((bytesRead = buffedInput.read(buffer)) > 0 ) {
                    buffedOutput.write(buffer, 0, bytesRead);

                }   
                input.close();
                output.close();
            } catch(IOException e) {
                Log.error(e);
            }
        }

尽管我很确定我有权访问该文件,就像我的代码的另一部分一样,我这样做:

        public static void dump() {
        if(!folder.exists()) {
            try {
                folder.mkdirs();
            } catch(SecurityException e) {
                Log.error(e);
            }
        }

        Path oldFilePath = Paths.get(folder + "/latest.log");
        Path newFilePath = Paths.get(folder + "/" + time.getDayOfMonth() + "." + time.getMonth() + "." + time.getYear() + "_" + time.getHour() + "." + time.getMinute() + "." + time.getSecond() + ".log");


        if(Config.get("keeplogs").equals("true")) {
            try(BufferedWriter writer = Files.newBufferedWriter(newFilePath, StandardOpenOption.CREATE, StandardOpenOption.APPEND, StandardOpenOption.WRITE)) {
                @SuppressWarnings("resource")
                Scanner scanner = new Scanner(oldFilePath);
                while(scanner.hasNextLine()) {
                    writer.write(scanner.nextLine());
                    writer.newLine();
                }
            } catch(IOException e) {
                e.printStackTrace();
            }
        } else if(Config.get("keeplogs").equals("false")) {

        } else {
            Log.warning("Unknown argument " + Config.get("log") + " in config.properties. Must be true or false");
        }
    }

我做错了什么?这可能是一个重复,但它可能不是因为我已经看到了我在这里可以找到的所有东西(StackOverflow)并尝试了所有。似乎没什么用。

2 个答案:

答案 0 :(得分:2)

您似乎使用mkdirs调用创建了C:\Users\censored\Documents\Electrocode Productions\template\resources\images目录。然后你试图像文件那样打开它显然失败了。

答案 1 :(得分:1)

以下是回答我问题的代码:

    public static void writeSaves(File src, File dest) throws IOException { 
    if(src.isDirectory()){
        //if directory not exists, create it
        if(!dest.exists()){
           dest.mkdirs();
        }

        //list all the directory contents
        String files[] = src.list();

        for (String file : files) {
           //construct the src and dest file structure
           File srcFile = new File(src, file);
           File destFile = new File(dest, file);
           //recursive copy
           writeSaves(srcFile,destFile);
        }

    }else{
        //if file, then copy it
        //Use bytes stream to support all file types

        InputStream in = new FileInputStream(src);
            OutputStream out = new FileOutputStream(dest);

            byte[] buffer = new byte[1024];

            int length;
            //copy the file content in bytes
            while ((length = in.read(buffer)) > 0){
               out.write(buffer, 0, length);
            }

            in.close();
            out.close();
    }

感谢所有试图提供帮助的人!