Search code examples
javascriptrhino

Copying files in Javascript Rhino


I'm looking for the easiest solution to copy a file from /pathA/filename to /pathB/filename. No relative paths or file modifications are needed.

I'm trying to copy a file in Javascript Rhino. The code below for making a new file works with no issues:

        //Make destination file
        destinationDir = new java.io.File(filepath).mkdirs();
        localFile = new java.io.File(filepath,filename);
        localFile.createNewFile();

I just need to copy the file with no modifications, so there is no need for copying an input/output stream. Any suggestions? Many of the other related questions I've found are for java or javascript with html, neither of which give me an exact solution. I've tried Files.copy but can't seem to get it correct.


Solution

  • I guess you could do just as you would in Java:

    sourceFile = new java.io.File(/* [...] */);
    
    //Make destination file
    destinationDir = new java.io.File(filepath).mkdirs();
    localFile = new java.io.File(filepath,filename);
    localFile.createNewFile();
    
    java.nio.file.Files.copy(
        sourceFile.toPath(),
        localFile.toPath(),
        java.nio.file.StandardCopyOption.REPLACE_EXISTING
    );