Files.copy renaming files
<p>Was hoping you could look at some code for me:</p>

<pre><code>public void Copy(Path sourcepath,
        Path targetpath) throws IOException {

    DateFormat dateFormat = new SimpleDateFormat("yyyymmdd");


    File origfile = targetpath.toFile(); // Changes targetpath to file
    String name = origfile.getName(); // Gets the name of the file to be updated
    File file1 = new File(targetpath.toString() + "." + dateFormat.format(Calendar.getInstance().getTime())); //Create a new file instance with the name of the old file + date
    origfile.renameTo(file1); //Rename the original file
    origfile.createNewFile(); //Backup the original file
    Files.delete(targetpath); //Delete the original file
    Files.copy(sourcepath, targetpath);
}
</code></pre>

<p>Now everything works, the backing up works and the copying works. My original intention was to rename file being copied to the file being backed up. (hence the string name = origfile.getName();</p>

<p>This was my code:</p>

<pre><code>File file2 = new File(name);
File srcfile = sourcepath.toFile();
srcfile.renameTo(file2);
</code></pre>

<p>Now, that worked up to a point, after a while I started getting IOException errors, so after a few hours of struggling. I gave up and just deleted that renaming part.</p>

<p>Lo and behold it still renames the file when being copied.</p>

<p>Now my question: Does Files.copy do it? Is there some mysterious thing happening here? It does EXACTLY what I want it to do, but I am baffled as hell. Why is my code working?</p>

<p>and yes I want to know, in case it breaks or stops working. I can't have something work and not know why!</p>

<p>EDIT:</p>

<p>Sorry was in a bit of a rush when posting, let me pose my question a bit more clearer:</p>

<p>my intention was to have my sourcepath renamed to the original name of the file that is being backed up. I had code to rename it, but it threw an IOException so I deleted it. I only used Files.Copy, so I assumed sourcepath would retain it's original value, and just copy for each instance in a for loop I have. But no, it renames perfectly to the original of each file being backed up. Where and how?</p>
