文件均分到目录

创建多个目录,在其下创建大量不同数目的文件,现将其均分到目录中,其中.ok和.trs必须在同一个文件夹中。

//线程类,做剪切操作
package test4;

import java.io.File;
import java.util.List;
import java.util.Map;

public class CutThread extends Thread {
    private int begin;//在list集合中的开始位置--像栈或队列的指针
    private int size;//要粘贴的文件总数
    private File file;//要剪切到哪个文件夹
    private List oldPaths;//存储文件地址的集合
    @Override
    public void run() {
        for(int j=begin;j".ok",".trs");
            File oldFile=new File(path);
            File oldFile1=new File(trsPath);
            File newFile=new File(file.getAbsolutePath()+File.separator+oldFile.getName());
            File newFile1=new File(file.getAbsolutePath()+File.separator+oldFile1.getName());
            oldFile.renameTo(newFile);
            oldFile1.renameTo(newFile1);
        }
    }
    /**
     * @return the begin
     */
    public int getBegin() {
        return begin;
    }
    /**
     * @param begin the begin to set
     */
    public void setBegin(int begin) {
        this.begin = begin;
    }
    /**
     * @return the size
     */
    public int getSize() {
        return size;
    }
    /**
     * @param size the size to set
     */
    public void setSize(int size) {
        this.size = size;
    }
    /**
     * @return the file
     */
    public File getFile() {
        return file;
    }
    /**
     * @param file the file to set
     */
    public void setFile(File file) {
        this.file = file;
    }
    /**
     * @return the oldPaths
     */
    public List getOldPaths() {
        return oldPaths;
    }
    /**
     * @param oldPaths the oldPaths to set
     */
    public void setOldPaths(List oldPaths) {
        this.oldPaths = oldPaths;
    }
    public CutThread(int begin, int size, File file, List oldPaths) {
        super();
        this.begin = begin;
        this.size = size;
        this.file = file;
        this.oldPaths = oldPaths;
    }

}
//逻辑处理类
package test4;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sun.javafx.fxml.BeanAdapter;

public class Luoji {
    private File files[];
    private int fileCount=0;
    private int mkdirCount=0;
    private int avg=0;
    private int lessNum=0;
    public List morePath=new ArrayList();
    private Map lessMap=new HashMap();
    //从小到大排序+文件夹数目+文件数+>avg的文件夹个数
    public void paixu(String path,String... ends){
        files=new File(path).listFiles();
        //文件夹个数
        mkdirCount=files.length;
        //查找出.ok的文件数目
        for(int i=0;iif(files[i].isDirectory()){
                fileCount+=getEnds(files[i].getAbsolutePath(), ends);
            }
        }
        //每个文件夹下放.ok的平均数
        avg=fileCount%mkdirCount==0?fileCount/mkdirCount:(fileCount/mkdirCount+1);
        //针对每个文件夹中的文件个数进行排序
        for(int i=0;i1;i++){
            for(int j=0;j1;j++){
                File temp=null;
                if(getEnds(files[j].getAbsolutePath(), ends)>getEnds(files[j+1].getAbsolutePath(), ends)){
                    temp=files[j];
                    files[j]=files[j+1];
                    files[j+1]=temp;
                }
            }
        }
        //取出
        for(int i=0;iif(getEnds(files[i].getAbsolutePath(), ends)".ok");
        countInFile(morePath, lessMap, files);
    }

    //用list存储所有多余文件,map存储每个文件需要的文件数目
    public void countOutFile(File[] files,int avg,String... ends){
        //size是要传递的文件数
        for(int i=0;iint all=getEnds(files[i].getAbsolutePath(), ".ok");
            if(temp.isDirectory()){
                if(all>avg){
                    File[] fs=temp.listFiles();
                    for(int j=0,size=0;jif(fs[j].getName().endsWith(ends[0])){
                            morePath.add(fs[j].getAbsolutePath());
                            size++;
                        }
                    }
                }else{
                    lessMap.put(i,avg-all);
                    //值存放的是需要多少个文件
                }
            }
        }
    }

    public void countInFile(List morePath,Map lessMap,File[] files){
        int begin=0;
        for(int i=0;iif(i>0){
                begin=begin+lessMap.get(i-1);
            }
            CutThread ct=new CutThread(begin, lessMap.get(i),files[i],morePath);
            ct.start();
            try {
                ct.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    //将以end为结尾的文件个数取出来
    public int getEnds(String path,String... ends){
        int count=0;
        File f=new File(path);
        if(f.isDirectory()){
            for(File ff:f.listFiles()){
                if(ff.getName().endsWith(ends[0])){
                    count++;
                }
            }
        }
        return count;
    }

    public void tongji(){
        int count;
        for(int i=0;iif(f.isDirectory()){
                System.out.println(f.getName());
                System.out.println(f.listFiles().length);
            }
        }
    }
    public int getFileCount() {
        return fileCount;
    }
    public void setFileCount(int fileCount) {
        this.fileCount = fileCount;
    }
    public int getMkdirCount() {
        return mkdirCount;
    }
    public void setMkdirCount(int mkdirCount) {
        this.mkdirCount = mkdirCount;
    }

}
//测试类,未经优化

package test4;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import javafx.scene.transform.Scale;

public class Test {
public static void main(String[] args) {
System.out.println(“请输入文件夹前缀”);
Scanner scan=new Scanner(System.in);
String s=scan.nextLine();
for(int i=0;i<10;i++){
new File(“d:\iotest”+File.separator+s+i).mkdir();
}
for(int i=0;i<1000;i++){
//D:\iotest\1\
try {
new File(“d:\iotest”+File.separator+s+”1”+File.separator+i+”.ok”).createNewFile();
new File(“d:\iotest”+File.separator+s+”1”+File.separator+i+”.trs”).createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for(int i=0;i<500;i++){
try {
new File(“d:\iotest”+File.separator+s+”2”+File.separator+i+”.ok”).createNewFile();
new File(“d:\iotest”+File.separator+s+”2”+File.separator+i+”.trs”).createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

    Luoji l=new Luoji();
    long beginT=System.currentTimeMillis();
    l.paixu("d:\\iotest", ".ok");
    l.tongji();
    long endT1=System.currentTimeMillis();
    System.out.println("总用时:"+(endT1-beginT));
}

}

你可能感兴趣的:(java小程序1)