The Java Developers Almanac 1.4

 
Webexampledepot.com

   
Home > List of Packages > java.io  [37 examples] > Directories  [7 examples]

e1072. Copying a Directory

    // 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);
        }
    }

 Related Examples
e28. Getting the Current Working Directory
e29. Creating a Directory
e30. Deleting a Directory
e31. Listing the Files or Subdirectories in a Directory
e32. Listing the File System Roots
e33. Traversing the Files and Directories Under a Directory

See also: Encodings    Filenames and Pathnames    Files    Parsing    Reading and Writing    Serialization   


© 2002 Addison-Wesley.