How to transfer a file to a non-empty directory?

I am new to the Java nio package and I cannot figure out how to get a file from one directory to another. My program should read through the directory and its subdirectories and process files based on certain conditions. I can get all the files using Files.walkFileTree, but when I try to move them, I get java.nio.file.AccessDeniedException.

If I try to copy them, I get a DirectoryNotEmptyException. I could not find help on Google. I am sure there should be an easy way to move a file from one directory to another, but I cannot figure it out.

Here is what I am trying to get a DirectoryNotEmptyException:

private static void findMatchingPdf(Path file, ArrayList cgbaFiles) { Iterator iter = cgbaFiles.iterator(); String pdfOfFile = file.getFileName().toString().substring(0, file.getFileName().toString().length() - 5) + ".pdf"; while (iter.hasNext()){ Path cgbaFile = (Path) iter.next(); if (cgbaFile.getFileName().toString().equals(pdfOfFile)) { try { Files.move(file, cgbaFile.getParent(), StandardCopyOption.REPLACE_EXISTING); } catch (IOException ex) { ex.printStackTrace(); } } } } 

I repeat the list of files, trying to map the .meta file to .pdf with the same name. As soon as I find a match, I move the metadata file to a directory with pdf.

I get this exception: java.nio.file.DirectoryNotEmptyException: C: \ test \ CGBA-RAC \ Part-A on sun.nio.fs.WindowsFileCopy.move (WindowsFileCopy.javahaps72) on sun.nio.fs.WindowsFileSystemProvider .move (WindowsFileSystemProvider.java:287) in java.nio.file.Files.move (Files.java:1347) in cgba.rac.errorprocessor.ErrorProcessor.findMatchingPdf (ErrorProcessor.java:149) in cgba.rac.errorprocessor. ErrorProcessor.matchErrorFile (ErrorProcessor.java:81) in cgba.rac.errorprocessor.ErrorProcessor.main (ErrorProcessor.java:36)

+6
source share
2 answers
 Files.move(file, cgbaFile.getParent(), StandardCopyOption.REPLACE_EXISTING); 

For the purpose, you provide the directory to which you want to move the file. This is not true. The target should be the new path name that you want the file to have - the new directory plus the file name.

For example, suppose you want to move /tmp/foo.txt to the /var/tmp . You call Files.move("/tmp/foo.txt", "/var/tmp") when you should call Files.move("/tmp/foo.txt", "/var/tmp/foo.txt")

You get this specific error because the JVM is trying to delete the target directory in order to replace it with a file.

One of them should generate the correct target path:

 Path target = cgbaFile.resolveSibling(file.getFileName()); Path target = cgbaFile.getParent().resolve(file.getFileName()); 
+11
source
 Path source = Paths.get("Var"); Path target = Paths.get("Fot", "Var"); try { Files.move( source, target, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { e.printStackTrace(); } 

java.nio.file. Files are a must, so here is an edited solution. See if it works coz. I have never used the new Files class before

+2
source

Source: https://habr.com/ru/post/977546/


All Articles