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))
– Valdecir
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:"
– Valdecir
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
– Valdecir
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?
– Maniero