1. 程式人生 > >利用List Map Set集合編寫一個簡單桌面整理,內含完整原始碼分享

利用List Map Set集合編寫一個簡單桌面整理,內含完整原始碼分享

0X00

獲取桌面資料夾所有檔案,把檔案包裝成自己寫的filemod類,拆分出檔案字尾。按照字尾把檔案分類後copy到對應的資料夾中,如果copy成功則將桌面檔案刪除處理。暫未實現桌面資料夾內的檔案遍歷。

0x01

File 介面

public interface file {

    //get and setter
public String getFileName();
    public String getFilePath();
    public String getFileSuffix();
    public long getFileSize();
    public void setFileName
(String fname); public void setFilePath(String filePath); public void setFileSize(int size); public void setFileSuffix(String suffix); }

實現類 FileMod

import java.io.File;
public class FileMod implements file{
   private String fileName = null;
   private String filePath=null;
   private long 
fileSize=-1; private String fileSuffix=null; private File f=null; private boolean fisnull=true; private String toPath=""; public FileMod(File f){ this.f=f; this.fisnull=false; } public FileMod(){ } public String getFileName() { if(!fisnull){ return
f.getName(); }else{ return this.fileName; } } public String getFilePath() { if(!fisnull){ return f.getAbsolutePath(); }else{ return this.filePath; } } public String getFileSuffix() { if(!fisnull){ String name=f.getName(); int dindex= name.lastIndexOf("."); return name.substring(dindex+1,name.length()); }else{ return this.getFileSuffix(); } } public long getFileSize() { if(!fisnull){ if(!f.isDirectory()){ return f.length(); }else{ return this.fileSize; } } return this.fileSize; } public void setFileName(String fname) { if(!fisnull){ }else{ } } public void setFilePath(String filePath) { if(!fisnull){ }else{ } } public void setFileSize(int size) { if(!fisnull){ }else{ } } public void setFileSuffix(String suffix) { if(!fisnull){ }else{ } } public void setToPath(String toPath){ this.toPath=toPath; } public String getToPath(){ return this.toPath; } public File getFile(){ return f; } public boolean deleteFile(){ if(!f.isDirectory()&& f.exists()){ f.delete(); return true; }else if(!f.exists()){ System.out.println("Error:指定檔案不存在--"+f.getAbsolutePath()); return false; }else{ System.out.println("Error:暫不提供資料夾刪除操作。"); return false; } } }

FileUtil類

import java.io.*;
import java.util.*;
public class FileUtil {
    private File desktop;
    private String[] dirlist;
    private FileMod[] fmarr;
    private Set keySet;
    private Map<String,List<FileMod>>fileMods;
    public FileUtil(String desktoppath){
        this.desktop=new File(desktoppath);
        if(this.desktop.isDirectory()){
            this.dirlist=this.desktop.list();
}
    }
    /**
     * 輸出當前目錄所有的檔案
     * */
public void printDir(){
        System.out.println("當前路徑下存在的檔案或目錄有");
        for (int i=0;i<this.dirlist.length;i++){
            System.out.println(this.dirlist[i]);
}
    }
    //檢索當前目錄下所有的檔案字尾
public void findAllSuffix(){
        File[] farr=this.desktop.listFiles();
System.out.println("11111"+farr[2].getPath());
        if(farr.length>0){
            fmarr=new FileMod[farr.length];
            for(int i=0;i<farr.length;i++){
                if((farr[i].getName().lastIndexOf(".")!=-1)){
                    fmarr[i]=new FileMod(farr[i]);
//System.out.println(fmarr[i].getFileSuffix());
}

            }
           }
    }
    //將目錄中所有檔案按照型別分類
public void Fileclassify(){
        if(fmarr.length>0){
            fileMods=new HashMap<String, List<FileMod>>();
List<String> list=new ArrayList<String>();
            for(int i=0;i<fmarr.length;i++){
                if(fmarr[i]!=null){
                    list.add(fmarr[i].getFileSuffix());
}else{
                    //System.out.println("空座標:"+i);
}

            }
            list=this.removeDuplicte(list);
Iterator<String> iterable=  list.iterator();
            while (iterable.hasNext()){
                List<FileMod> filemodList=new ArrayList<FileMod>();
fileMods.put(iterable.next(),filemodList);
}
            for (int i=0;i<fmarr.length;i++){
                if (fmarr[i]!=null){
                    List<FileMod> filist=fileMods.get(fmarr[i].getFileSuffix());
filist.add(fmarr[i]);
}


            }

        }else{
            fileMods=null;
}
    }

    //按照字尾名將當前所有檔案分類
public void printAllFile(){
        keySet=fileMods.keySet();
Iterator it= keySet.iterator();
       while (it.hasNext()){
           String key=it.next().toString();
System.out.println("<<<<<<<<<<<<<<當前檔案型別:"+key);
List ls= fileMods.get(key);
          for(int i=0;i<ls.size();i++){
              System.out.println(((FileMod)ls.get(i)).getFileName());
}
          System.out.println();
}
    }

    public void copyFileMods(String homepath) throws IOException {
        File homedir=new File(homepath);
File dir;
        if(!homedir.exists()){
            homedir.mkdir();
}
        Iterator it=keySet.iterator();
        while(it.hasNext()){
            String key=(String) it.next();
dir=new File(homepath+key+"\\");
            if(!dir.exists()){
                dir.mkdir();
}
            List li=fileMods.get(key);
Iterator listit= li.iterator();
            for(int i=0;i<li.size();i++){
                while (listit.hasNext()){
                    FileMod fm=(FileMod) listit.next();
                    this.setToPathOnFileMod(dir.getAbsolutePath()+"\\"+fm.getFileName(),fm);
System.out.println("當前filemod to path:"+fm.getToPath());
                    boolean copyisok=this.copyFile(fm.getFile(),new File(fm.getToPath()));
                    if(copyisok){
                        fm.deleteFile();
}
                }
            }

        }


    }
    public boolean copyFile(File fromFile,File toFile) throws IOException {
        FileInputStream ins=null;
FileOutputStream out=null;
        try{
             ins = new FileInputStream(fromFile);
out = new FileOutputStream(toFile);
            byte[] b = new byte[1024];
            int n=0;
            while((n=ins.read(b))!=-1){
                out.write(b, 0, n);
}

            ins.close();
out.close();
            return true;
}catch(Exception e){
            e.printStackTrace();
            if(ins!=null){
                ins.close();
}
            if(out!=null){
                out.close();
}


            return false;
}

    }

    public void setToPathOnFileMod(String toPath,FileMod fileMod){
        fileMod.setToPath(toPath);
}


    public List<String> removeDuplicte(List<String> list){
        HashSet<String> set=new HashSet<String>(list);
list.clear();
list.addAll(set);
        return list;
}

    public static void main(String[] Args) throws IOException {
        while(true){


        System.out.println("請輸入您的桌面路徑");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String descktopPath;
descktopPath = br.readLine();
        if("exit".equals(descktopPath)){
             break;
}
        File desktop=new File(descktopPath);
        if(desktop.exists()&&desktop.isDirectory()){
            System.out.println("<<<<<<<<開始整理");
FileUtil fu=new FileUtil(descktopPath);
fu.printDir();
fu.findAllSuffix();
fu.Fileclassify();
fu.printAllFile();
            try {
                fu.copyFileMods("D:\\home\\");
}catch (Exception e){
                System.out.println("檔案轉換時出現了異常"+e.getMessage());
}
        }else{
            System.out.println("輸入路徑有誤請重新輸入");
}

        }
    }
}

執行結果: