Difference between revisions of "Copy a directory"

From CodeCodex

m (Reverted edits by 80.97.12.51 (Talk); changed back to last version by Nostromo)
(Ruby)
Line 22: Line 22:
 
</pre>
 
</pre>
 
* Original Source: [http://javaalmanac.com/egs/java.io/pkg.html?l=rel#Files The Java Developers Almanac 1.4]
 
* Original Source: [http://javaalmanac.com/egs/java.io/pkg.html?l=rel#Files The Java Developers Almanac 1.4]
 +
 +
===Ruby===
 +
Copies a file system entry src to dest. If src is a directory, this method copies its contents recursively.
 +
<pre>
 +
require 'fileutils'
 +
 +
FileUtils.copy_entry src, dest
 +
</pre>
 +
 
[[Category:Java]]
 
[[Category:Java]]
 +
[[Category:Ruby]]
 
[[Category:I/O]]
 
[[Category:I/O]]

Revision as of 06:54, 19 July 2010

Implementations

Java

    // Copies all files under srcDir to dstDir.
    // If dstDir does not exist, it will be created.
    public void copyDirectory(File srcDir, File dstDir) throws IOException {
        if (srcDir.isDirectory()) {
            if (!dstDir.exists()) {
                dstDir.mkdir();
            }
    
            String[] children = srcDir.list();
            for (int i=0; i<children.length; i++) {
                copyDirectory(new File(srcDir, children[i]),
                                     new File(dstDir, children[i]));
            }
        } else {
            // This method is implemented in e1071 Copying a File
            copyFile(srcDir, dstDir);
        }
    }

Ruby

Copies a file system entry src to dest. If src is a directory, this method copies its contents recursively.

require 'fileutils'

FileUtils.copy_entry src, dest