Dom
Dom

Reputation: 69

Copy file and rename file on destination

I created a java function to copy file_1 from path_1 to path_2.

Function code is:

public void copyFiles(String source, String destination) {

    File src = new File(source);
    File dest = new File(destination);
    try {
        Files.copy(src.toPath(), dest.toPath());
    }catch (IOException e){
        logger.log(Level.SEVERE, "IOException.", e);

    }
}

I would like to modifiy my function to copy file_1 from path_1 and rename the file on destination to file_dest The idea of the function is to be used in another function. How can do it please ? Thanks

Upvotes: 0

Views: 101

Answers (1)

lereis
lereis

Reputation: 24

The keyword you search is renameTo which is documented here: https://docs.oracle.com/javase/7/docs/api/java/io/File.html

In your case it would look like:

dest.renameTo(newFile)

And watch out: "newFile" is not just the filename, but an File Object. For further information you can read the provided documentation.

Upvotes: 1

Related Questions