java中实用的File工具类

和File有关的工具类

package arithmetic;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

/**
 * 该类封装和File相关的操作方法
 * @author liujd
 *
 */
public class FileUtil {

	//私有构造方法的目的是防止被实例化
	private FileUtil() {
	}

	/**
	 * 判断文件是否存在
	 * @param file
	 * @return
	 */
	public static boolean isExist(File file) {
		return file.exists();
	}
	/**
	 * 判断文件是存在(根据文件名来判断)
	 * @param fileName
	 * @return
	 */
	public static boolean isExist(String fileName) {
		File file = new File(fileName);
		return isExist(file);
	}
	/**
	 * 创建新的空文件
	 * @param file
	 * @return
	 * @throws IOException
	 */
	public static boolean createNewFile(File file) throws IOException {
		return file.createNewFile();
	}
	/**
	 * 根据文件名创建新的文件
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public static boolean createNewFile(String fileName) throws IOException {
		File file = new File(fileName);
		return createNewFile(file);
	}
	/**
	 * 创建指定目录
	 * @param file
	 * @return
	 * 这里说一下File类中mkdir()和mkdirs()的区别
	 * mkdir()是创建此抽象路径名称指定的目录,并且只能创建一级的目录 且需要存在父目录
	 * mkdirs()是创建此抽象路径指定的目录 包括所有必须但是不存在的父目录   并且可以创建多级目录 无论是否存在父目录
	 */
	public static boolean makeDictionary (File file) {
		File parent = file.getParentFile();
		if(parent != null) {
			return parent.mkdirs();
		}
		return false;
	}
	/**
	 * 创建指定目录(根据名称)
	 * @param fileName
	 * @return
	 */
	public static boolean makeDictionary (String fileName) {
		File file = new File(fileName);
		return makeDictionary(file);
	}
	/**
	 * 清空指定的目录
	 * @param dictionary
	 * @return
	 */
	public static boolean emptyDictionary(File dictionary) {
		boolean result = false;
		//获取目录下的文件或目录列表
		File[] files = dictionary.listFiles();
		//比那里列表 如果每一项删除了 返回true
		for(int i = 0 ; i<files.length ; i++) {
			if(files[i].delete()) 
				result = true;
		}
		return result;
	}
	/**
	 * 清空指定目录(根据指定名字)
	 * @param dicName
	 * @return
	 */
	public static boolean emptyDictionary(String dicName) {
		File file = new File(dicName);
		return emptyDictionary(file);
	}
	/**
	 * 删除目录以及目录下的所有内容
	 * @param dir
	 * @return
	 */
	public static void deleteDictionary(File dir) {
		if(dir == null || !dir.isDirectory())
			throw new IllegalArgumentException("你输入的不是一个目录名");
		//获取列表
		File[] files = dir.listFiles();
		//遍历列表  并且删除列表
		for(int i=0;i<files.length;i++) {
			//如果列表的某一项是一个目录 就应该递归调用所写方法
			if(files[i].isDirectory()) {
				emptyDictionary(files[i]);
			}else {
				//如果是文件 则直接删除
				if(files[i].delete())
					System.out.println("成功删除文件"+files[i].getName());
			}
		}
		//删除根目录
		if(dir.delete())
			System.out.println("成功删除目录"+dir.getName());
	}
	/**
	 * 列出目录下所有列表
	 * @param dir
	 * @return
	 */
	public static File[] listAllFiles(File dir) {
		if(dir == null || !dir.isDirectory()) 
			throw new IllegalArgumentException("你输入的不是一个目录名");
		return dir.listFiles();
		
	}
	/**
	 * 文字形式列出目录下所有的子目录以及子文件
	 * @param dir
	 */
	public static void listFiles(File dir) {
		//获取目录下的列表
		File[] files = listAllFiles(dir);
		System.out.println("根目录"+dir.getName());
		//遍历列表
		for(int i = 0; i<files.length;i++) {
			//如果是目录的话 就继续递归
			if(files[i].isDirectory()) {
				listFiles(dir);
				System.out.println("查找到目录"+files[i].getName());
			}else {
				System.out.println("  查找到文件"+files[i].getName());
			}
		}
	}
	/**
	 * 根据文件路径名获取文件名字
	 * @param filePath
	 * @return
	 */
	public static String getFileName(String filePath) {
		File file = new File(filePath);
		return file.getName();
	}
	/**
	 * 根据文件名字获取文件的绝对路径
	 * @param fileName
	 * @return
	 */
	public static String getFilePath(String fileName) {
		File file = new File(fileName);
		return file.getAbsolutePath();
	}
	/**
	 * 获取文件的后缀
	 * @param file
	 * @return
	 */
	public static String getFileType(File file) {
		//获取文件名字
		String fileName = file.getName();
		//获取“.”所在的索引
		int point = fileName.lastIndexOf('.');
		//获取名字长度
		int nameLength = fileName.length();
		//如果点所在位置等于-1(其实就是不存在) 或者点的位置在最后  就直接返回一个空
		if(point == -1 || point == nameLength - 1 ) {
			return "";
		}else {
			return fileName.substring(point+1, nameLength);
		}
	}
	/**
	 * 根据文件名获取它的后缀
	 * @param fileName
	 * @return
	 */
	public static String getFileType(String fileName) {
		File file = new File(fileName);
		return getFileType(file);
	}
	/**
	 * 从指定文件中读取内容
	 * @param path
	 * @return
	 */
	public static String getFileContent(String path) {
		StringBuilder fileContent = new StringBuilder("");
		File file = new File(path);
			try {
				if(file.exists()) {
					//建立BufferedReader对象
					BufferedReader br = new BufferedReader(new FileReader(file));
					//从文件中读取一行
					String line = br.readLine();
					while(line != null) {
						fileContent.append(line).append("\n");
					}
					br.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return fileContent.toString();
	}
	/**
	 * 拷贝文件
	 * @param in
	 * @param out
	 * @return
	 */
	public static boolean copyFile(File in , File out) {
		try {
			FileInputStream fis = new FileInputStream(in);
			FileOutputStream fos = new FileOutputStream(out);
			byte[] buf = new byte[1024];
			int i = 0;
			//将数据读到buf里面
			while((i = fis.read(buf)) != -1) {
				//再将buf里面的数据写出来
				fos.write(buf, 0, i);
			}
			fis.close();
			fos.close();
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		
	}
	/**
	 * 根据文件名来拷贝
	 * @param inFile
	 * @param outFile
	 * @return
	 */
	public static boolean copyFile(String inFile , String outFile) {
		File in = new File(inFile);
		File out = new File(outFile);
		return copyFile(in, out);
	}
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值