创建多个目录,在其下创建大量不同数目的文件,现将其均分到目录中,其中.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<String> oldPaths;//存储文件地址的集合
@Override
public void run() {
for(int j=begin;j<begin+size;j++){
String path=oldPaths.get(j);
String trsPath=oldPaths.get(j).replaceAll(".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<String> getOldPaths() {
return oldPaths;
}
/**
* @param oldPaths the oldPaths to set
*/
public void setOldPaths(List<String> oldPaths) {
this.oldPaths = oldPaths;
}
public CutThread(int begin, int size, File file, List<String> 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<String> morePath=new ArrayList<String>();
private Map<Integer,Integer> lessMap=new HashMap<Integer,Integer>();
//从小到大排序+文件夹数目+文件数+>avg的文件夹个数
public void paixu(String path,String... ends){
files=new File(path).listFiles();
//文件夹个数
mkdirCount=files.length;
//查找出.ok的文件数目
for(int i=0;i<files.length;i++){
if(files[i].isDirectory()){
fileCount+=getEnds(files[i].getAbsolutePath(), ends);
}
}
//每个文件夹下放.ok的平均数
avg=fileCount%mkdirCount==0?fileCount/mkdirCount:(fileCount/mkdirCount+1);
//针对每个文件夹中的文件个数进行排序
for(int i=0;i<files.length-1;i++){
for(int j=0;j<files.length-i-1;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;
}
}
}
//取出<avg的文件夹个数
for(int i=0;i<files.length;i++){
if(getEnds(files[i].getAbsolutePath(), ends)<avg){
lessNum++;
}
}
countOutFile(files, avg, ".ok");
countInFile(morePath, lessMap, files);
}
//用list存储所有多余文件,map存储每个文件需要的文件数目
public void countOutFile(File[] files,int avg,String... ends){
//size是要传递的文件数
for(int i=0;i<files.length;i++){
File temp=files[i];
int all=getEnds(files[i].getAbsolutePath(), ".ok");
if(temp.isDirectory()){
if(all>avg){
File[] fs=temp.listFiles();
for(int j=0,size=0;j<temp.listFiles().length&&size<all-avg;j++){
if(fs[j].getName().endsWith(ends[0])){
morePath.add(fs[j].getAbsolutePath());
size++;
}
}
}else{
lessMap.put(i,avg-all);
//值存放的是需要多少个文件
}
}
}
}
public void countInFile(List<String> morePath,Map<Integer,Integer> lessMap,File[] files){
int begin=0;
for(int i=0;i<lessNum;i++){
if(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;i<files.length;i++){
File f=files[i];
if(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));
}
}