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

java.io.FileNotFoundException:(访问被拒绝)当我有权限访问该文件时,java,ioexception,access-denied,Java,Ioexception,Access Denied,所以我得到了这个错误: java.io.FileNotFoundException:C:\Users\Censeded\Documents\Electrocode Productions\template\resources\images(访问被拒绝) 当我运行此代码时: package com.template; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.F

所以我得到了这个错误:

java.io.FileNotFoundException:C:\Users\Censeded\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),并尝试了所有。似乎什么也没用。

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

似乎您使用mkdirs调用创建了
C:\Users\censtered\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();
    }

谢谢你们所有试图帮助我的人

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

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

谢谢你们所有试图帮助我的人

哪个
catch
块正在捕获
java.io.FileNotFoundException
?如果第一个代码块引发异常,第二个代码块是否执行?}catch(IOException e){Log.error(e);}捕获异常,第二个代码块实际首先成功执行,您如何知道您对文件具有写访问权限。编辑:我不小心按了enter键而不是shift+enter键,并过早地提交了此文件。在您所说的代码中,首先检查文件夹是否存在,然后访问该文件。在引发FileNotFoundException的代码中,您加载的没有此代码,只是在您尝试将尝试创建文件夹的图像作为输入流加载之后。可能会检查您尝试访问的文件夹是否存在。您在哪个平台上运行此代码?您是否曾经在代码的其他地方打开过(您试图打开的)文件?如果是这样,你是否关闭过其他引用?Mario Alexandro Santini-我不检查,因为它是一个类文件(我知道它在那里,我正试图将它复制到非类文件)| CBHacking:Windows 10,它在代码的其他任何地方都没有被调用。哪个
catch
块正在捕获
java.io.FileNotFoundException
?如果第一个代码块引发异常,第二个代码块是否执行?}catch(IOException e){Log.error(e);}捕获异常,第二个代码块实际首先成功执行,您如何知道您对文件具有写访问权限。编辑:我不小心按了enter键而不是shift+enter键,并过早地提交了此文件。在您所说的代码中,首先检查文件夹是否存在,然后访问该文件。在引发FileNotFoundException的代码中,您加载的没有此代码,只是在您尝试将尝试创建文件夹的图像作为输入流加载之后。可能会检查您尝试访问的文件夹是否存在。您在哪个平台上运行此代码?您是否曾经在代码的其他地方打开过(您试图打开的)文件?如果是这样的话,你是否关闭过其他引用?Mario Alexandro Santini-我不检查,因为它是一个类文件(我知道它在那里,我正试图将它复制到非类文件)| CBHacking:Windows 10,代码中的其他任何地方都没有调用它。Ok。有没有其他方法(xOutputStream)可以用来编写目录?没有,您只能列出、创建、删除或重命名目录中的文件。至少在Windows上,您不能将directroy作为字节流打开,以便使用java.io进行读写。读/写资源目录也是如此。如果要复制多个文件,需要在BudSkywalkerOk上迭代它们。有没有其他方法(xOutputStream)可以用来编写目录?没有,您只能列出、创建、删除或重命名目录中的文件。至少在Windows上,您不能将directroy作为字节流打开,以便使用java.io进行读写。读/写资源目录也是如此。如果你想复制多个文件,你需要在天行者处迭代它们