`

java 文件工具类 FileUtil 备忘笔记

阅读更多

package com.woyo.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;

import org.apache.log4j.Logger;
/**
 * 过滤指路歌曲文件
 * @author dylan_xu
 * @date Mar 11, 2012
 * @modified by
 * @modified date
 * @since JDK1.6
 * @see com.woyo.utils.FileUtil
 */
public class FileUtil {
	public static Logger logger = Logger.getLogger(FileUtil.class);
	public static Set<String> sets = new HashSet<String>();

	public static void main(String[] args) {
		refreshFileList("G:\\Music");
		//moveFolder("G:\\music\\周杰伦", "E:\\Kugou");
	}

	/**
	 * 过滤MP3文件
	 * 
	 * @param strPath
	 */
	public static void refreshFileList(String strPath) {
		File dir = new File(strPath);
		File[] files = dir.listFiles();
		if (files == null) {
			return;
		}
		for (int i = 0; i < files.length; i++) {
			if (files[i].isDirectory()) {
				refreshFileList(files[i].getAbsolutePath());
			} else {
				String strFilePath = files[i].getAbsolutePath().toLowerCase();
				String strName = files[i].getName();
				if (strName.endsWith(".mp3")) {
					boolean bFlag = sets.add(strName);
					if (bFlag == Boolean.FALSE) {
						// 删除重复文件
						removeFile(strFilePath);
					}
				}
				// System.out.println("FILE_PATH:" + strFilePath + "|strName:" +
				// strName);
			}
		}
	}

	/**
	 * 创建文件夹
	 * 
	 * @param strFilePath
	 *            文件夹路径
	 */
	public boolean mkdirFolder(String strFilePath) {
		boolean bFlag = false;
		try {
			File file = new File(strFilePath.toString());
			if (!file.exists()) {
				bFlag = file.mkdir();
			}
		} catch (Exception e) {
			logger.error("新建目录操作出错" + e.getLocalizedMessage());
			e.printStackTrace();
		}
		return bFlag;
	}

	public boolean createFile(String strFilePath, String strFileContent) {
		boolean bFlag = false;
		try {
			File file = new File(strFilePath.toString());
			if (!file.exists()) {
				bFlag = file.createNewFile();
			}
			if (bFlag == Boolean.TRUE) {
				FileWriter fw = new FileWriter(file);
				PrintWriter pw = new PrintWriter(fw);
				pw.println(strFileContent.toString());
				pw.close();
			}
		} catch (Exception e) {
			logger.error("新建文件操作出错" + e.getLocalizedMessage());
			e.printStackTrace();
		}
		return bFlag;
	}

	/**
	 * 删除文件
	 * 
	 * @param strFilePath
	 * @return
	 */
	public static boolean removeFile(String strFilePath) {
		boolean result = false;
		if (strFilePath == null || "".equals(strFilePath)) {
			return result;
		}
		File file = new File(strFilePath);
		if (file.isFile() && file.exists()) {
			result = file.delete();
			if (result == Boolean.TRUE) {
				logger.debug("[REMOE_FILE:" + strFilePath + "删除成功!]");
			} else {
				logger.debug("[REMOE_FILE:" + strFilePath + "删除失败]");
			}
		}
		return result;
	}

	/**
	 * 删除文件夹(包括文件夹中的文件内容,文件夹)
	 * 
	 * @param strFolderPath
	 * @return
	 */
	public static boolean removeFolder(String strFolderPath) {
		boolean bFlag = false;
		try {
			if (strFolderPath == null || "".equals(strFolderPath)) {
				return bFlag;
			}
			File file = new File(strFolderPath.toString());
			bFlag = file.delete();
			if (bFlag == Boolean.TRUE) {
				logger.debug("[REMOE_FOLDER:" + file.getPath() + "删除成功!]");
			} else {
				logger.debug("[REMOE_FOLDER:" + file.getPath() + "删除失败]");
			}
		} catch (Exception e) {
			logger.error("FLOADER_PATH:" + strFolderPath + "删除文件夹失败!");
			e.printStackTrace();
		}
		return bFlag;
	}

	/**
	 * 移除所有文件
	 * 
	 * @param strPath
	 */
	public static void removeAllFile(String strPath) {
		File file = new File(strPath);
		if (!file.exists()) {
			return;
		}
		if (!file.isDirectory()) {
			return;
		}
		String[] fileList = file.list();
		File tempFile = null;
		for (int i = 0; i < fileList.length; i++) {
			if (strPath.endsWith(File.separator)) {
				tempFile = new File(strPath + fileList[i]);
			} else {
				tempFile = new File(strPath + File.separator + fileList[i]);
			}
			if (tempFile.isFile()) {
				tempFile.delete();
			}
			if (tempFile.isDirectory()) {
				removeAllFile(strPath + "/" + fileList[i]);// 下删除文件夹里面的文件
				removeFolder(strPath + "/" + fileList[i]);// 删除文件夹
			}
		}
	}

	public static void copyFile(String oldPath, String newPath) {
		try {
			int bytesum = 0;
			int byteread = 0;
			File oldfile = new File(oldPath);
			if (oldfile.exists()) { // 文件存在时
				InputStream inStream = new FileInputStream(oldPath); // 读入原文件
				FileOutputStream fs = new FileOutputStream(newPath);
				byte[] buffer = new byte[1444];
				while ((byteread = inStream.read(buffer)) != -1) {
					bytesum += byteread; // 字节数 文件大小
					System.out.println(bytesum);
					fs.write(buffer, 0, byteread);
				}
				inStream.close();
				logger.debug("[COPY_FILE:" + oldfile.getPath() + "复制文件成功!]");
			}
		} catch (Exception e) {
			System.out.println("复制单个文件操作出错 ");
			e.printStackTrace();
		}
	}

	public static void copyFolder(String oldPath, String newPath) {
		try {
			(new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
			File a = new File(oldPath);
			String[] file = a.list();
			File temp = null;
			for (int i = 0; i < file.length; i++) {
				if (oldPath.endsWith(File.separator)) {
					temp = new File(oldPath + file[i]);
				} else {
					temp = new File(oldPath + File.separator + file[i]);
				}
				if (temp.isFile()) {
					FileInputStream input = new FileInputStream(temp);
					FileOutputStream output = new FileOutputStream(newPath
							+ "/ " + (temp.getName()).toString());
					byte[] b = new byte[1024 * 5];
					int len;
					while ((len = input.read(b)) != -1) {
						output.write(b, 0, len);
					}
					output.flush();
					output.close();
					input.close();
					logger.debug("[COPY_FILE:" + temp.getPath() + "复制文件成功!]");
				}
				if (temp.isDirectory()) {// 如果是子文件夹
					copyFolder(oldPath + "/ " + file[i], newPath + "/ "
							+ file[i]);
				}
			}
		} catch (Exception e) {
			System.out.println("复制整个文件夹内容操作出错 ");
			e.printStackTrace();
		}
	}

	public static void moveFile(String oldPath, String newPath) {
		copyFile(oldPath, newPath);
		//removeFile(oldPath);
	}

	public static void moveFolder(String oldPath, String newPath) {
		copyFolder(oldPath, newPath);
		//removeFolder(oldPath);
	}
}
 
分享到:
评论

相关推荐

    java文件工具类FileUtil

    java文件工具类FileUtil 递归获取一个文件夹(及其子文件夹)下所有文件 获取扩展名 (doc/docx/jpg等) 判断是否是图片 判断是否是压缩包 是否是word文档 是否是excel

    Java文件处理工具类--FileUtil

    import java.io.*; /** * FileUtil. Simple file operation class. * * @author BeanSoft * */ public class FileUtil { /** * The buffer. */ protected static byte buf[] = new byte[1024]; /**...

    Java文件操作工具类fileUtil实例【文件增删改,复制等】

    Java文件操作工具类fileUtil实例 Java文件操作工具类fileUtil实例是一个提供了多种文件操作方法的工具类,包括文件读取、增加、删除、修改、复制等操作。该工具类可以帮助开发者快速实现文件操作功能,提高开发效率...

    30个java工具类

    [工具类] 文件FileUtil.java [工具类] 通信客户端simpleClient.java [工具类] 通信服务端simpleServer.java [工具类] 框架StringUtil.java [工具类] 时间Time.java [工具类] 时间工具TimeUtil.java [工具类] 连...

    【强2】30个java工具类

    [工具类] 文件FileUtil.java [工具类] 通信客户端simpleClient.java [工具类] 通信服务端simpleServer.java [工具类] 框架StringUtil.java [工具类] 时间Time.java [工具类] 时间工具TimeUtil.java [工具类] 连...

    关于文件操作的工具类 -- FileUtil

    java.io.File myFilePath = new java.io.File(filePath); if (!myFilePath.exists()) { myFilePath.mkdir(); } } catch (Exception e) { System.out.println("新建目录操作出错"); e.printStackTrace...

    一个常用的Java文件操作类FileUtil.java源码下载

    一个常用的Java文件操作类FileUtil.java源代码,类中的所有方法都是静态方法,不需要生成此类的实例,这些Java文件操作类主要有修改文件的最后访问时间、判断指定的文件是否存在、创建指定的目录、清空指定目录中的...

    值得分享的超全文件工具类FileUtil

    文件工具类FileUtil是一个专门为Android开发提供帮助的实用类,它能够简化对文件的操作,使得开发者能更专注于业务逻辑,而不必重复编写文件操作的代码。下面详细介绍FileUtil中包含的一些关键功能点: 1. 读取raw...

    Android开发中的文件操作工具类FileUtil完整实例

    Android文件操作工具类FileUtil详解 在Android开发中,文件操作是非常重要的一部分,涵盖了文件的获取、遍历、搜索、复制、删除、判断等多种功能。为了方便开发者更好地进行文件操作,今天我们将详细介绍Android...

    FileUtil(文件操作工具类)

    在Java编程中,`FileUtil`通常是一个自定义的工具类,用于封装常见的文件操作,以便在项目中方便地处理文件。这个类可以提供一系列静态方法,帮助开发者执行读写文件、创建、删除、移动、复制文件等任务,极大地提高...

    java File文件处理工具类

    Java中的`File`类是Java I/O流体系中不可或缺的一部分,它是用来操作文件和目录的基础类。`File`对象代表了文件和目录路径名的抽象表示。在这个详细的讲解中,我们将深入探讨`File`类的各种功能,包括创建、读取、...

    文件操作工具 FileUtil

    在IT领域,文件操作是日常开发中的重要环节。"FileUtil"是一个基于Qt框架的库,专门用于处理各种类型的文件,包括CSV、DBF、...在实际项目中,可以根据需要选择相应的工具类,通过调用其方法来完成文件的读写和操作。

    fileutil工具类 处理文件流工具

    fileutil工具类 处理文件流工具 private static File file; /** * 判断文件是否存在 * * @param path * 文件路径 * @return boolean */ public static boolean fileIsExists(String path) { if (path ==...

    FileUtil类文件整理

    在Java编程语言中,`FileUtil`类通常是由开发者自定义的一个工具类,用于封装对文件和目录的操作,以提供更便捷、安全的文件处理功能。这个类可能会包含各种静态方法,用来创建、读取、写入、删除、移动、复制文件...

    Java常用工具类UtilClass

    在Java编程中,工具类(Util Class)是包含各种实用函数的静态类,它们提供了一种简化常见任务的方法。在给定的`UtilClass`中,我们有五个主要的工具类:`StringUtil`、`FileUtil`、`ConnectDB`、`DateUtil`和`...

    我积攒的java工具类 基本满足开发需要的工具类

    D:\002 我的工具类\001 流\文件操作整体\FileUtil.java D:\002 我的工具类\001 流\文件操作整体\valid.java D:\002 我的工具类\001 流\文件操作整体2 D:\002 我的工具类\001 流\文件操作整体2\Charsets.java D:\002 ...

    Java工具类合集

    在Java编程领域,工具类(Utility Class)是程序员日常工作中不可或缺的一部分。这些类通常包含一组静态方法,用于执行特定的任务,比如字符串处理、日期时间操作、集合操作等,旨在提高代码复用性和开发效率。本...

Global site tag (gtag.js) - Google Analytics