Tuesday, July 24, 2012

java - copy directory


surprisingly copy file in Java <7 copies a file, not 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 Copying a File
            // copyFile(srcDir, dstDir);
        }
    }

No comments:

Post a Comment