利用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("输入路径有误请重新输入");
        }

        }
    }
}

运行结果:



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值