天天看點

Java 中按檔案名稱分類,按檔案大小分類,按照檔案類型分類,按照最後修改時間分類的工具類

在此部落格中用到了檔案操作的工具類,可以連接配接

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);

}