Thursday, July 1, 2010

Java Directory Examples


1.How to create directory or folder
2.How to delete directory or folder
3.How to copy directory or folder (including subdirectories and files)

1.How to create directory or folder

package io;

import java.io.File;

public class DirectoryExample {
public static void main(String[] args) {

/* The parent directory "C:/temp/" should be exists before
* creating directory "a" */
File directory = new File("C:/temp/a");
boolean created = directory.mkdir();
System.out.println(created);

/* IF parent directory "C:/temp/" does exists, then it creates all */
created = directory.mkdirs();
System.out.println(created);
}
}


2.How to delete directory or folder

package io;

import java.io.File;

public class DirectoryExample {
public static void main(String[] args) {
try {
boolean delete = DirectoryExample.delete(new File("C:/temp/a"));
System.out.println(delete);
} catch (Exception e) {
e.printStackTrace();
}

}

/* Recursively delete all files and sub directories under g directory. */
public static boolean delete(File dir) throws Exception {

// check if the directory(or file) exists
if (!dir.exists()) {
throw new Exception(dir.toString() + " does not exist");
}

// Delete all files and sub directories if current one is a directory
if (dir.isDirectory()) {
String[] list = dir.list();
for (int i = 0; i < list.length; i++) {
delete(new File(dir, list[i]));
}
}

// All files and sub directories have been deleted
// Now delete the given directory
return dir.delete();
}

}

3.How to copy directory or folder (including subdirectories and files)

package io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class DirectoryExample {
public static void main(String[] args) {
try {
boolean coiped = DirectoryExample.copyDirectory(new File("C:/temp/a"),new File("C:/temp/b"));
System.out.println(coiped);
} catch (Exception e) {
e.printStackTrace();
}

}

// Copies all files under srcDir to dstDir.
// If dstDir does not exist, it will be created.
public static boolean copyDirectory(File srcDir, File destDir) throws Exception {

// check whether the source directory(or file) exists
if (!srcDir.exists()) {
throw new Exception(srcDir.toString() + " does not exist");
}

if (srcDir.isDirectory()) {

// Create destination directory if does not exists
if (!destDir.exists()) {
destDir.mkdir();
}

// copy all sub directories and files
String[] children = srcDir.list();
for (int i = 0; i < children.length; i++) {
copyDirectory(new File(srcDir, children[i]), new File(destDir, children[i]));
}
} else {
// This method is implemented in Copying a File
copyFile(srcDir, destDir);
}
return true;
}

public static void copyFile(File srcFile, File destFile) throws Exception {

FileChannel srcChannel = null;
FileChannel dstChannel = null;
try {
srcChannel = new FileInputStream(srcFile).getChannel();
dstChannel = new FileOutputStream(destFile).getChannel();

// Copy file contents from source to destination
dstChannel.transferFrom(srcChannel, 0, srcChannel.size());

} catch (Exception e) {
throw new Exception(e.getMessage(), e);
} finally {
// Closing the channels
try {
if (srcChannel != null) {
srcChannel.close();
}
if (dstChannel != null) {
dstChannel.close();
}
} catch (IOException ioException) {
throw new Exception("Exception thrown while closing the channels inside finally!", ioException);
}
}
}
}

No comments:

Post a Comment