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)
source share