How to use Files.move with Java?

0

I created a photo upload on my system, these photos are saved in a temp, I need to move them to a default directory, other than temp.

This is the method that creates the directory:

public FotoStorageLocal() {
    //Versao MAC
    //this(getDefault().getPath(System.getenv("user.home"), ".genesisfotos"));

    // versao windows
    this(getDefault().getPath(System.getProperty("user.home"), ".genesisfotos"));
        System.out.println(">>>>>>>> CRIADO GENESIS + FOTOS ");
}

public FotoStorageLocal(Path path) {
    this.local = path;
    criarPastas();
}

And this is the method that makes the photo saved and then transferred from the temp folder to the default directory.

I run the system but it can not take the photo from the folder it has to another folder, follow code

public void salvar(String foto) {
    try {
        Files.move(this.localTemporario.resolve(foto), this.local.resolve(foto));
    } catch (IOException e) {
        throw new RuntimeException("Erro movendo a foto para destino final", e);
    }
    try {
        Thumbnails.of(this.local.resolve(foto).toString()).size(40, 68).toFiles(Rename.PREFIX_DOT_THUMBNAIL);
    } catch (IOException e) {
        throw new RuntimeException("Erro gerando thumbnail", e);
    }
}
    
asked by anonymous 09.05.2018 / 02:47

1 answer

0

The use of Files.move() method is:

Path sourcePath      = Paths.get("data/logging-copy.properties");
Path destinationPath = Paths.get("data/subdir/logging-moved.properties");

try {
    Files.move(sourcePath, destinationPath,
            StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
    e.printStackTrace();
}

Note that (Path source, Path target, CopyOption... options) are used as parameters. Ensure that the Files.move(this.localTemporario.resolve(foto), this.local.resolve(foto)); method is correctly using the

    
09.05.2018 / 03:17