Best way of Deleting Nested files/directories recursivley.


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class JavaHunterFile {

    public static void main(String[] args) {
        try {
            File file1 = new File("C:\\JavaHunter");
            File file2 = new File("C:\\Taher");
            copyFilesRecursively(file1, file2);
            File file = new File("C:\\Taher\\JavaHunter");
            if (file.exists()) {
                System.out.println("Del dir : " + delFileRecursively(file));
            }
        } catch (IOException ex) {
            System.out.println("Exception : " + ex);
        }
    }

    public static boolean delFileRecursively(File root) {
        if (root != null && root.exists()) {
            if (root.isDirectory()) {
                File[] childFiles = root.listFiles();
                if (childFiles != null) {
                    for (int i = 0; i < childFiles.length; i++) {
                        delFileRecursively(childFiles[i]);
                    }
                }
            }
            return root.delete();
        }
        return false;
    }

    private static void copyFilesRecursively(File src, File dest) throws IOException {
        if (src.isDirectory()) {
            dest.mkdir();
            File[] entries = src.listFiles();
            if (entries == null) {
                throw new IOException("No files in directory: " + src);
            }
            for (int i = 0; i < entries.length; i++) {
                File file = entries[i];
                copyFilesRecursively(file, new File(dest, file.getName()));
            }
        } else if (src.isFile()) {
            try {
                dest.createNewFile();
            } catch (IOException ex) {
                IOException ioex = new IOException("Failed creating file: " + dest);
                ioex.initCause(ex);
                throw ioex;
            }
            copy(src, dest);
        } else {
            System.out.println("Neither a file nor a directory");
        }
    }

    public static int copy(File fileIn, File fileOut) throws IOException {
        InputStream in = new BufferedInputStream(new FileInputStream(fileIn));
        OutputStream out = new BufferedOutputStream(new FileOutputStream(fileOut));
        try {
            int byteCount = 0;
            byte[] buffer = new byte[4096];
            int bytesRead = -1;
            while ((bytesRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytesRead);
                byteCount += bytesRead;
            }
            out.flush();
            return byteCount;
        } finally {
            try {
                in.close();
            } catch (IOException ex) {
            }
            try {
                out.close();
            } catch (IOException ex) {
            }
        }
    }
}
Advertisements
This entry was posted in Core JAVA. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s