BudSkywalker
BudSkywalker

Reputation: 21

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

so I'm getting this error:

java.io.FileNotFoundException: C:\Users\censored\Documents\Electrocode Productions\template\resources\images (Access is denied)

when I run this code:

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

despite the fact that I'm pretty sure I have access to write to the file as in another section of my code I do this:

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

What am I doing wrong? This maybe a duplicate but it may not be as I've looked at everything I can find on here (StackOverflow) and tried it all. Nothing seems to be working.

Upvotes: 0

Views: 7540

Answers (2)

BudSkywalker
BudSkywalker

Reputation: 21

Here is the code that answered my question:

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

Thank you all who tried to help!

Upvotes: 1

Anton Malyshev
Anton Malyshev

Reputation: 8851

It seems that you created C:\Users\censored\Documents\Electrocode Productions\template\resources\images directory with mkdirs call. Then you trying to open it like a file what obviously fails.

Upvotes: 2

Related Questions