Java文件操做工具类

在工做中,对于文件的操做是必不可少的。在大多数的项目中都会遇到,若是每次进行对文件的操做都要去写一遍相关的代码,那会浪费不少时间。所以我把常常用到的操做抽取出来封装为一个工具类,用的时候调用就好了。
工具类代码以下:java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 文件操做工具类
 */
public class FileUtils {

	/**
	 *	 获取文件扩展名
	 * @param filename
	 * @return
	 */
	public static String getExtend(String filename) {
		if ((filename != null) && (filename.length() > 0)) {
			int i = filename.lastIndexOf('.');

			if ((i > 0) && (i < (filename.length() - 1))) {
				return (filename.substring(i+1)).toLowerCase();
			}
		}
		return "";
	}

	/**
	 * 文件复制
	 * @param inputFile 被复制文件
	 * @param outputFile 复制后的文件
	 * @throws FileNotFoundException
	 */
	public static void copyFile(String inputFile,String outputFile) throws FileNotFoundException{
		File sFile = new File(inputFile);
		if (!sFile.exists() || !sFile.isFile()) {
			System.out.println("错误: " + inputFile + "不存在!");
			return ;
		}
		File tFile = new File(outputFile);
		FileInputStream fis = new FileInputStream(sFile);
		FileOutputStream fos = new FileOutputStream(tFile);
		int temp = 0;  
		byte[] buf = new byte[10240];
		try {  
			while((temp = fis.read(buf))!=-1){   
				fos.write(buf, 0, temp);   
			}  
			System.out.println("--------成功复制文件---------"+inputFile);
		} catch (IOException e) { 
			System.out.println("文件流复制失败!");
			e.printStackTrace();  
		} finally{
			try {
				fis.close();
				fos.close();
			} catch (IOException e) {
				System.out.println("文件流关闭失败!");
				e.printStackTrace();
			}
		} 
	}
	/**
	 * 判断文件是否为图片
	 * @param filename 
	 * @return true 是,false 否
	 */
	public static boolean isPicture(String filename) {
		// 文件名称为空的场合
		if (isEmpty(filename)) {
			// 返回不和合法
			return false;
		}
		// 得到文件后缀名
		String tmpName = getExtend(filename);
		// 声明图片后缀名数组
		String imgeArray[][] = { { "bmp", "0" }, { "dib", "1" },
				{ "gif", "2" }, { "jfif", "3" }, { "jpe", "4" },
				{ "jpeg", "5" }, { "jpg", "6" }, { "png", "7" },
				{ "tif", "8" }, { "tiff", "9" }, { "ico", "10" } };
		// 遍历名称数组
		for (int i = 0; i < imgeArray.length; i++) {
			// 判断单个类型文件的场合
			if (imgeArray[i][0].equals(tmpName.toLowerCase())) {
				return true;
			}
		}
		return false;
	}

	
	/**
	 * 	删除绝对路径下的文件
	 * @param strFileName
	 * @return 若是删除成功true不然false
	 */
	public static boolean delete(String strFileName) {
		File fileDelete = new File(strFileName);
		if (!fileDelete.exists() || !fileDelete.isFile()) {
			System.out.println("错误: " + strFileName + "不存在!");
			return false;
		}
		System.out.println("--------成功删除文件---------"+strFileName);
		return fileDelete.delete();
	}
	
	/**
	 * 判断字符是否为空值
	 * @param object
	 * @return
	 */
	public static boolean isEmpty(Object object) {
		if (object == null) {
			return (true);
		}
		if (object.equals("")) {
			return (true);
		}
		if (object.equals("null")) {
			return (true);
		}
		return (false);
	}

	
	//测试方法
	public static void main(String[] args) throws FileNotFoundException {
		//获取文件扩展名
		//String extend = FileUtils.getExtend("E:\\test.jpg");
		//System.out.println(extend);
		//复制文件
		//FileUtils.copyFile("E:\\test.txt", "D:\\test.txt");
		//判断文件是否为图片
		//boolean isPicture = FileUtils.isPicture("E:\\test.jpg");
		//System.out.println(isPicture);
		boolean isDelete = FileUtils.delete("D:\\test.ftl");
		System.out.println(isDelete);
	}
}
相关文章
相关标签/搜索