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 递归获取一个文件夹(及其子文件夹)下所有文件 获取扩展名 (doc/docx/jpg等) 判断是否是图片 判断是否是压缩包 是否是word文档 是否是excel
import java.io.*; /** * FileUtil. Simple file operation class. * * @author BeanSoft * */ public class FileUtil { /** * The buffer. */ protected static byte buf[] = new byte[1024]; /**...
[工具类] 文件FileUtil.java [工具类] 通信客户端simpleClient.java [工具类] 通信服务端simpleServer.java [工具类] 框架StringUtil.java [工具类] 时间Time.java [工具类] 时间工具TimeUtil.java [工具类] 连...
[工具类] 文件FileUtil.java [工具类] 通信客户端simpleClient.java [工具类] 通信服务端simpleServer.java [工具类] 框架StringUtil.java [工具类] 时间Time.java [工具类] 时间工具TimeUtil.java [工具类] 连...
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针对文件进行读取、增加、删除、修改、复制等操作的相关实现技巧,需要的朋友可以参考下
从输入流中读取string,新建一个文件并写入内容,复制一个目录及其子目录、文件到另外一个目录 ,递归删除目录下的所有文件及子目录下所有文件,读取文本文件内容,以行的形式读取....
fileutil工具类 处理文件流工具 private static File file; /** * 判断文件是否存在 * * @param path * 文件路径 * @return boolean */ public static boolean fileIsExists(String path) { if (path ==...
D:\002 我的工具类\001 流\文件操作整体\FileUtil.java D:\002 我的工具类\001 流\文件操作整体\valid.java D:\002 我的工具类\001 流\文件操作整体2 D:\002 我的工具类\001 流\文件操作整体2\Charsets.java D:\002 ...
本文实例讲述了Android开发中的文件操作工具类FileUtil。分享给大家供大家参考,具体如下: package com.ymerp.android.tools; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java...
包含各种工具类文件如ChangePinYin.java、CollectionUtil.java、DateUtil.java、DBConnectionUtil.java、FileUtil.java、FtpUtil.java、HttpClientUtil.java、MathUtil.java、MD5Util.java、StringUtil.java、...
public class FileUtil { protected static Logger log = LoggerFactory.getLogger(FileUtil.class); /** * 压缩文件 * @param inputFileName 要压缩的文件或文件夹路径,例如:c:\\a.txt,c:\\a\ * ...
文件上传的工具类。里面包括一些文件的下载以及上传。都是封装好的一些方法,很好用的。
对文件的读写工具类,支持文件夹的写出和写入。。。。可桌面路劲写出写入
文件工具类,文件常用方法,获得文件大小、文件大小转换。 MD5Util.java MD5加密工具类。 RegexUtil.java 常用正则表达式工具类。 ...
解决java 运行 MapReduce 相关代码时报权限错误的问题。
依赖与commons.lang的日期通用工具类FileUtil ----->文件通用工具类IOUtil ----->输入输出通用工具类JVMRandom ----->随机数通用工具类NumberUtil ----->数值通用工具类ObjectUtil ----->对象通用...
StringUtil.toUncapitalize, && 修复转换BUGv1.1.41.[BUG] FileUtil 修复获取文件名和后缀名方法参数非存在的文件或目录抛出异常2.[ADDED] FileUtil 添加创建文件目录存在是否跳过控制3.[ADDED] FileUtil 添加...