Home > Blockchain >  How do I move a file to a non-empty directory in java?
How do I move a file to a non-empty directory in java?

Time:06-21

I'm trying to move an exe but I'm getting DirectoryNotEmptyException. Do I have to use another method or is there something I'm missing.

try {
    Path rbx = Path.of(System.getProperty("user.dir") "\\src\\test\\something.txt");
    Path target = Path.of(System.getenv("LOCALAPPDATA")  "\\toIt\\4ddd");
    Files.move(rbx, target, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
    e.printStackTrace();
}

CodePudding user response:

Files.move isn't like the shell mv command. The 'target' has to include the actual file name. In other words, if your intent is for the something.txt file to cease being at ~/src/test/something.txt and start being at $LOCALAPPDATA/toIt/4ddd/something.txt, then you actually have to add something.txt to the target.

The reason you get this error is that the instruction you are running is telling the system: DELETE the entire 4ddd directory and once it is completely gone, make a text file named 4ddd. Which Files.move won't do, even if you use REPLACE_EXISTING.

CodePudding user response:

if you want to move,you should add file type like this

try {
        Path rbx = Path.of(System.getProperty("user.dir") "\\src\\test\\something.txt");
        Path target = Path.of(System.getenv("LOCALAPPDATA")  "\\toIt\\4ddd.txt");
        Files.move(rbx, target, StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {

        e.printStackTrace();
    }
  •  Tags:  
  • java
  • Related