dir.java

来自「plugin for eclipse」· Java 代码 · 共 136 行

JAVA
136
字号
package isis.commons.fs;

import java.io.File;
/**
 * Some basic directory access functions, most of which is actually available through java.io.File.
 * @author sallai
 */
public class Dir {
	/**
	 * Create a directory.
	 * @param f directory
	 * @return true if success, false otherwise.
	 */
	public static boolean mkDir(File f) {
		return f.mkdir();
	}

	/**
	 * Create a directory.
	 * @param s Name of directory.
	 * @return true if success, false otherwise.
	 */
	public static boolean mkDir(String s) {
		File f = new File(s);
		return mkDir(f);
	}

	/**
	 * Create a directory and all parent directories, if necessary.
	 * @param f directory
	 * @return true if success, false otherwise.
	 */
	public static boolean mkDirs(File f) {
		return f.mkdirs();
	}
	
	/**
	 * Create a directory and all parent directories, if necessary.
	 * @param s directory
	 * @return true if success, false otherwise.
	 */	
	public static boolean mkDirs(String s) {
		File f = new File(s);
		return mkDirs(f);
	}

	/**
	 * Check if a directory exists. 
	 * @param f directory
	 * @return true if exists, false if not directory or does not exist.
	 */
	public static boolean isDir(File f) {
		return f.isDirectory();
	}
	
	/**
	 * Check if a directory exists. 
	 * @param s directory
	 * @return true if exists, false if not directory or does not exist.
	 */
	public static boolean isDir(String s) {
		File f = new File(s);
		return isDir(f);
	}
	
	/**
	 * Remove a directory, optionally recursively.
	 * @param s directory
	 * @param recursive recursive removal
	 * @return true if success, false otherwise.
	 */
	public static boolean rmDir(String s, boolean recursive) {
		File f = new File(s);
		if(isDir(f)) {
			return rmDir(f, recursive);
		} else {
			return true;
		}
	}

	/**
	 * Remove a directory, optionally recursively.
	 * @param f directory
	 * @param recursive recursive removal
	 * @return true if success, false otherwise.
	 */
	public static boolean rmDir(File f, boolean recursive) {
		boolean success = true;
		
		if(recursive) {
			success = clearDir(f);
		}
		
		if(success) {
			return f.delete();
		} else {
			return false;
		}
	}
	
	/**
	 * Clear the contents of a directory.
	 * @param s directory
	 * @return true if success, false otherwise.
	 */
	public static boolean clearDir(String s) {
		File f = new File(s);
		return clearDir(f);
	}

	/**
	 * Clear the contents of a directory.
	 * @param f directory
	 * @return true if success, false otherwise.
	 */
	public static boolean clearDir(File f) {
		boolean rval = true;
		
        if (isDir(f)) {
            String[] children = f.list();
            for (int i=0; i<children.length; i++) {
                boolean success = rmDir(new File(f, children[i]), true);
                if (!success) {
                    rval = false;
                }
            }
        }
        return rval;
    }	

	/**
	 * Private constructor.
	 */
	private Dir() {}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?