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

java.io.FileNotFoundException: (Access is denied) when i have permission to the file

所以我收到了这个错误:

java.io.FileNotFoundException: C:\Users\censored\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");
        }
    }

我做错了什么?这可能是重复的,但可能不是,因为我已经查看了我在此处 (Whosebug) 上可以找到的所有内容并尝试了所有内容。似乎没有任何效果。

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

这是回答我问题的代码:

    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();
    }

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