Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I move a file to a non-empty directory?

Tags:

java

nio

I'm new to Java's nio package and I can't figure out how to get a file from one directory into another. My program is supposed to read through a 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 a java.nio.file.AccessDeniedException.

If I try to copy them, I get a DirectoryNotEmptyException. I haven't been able to find any help on Google. I'm sure there must an easy way to move a file from one directory to another, but I can't figure it out.

This is what I'm trying that gets the 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'm iterating through a list of files, trying to match a .meta file with a .pdf of the same name. Once I find the match, I move the metadata file to the directory that has the pdf.

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

like image 842
user2406854 Avatar asked Oct 31 '14 15:10

user2406854


People also ask

How do I move a folder that is not empty?

One of the best way is using rsync instead. rsync is built for copying files from source to destination, and have the ability to retain the attributes of many files and folders. Overall, rsync is better than cp because it only copies the files that is not present in the target directory instead of all files.

How do I move a file to a different directory?

Right-click the file or folder you want, and from the menu that displays click Move or Copy. The Move or Copy window opens. Scroll down if necessary to find the destination folder you want. If you need to, click on any folder you see to access its subfolders.

What is a non empty directory?

The non-empty directory means the directory with files or subdirectories. We can delete the directory by using the Delete() method of the Directory class.

How copy non empty directory in Linux?

Copying Directories with cp Command To copy a directory, including all its files and subdirectories, use the -R or -r option. The command above creates the destination directory and recursively copy all files and subdirectories from the source to the destination directory.


2 Answers

Files.move(file, cgbaFile.getParent(), StandardCopyOption.REPLACE_EXISTING);

For the target, you're providing the directory you want to move the file into. This is incorrect. The target should be the new pathname that you want the file to have--the new directory plus the filename.

For example, suppose you wanted to move /tmp/foo.txt to the /var/tmp directory. You're calling Files.move("/tmp/foo.txt", "/var/tmp") when you should be calling Files.move("/tmp/foo.txt", "/var/tmp/foo.txt").

You're getting that specific error because the JVM is trying to delete the target directory in order to replace it with the file.

One of these ought to generate the correct target path:

Path target = cgbaFile.resolveSibling(file.getFileName());

Path target = cgbaFile.getParent().resolve(file.getFileName());
like image 150
Kenster Avatar answered Oct 21 '22 20:10

Kenster


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 is a necessity, so here is the edited solution. Please see if it works coz I have never used the new Files class before

like image 36
Ferrakkem Bhuiyan Avatar answered Oct 21 '22 20:10

Ferrakkem Bhuiyan