Java文件夹操作

Java文件夹操作

javaIO中File.isfile()方法判断文件路径

如果这个文件路径中的文件是存在的而且是一个txt的标准文件,那么就应该返回true

package com.tang.utils;

import java.io.File;

/**
 * @author 小汤同学 文件操作
 */
public class FileUtils {

	/**
	 * 创建文件夹通用
	 * 
	 * @return
	 * @throws Exception
	 */
	public static boolean create(String strPath) throws Exception {
		if (strPath.indexOf(".") > 0) {
			System.out.println("执行第一个");
			return createFileAndDirs(strPath);
		} else {
			System.out.println("执行第二个");
			return createDirs(strPath);
		}
	}

	/**
	 * 创建文件夹,仅创建文件夹
	 * 
	 * @param strPath(路径名E:\\a\\aa\\aaa)
	 * @return 创建成功或已存在true、创建失败false
	 * @throws Exception
	 */
	public static boolean createDirs(String strPath) throws Exception {
		File file = new File(strPath);
		if (!file.exists()) {
			if (!file.mkdirs()) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 
	 * @param strPath(路径名带文件E:\\a\\aa\\aaa.txt)
	 * @return
	 * @throws Exception
	 */
	public static boolean createFileAndDirs(String strPath) throws Exception {
		// String strPath = "";
		File file = new File(strPath);
		boolean flag = true;
		if (!file.exists()) {
			File fileParent = file.getParentFile();
			if (!fileParent.exists()) {
				if (!fileParent.mkdirs()) {
					flag = false;// 创建失败
				}
			}
			if (!file.createNewFile()) {
				flag = false;// 创建图片失败
			}
		}
		return flag;
	}

	/**
	 * 删除文件,可以是文件或文件夹
	 * 
	 * @param fileName(要删除的文件名)
	 * @return 删除成功返回true,否则返回false
	 */
	public static boolean delete(String fileName) throws Exception {
		File file = new File(fileName);
		if (!file.exists()) {
			return false;
		} else {
			if (file.isFile())
				return deleteOneFile(fileName);
			else
				return deleteDirectory(fileName);
		}
	}

	/**
	 * 删除单个文件
	 * 
	 * @param fileName(要删除的文件名称)
	 * @return 单个文件删除成功返回true,失败返回false
	 */
	public static boolean deleteOneFile(String fileName) throws Exception {
		File file = new File(fileName);
		// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
		if (file.exists() && file.isFile()) {
			if (file.delete()) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	/**
	 * 删除目录及目录下的文件
	 *
	 * @param dir(要删除的目录的文件路径)
	 * @return 目录删除成功返回true,否则返回false
	 */
	public static boolean deleteDirectory(String dir) throws Exception {
		// 添加分隔符
		if (!dir.endsWith(File.separator))
			dir = dir + File.separator;
		File dirFile = new File(dir);
		// 如果dir对应的文件不存在,或者不是一个目录,则退出
		if ((!dirFile.exists()) || (!dirFile.isDirectory())) {
			return false;
		}
		boolean flag = true;
		// 删除文件夹中的所有文件包括子目录
		File[] files = dirFile.listFiles();
		for (int i = 0; i < files.length; i++) {
			System.out.println(files.toString());
			// 删除子文件
			if (files[i].isFile()) {
				flag = FileUtils.deleteOneFile(files[i].getAbsolutePath());
				if (!flag)
					break;
			}
			// 删除子目录
			else if (files[i].isDirectory()) {
				flag = FileUtils.deleteDirectory(files[i].getAbsolutePath());
				if (!flag)
					break;
			}
		}
		if (!flag) {
			System.out.println("删除目录失败!");
			return false;
		}
		// 删除当前目录
		if (dirFile.delete()) {
			System.out.println("删除目录" + dir + "成功!");
			return true;
		} else {
			return false;
		}
	}

}

猜你喜欢

转载自blog.csdn.net/qq_26786441/article/details/88835884