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]; /**...
### Java中用FileUtil实现文件读写 ...通过上述介绍可以看出,`FileUtil` 工具类在Java项目中对于简化文件操作方面具有重要的作用。开发者可以根据具体需求进一步扩展此类的功能,以便更好地服务于项目的需求。
Java文件操作工具类fileUtil实例 Java文件操作工具类fileUtil实例是一个提供了多种文件操作方法的工具类,包括文件读取、增加、删除、修改、复制等操作。该工具类可以帮助开发者快速实现文件操作功能,提高开发效率...
[工具类] 文件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...
文件工具类FileUtil是一个专门为Android开发提供帮助的实用类,它能够简化对文件的操作,使得开发者能更专注于业务逻辑,而不必重复编写文件操作的代码。下面详细介绍FileUtil中包含的一些关键功能点: 1. 读取raw...
在Java编程中,`FileUtil`通常是一个自定义的工具类,用于封装常见的文件操作,以便在项目中方便地处理文件。这个类可以提供一系列静态方法,帮助开发者执行读写文件、创建、删除、移动、复制文件等任务,极大地提高...
Java中的`File`类是Java I/O流体系中不可或缺的一部分,它是用来操作...结合`FileUtils`这样的辅助工具类,我们可以编写出更简洁、易维护的代码。在实际开发中,还应考虑异常处理和错误恢复策略,以确保程序的健壮性。
fileutil工具类 处理文件流工具 private static File file; /** * 判断文件是否存在 * * @param path * 文件路径 * @return boolean */ public static boolean fileIsExists(String path) { if (path ==...
在Java编程语言中,`FileUtil`类通常是由开发者自定义的一个工具类,用于封装对文件和目录的操作,以提供更便捷、安全的文件处理功能。这个类可能会包含各种静态方法,用来创建、读取、写入、删除、移动、复制文件...
在Java编程中,工具类(Util Class)是包含各种实用函数的静态类,它们提供了一种简化常见任务的方法。在给定的`UtilClass`中,我们有五个主要的工具类:`StringUtil`、`FileUtil`、`ConnectDB`、`DateUtil`和`...
在本例中,我们关注的是名为`FileUtil`的工具类,它包含了多个与文件操作相关的静态方法。以下是对`FileUtil`类及其相关知识点的详细解释: 1. **环境检查**: 在进行SD卡操作之前,`FileUtil`会检查SD卡是否已...
D:\002 我的工具类\001 流\文件操作整体\FileUtil.java D:\002 我的工具类\001 流\文件操作整体\valid.java D:\002 我的工具类\001 流\文件操作整体2 D:\002 我的工具类\001 流\文件操作整体2\Charsets.java D:\002 ...
在Java编程领域,工具类(Utility Class)是程序员日常工作中不可或缺的一部分。这些类通常包含一组静态方法,用于执行特定的任务,比如字符串处理、日期时间操作、集合操作等,旨在提高代码复用性和开发效率。本...
8. **文件操作工具类(FileUtil)**:提供文件的创建、删除、移动、复制等操作,简化文件操作。例如`createFileIfNotExists()`用于创建文件(如果文件不存在),`deleteFile()`用于删除文件。 9. **网络工具类...
Hutool提供了FileUtil工具类,支持文件的创建、删除、复制、移动等基本操作,还包含了文件的读写、查找、遍历等功能。例如,你可以轻松实现文件的复制: ```java FileUtil.copy(new File("源文件路径"), new File(...
FileUtil工具类主要处理文件和目录的操作,简化了Java的I/O操作。它可能包含如读取文件内容、写入文件、创建或删除文件/目录等方法。例如,`readFileToString`可以用来读取整个文件并返回一个字符串,而`...