在此博客中用到了文件操作的工具类,可以连接
package cn.edu.hactcm.cfcms.utils;
import java.io.file;
import java.util.hashset;
import java.util.set;
import javax.swing.joptionpane;
/**
* 文件分类中用到的所有工具类
* cfms :computer files management system
* version :1.0 2013-3-3 上午02:28:40
*/
public class classifyfolderutils {
/**
* 在from 这个文件夹里创建 innerfoldername 这个文件夹,如果前者内部的文
* 件中包含关键字innerfoldername,那么把文件拷贝到innerfoldername文件夹里面去
*
* @param innerfoldername
* @param from
*/
public static void nameclassify(string innerfoldername,string from){
file fromfile = new file(from);
//如果选中的文件夹是一个文件夹,那么将不做任何操作
if (from.isempty()) {
return;
} else {
//获得文件里面的所有文件
file[] listfiles = fromfile.listfiles();
//如果选中的文件不是空文件,在这个文件里面创建要分类的文件
string newpath = from + file.separator + innerfoldername;
file newfile = new file(newpath);
for (int i = 0; i < listfiles.length; i++) {
if (listfiles[i].isdirectory()) {
//判断这个文件夹中是否有这个关键字,如果有的话就拷贝目录了到制定目录内了。
if (listfiles[i].getname().indexof(innerfoldername) != -1) {
//获得这个文件夹的名称,在指定的文件内创建这个目录
if (!newfile.exists()) {
newfile.mkdir();
}
fileoperationutils.copyfolder2folder(listfiles[i].getpath(), newpath);
} else {//如果没有就跳出这个循环
continue;
}
} else {
//判断这个文件是否含有名称关键字
string destfilepath = newpath + file.separator + listfiles[i].getname();
fileoperationutils.copyfiletofile(listfiles[i].getpath() , destfilepath);
} else {
}
}
}
joptionpane.showmessagedialog(null, "操作成功,请查看指定目录!","提示消息",joptionpane.warning_message);
}
public static void typeclassify(string path){
file destfile = new file(path);
//判断是否是文件夹,文件夹是否为空
if (destfile.isfile()) {
joptionpane.showmessagedialog(null, "对不起,不能对文件进行按类型分类,请填写文件夹路径");
return ;
if (!destfile.exists()) {
joptionpane.showmessagedialog(null, "对不起,所指定文件夹不存在");
typeoperation(path, destfile);
//提示消息
* @param path
* @param destfile
private static void typeoperation(string path, file destfile) {
file[] listfiles = destfile.listfiles();
if (listfiles.length == 0) {
//条件都成立了的话,就在这个路径下面创建一个文件
string newpath = path + file.separator + "这里存放的是按照类型分类之后的文件所在位置";
file newfile = new file(newpath);
if (!newfile.exists()) {
newfile.mkdirs();
set<string> typesset = new hashset<string>();
for (file file2 : listfiles) {
if (file2.isdirectory()) {
//进行递归
typeoperation(path, file2);
} else {
string filepath = file2.getpath();
//截取后缀名
string suffix = filepath.substring(filepath.lastindexof(".") + 1,filepath.length());
//文件类型文件夹所在的位置
string newtypepath = newpath + file.separator + suffix;
//使用set集合存储后缀名,让这些重复的后缀名只存在一次,如果没有才存入这个结合中
if (!typesset.contains(suffix)) {
typesset.add(suffix);
//在新创建的文件夹里面创建这个后缀名命名的文件夹。
new file(newtypepath).mkdir();
fileoperationutils.copyfile2folder(file2.getpath(), newtypepath);
* 按大小分类
* @param sizenamevalue :按名称分类的文件名称
* @param folderpath : 对那个文件夹进行按照大小分类
* @param min : 文件最小值
* @param max : 文件最大值
* @param unit1 : 最小值的单位
* @param unit2 : 最大值的单位
public static void sizeclassify(string sizenamevalue, string folderpath,
string min, string max, string unit1, string unit2) {
boolean b = datavalidate.validate(min, max);
if (!b) {
integer minvalue = integer.parseint(min);
long minsize = fileinfoutils.judgeunit(unit1)*minvalue;
integer maxvalue = integer.parseint(max);
long maxsize = fileinfoutils.judgeunit(unit2)*maxvalue;
string newpath = folderpath + file.separator + sizenamevalue;
if (!newfile.exists()) {
newfile.mkdir();
//开始按照大小分类
file fromfile = new file(folderpath);
//判断这个文件夹里面是否有文件,如果有文件才可以,否者直接返回
if (listfiles.length > 0) {
for (file file : listfiles) {
long filesize = fileinfoutils.getfilesize(file.getpath());
if (filesize > minsize && filesize < maxsize) {
fileoperationutils.copyfileinalltype(file.getpath(), newpath);
joptionpane.showmessagedialog(null, "对不起,你选中的文件夹为空!","提示信息",joptionpane.warning_message);
return ;
* 按照时间分类
* @param sizenamevalue :时间分类的名称
* @param folderpath : 这是一个文件夹路径,指定要对那个文件进行操作
* @param datetime1 : 这是时间点一
* @param datetime2 : 这是时间点二
public static void timeclassify(string timenamevalue, string folderpath,
string datetime1, string datetime2) {
//获得两个时间的时间毫秒值
long time1 = new dateformatutils().getmillisecondsbytimestring(datetime1);
long time2 = new dateformatutils().getmillisecondsbytimestring(datetime2);
long timemax = (time1 > time2) ? time1 : time2;
long timemin = (time1 > time2) ? time2 : time1;
if (time1 == time2) {
joptionpane.showmessagedialog(null, "对不起,两个时间的值不能相同!","提示消息",joptionpane.warning_message);
// 判断这个文件夹里面是否有文件,如果没有文件,那么就返回
file file = new file(folderpath);
file[] listfiles = file.listfiles();
// 如果有文件,创建文件夹,然后执行其他的工作
string newpath = folderpath + file.separator + timenamevalue;
newfile.mkdir();
long lastmodifiedtime = file2.lastmodified();
if (lastmodifiedtime <= timemax && lastmodifiedtime >= timemin) {
fileoperationutils.copyfileinalltype(file2.getpath(), newpath);
}