javaでディレクトリのコピー作ったのなら削除もか。

ディレクトリのコピー作ったら、削除もだよね。

まあ、コピーなんかより簡単で、メモするまでも無いと思っていたけど、一応。

/**
 * @author ikki
 *
 */
class FileUtil {
	/**
	 * @param src 送信元
	 * @param dest 宛先
	 * @throws IOException
	 */
	public static void copyFile(File src,File dest) 
		throws IOException {
		
		if (src.isDirectory()) 
			copyDirectory(src,dest);
		else
			copyTransfer(src.getPath(),dest.getPath());
	}
	/**
	 * @param srcPath 送信元
	 * @param destPath 宛先
	 * @throws IOException
	 */
	public static void copyTransfer(final String srcPath,final String destPath) 
    	throws IOException {
    
	    FileChannel srcChannel = new
	        FileInputStream(srcPath).getChannel();
	    FileChannel destChannel = new
	        FileOutputStream(destPath).getChannel();
	    try {
	        srcChannel.transferTo(0, srcChannel.size(), destChannel);
	    } finally {
	        srcChannel.close();
	        destChannel.close();
	    }
	}
	/**
	 * @param srcPath 送信元
	 * @param destPath 宛先
	 * @throws IOException
	 */
	public static void copyDirectory(File src ,File dest) 
		throws IOException {
		
		if (src.isFile())
			copyFile(src,dest);
		if (!dest.exists())
			dest.mkdir();
		for (File ff : src.listFiles()) {
			
			String name = ff.getName();
			if (name.equals(".") || name.equals("..")) continue;
			File d2 = new File(dest.getPath()+"/"+ff.getName());
			
			if (ff.isDirectory())
				copyDirectory(ff,d2);
			else
				copyFile(ff,d2);
		}
	}
	
	/**
	 * @param f 削除するfile
	 */
	public static void delete(File f) {
		if (f.isDirectory())
			for (File ff : f.listFiles())
				delete(ff);
		f.delete();
	}
}