Failure to copy an entire directory and its files

Asked

Viewed 421 times

0

I need to copy an entire directory that contains files inside using Java NIO2 with FileChannels. But I can only copy the files inside the directory.

Follow the code I’m using to copy the files:

public static void copiaDestino(File destino, File origem) throws IOException{

    FileChannel inChannel = new FileInputStream(origem).getChannel();
    FileChannel outChannel = new FileOutputStream(destino).getChannel();
    try{
        int maxCont = (64 * 1024 * 1024) - (32 * 1024);
        long size = inChannel.size();
        long position = 0;

        while (position < size){
            position += inChannel.transferTo(position, size, outChannel);
        }
        }finally{
            if(inChannel != null){
                inChannel.close();
            }
            if(outChannel != null){
                outChannel.close();
            }
        }
    }
}
  • Then as I am still testing I create two variables of type File and step the paths (Origin/Destination). When executing the code by passing the directory and not the file name of an error (Exception in thread "main" java.io.Filenotfoundexception: C: Novafolder (Access denied))

  • Then the path even finds when I put inside the " C: "however if I want to copy for example "C: test" also gives the same error, but if I add: Source file = new File("C: Novafolder server.sql"); Target file = new File("C: t"); It creates a file named " t " in "C:"

  • I apologize I’m starting here now. But what I am trying to do is copy the directory "C: New Folder" to "C: New Pasta2", I have the above method to copy both the directory and the files contained in it. In main I prompt 2 variables of type File and step origin and destination, if I leave as I said above origin and destination it from the access denied in the source folder and if I point a file inside the source folder the access denied error happens in the destination path, if the source path does not exist then it creates a file and does not copy the cited one at the source

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

1 answer

1

This method only copies files and not directories.

The comments seem to be working when used correctly. I noticed that there is a parameter inversion that makes its use not so intuitive and is probably generating errors because it is doing the opposite of what it thinks.

For lack of information I can not help more than this.

//note a inversão dos parâmetros aqui
public static void copiaDestino(File origem, File destino) throws IOException{

    FileChannel inChannel = new FileInputStream(origem).getChannel();
    FileChannel outChannel = new FileOutputStream(destino).getChannel();
    try{
    int maxCont = (64 * 1024 * 1024) - (32 * 1024);
    long size = inChannel.size();
    long position = 0;

    while (position < size){
        position += inChannel.transferTo(position, size, outChannel);
    }
    }finally{
    if(inChannel != null){
        inChannel.close();
    }
    if(outChannel != null){
        outChannel.close();
    }
}

In the main():

try {
    File origem = new File("C:\\Novapasta\\server.sql");
    File destino = new File("C:\\t");
    copiaDestino(origem, destino)
} catch (Exception ex) { //isto só é útil aqui porque é o main
    System.out.println("Deu erro");
}

I put in the Github for future reference.

  • Thanks for your help... But the error that was giving access denied was because I was passing a file as source and a directory as destination... would look like this: Source file = new File("C: Novafolder server.sql"); Target file = new File("C: t copy.sql"); Only so he copied. Thank you and stay there in case someone is having the same difficulty.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.