手记

用多线程对文件的分来复制 ,比如音乐,word文档,txt,excel等

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Worker extends Thread {

private DirManager manager;
// private String copy_From = "E:\\amu1"; // 源文件

private String copy_TO_txt = "E:\\amu_txt"; // 目标文件
private String copy_TO_xlsx = "E:\\amu_xlsx"; // 目标文件
private String copy_TO_zip = "E:\\amu_zip"; // 目标文件
private String copy_TO_mp3= "E:\\amu_mp3"; // 目标文件
private String copy_TO_pdf= "E:\\amu_pdf"; // 目标文件
private String copy_TO_docx= "E:\\amu_docx"; // 目标文件
private String  copy_TO_other= "E:\\amu_other"; // 目标文件
private String  copy_TO_picture= "E:\\amu_picture"; // 目标文件

public Worker(DirManager m) {
    manager = m;
}

public void run() {

    //System.out.println(11);

    // 尝试3次
    int test = 5;
    while (test > 0) {   //这个循环非常巧妙,在经理的提示下完才完成,这个方法必须学会

        File[] dir = manager.getFirstDirectory();

        if (dir != null) {
            processDirectory(dir);

            test = 5;
        } else {

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            test--;

        }
    }

}

private void processDirectory(File[] dir) {

    //File [] get_From2=(new File(dir.getAbsolutePath()).listFiles();
    for(int i=0;i<dir.length;i++){

                    if (dir[i].isFile()) {
                    String fileName=dir[i].getName();
                    int dot=fileName.lastIndexOf(".");
                    String subName=fileName.substring(dot+1);

                    switch (subName) {
                    case "xlsx":
                        (new File(copy_TO_xlsx)).mkdirs();
                        String toFile = copy_TO_xlsx + "/" + dir[i].getName();
                        try {
                            copyFile(dir[i], new File(toFile));
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        break;

                    case "txt":
                        (new File(copy_TO_txt)).mkdirs();
                        String toFile1 = copy_TO_txt + "/" + dir[i].getName(); // 写的时候这里少了个/.导致不能复制
                        try {
                            copyFile(dir[i], new File(toFile1));
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        break;

                    case "docx":
                        (new File(copy_TO_docx)).mkdirs();
                        String toFile2 = copy_TO_docx + "/" + dir[i].getName();
                        try {
                            copyFile(dir[i], new File(toFile2));
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        break;

                    case "mp3":
                        (new File(copy_TO_mp3)).mkdirs();
                        String toFile3 = copy_TO_mp3 + "/" + dir[i].getName();
                        try {
                            copyFile(dir[i], new File(toFile3));
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        break;

                    case "pdf":
                        (new File(copy_TO_pdf)).mkdirs();
                        String toFile4 = copy_TO_pdf + "/" + dir[i].getName();
                        try {
                            copyFile(dir[i], new File(toFile4));
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        break;

                    case "zip":
                        (new File(copy_TO_zip)).mkdirs();
                        String toFile5 = copy_TO_zip + "/" + dir[i].getName();
                        try {
                            copyFile(dir[i], new File(toFile5));
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        break;

                    case "jpg":
                    case "png":
                        (new File(copy_TO_picture)).mkdirs();
                        String toFile7 = copy_TO_picture + "/" + dir[i].getName();
                        try {
                            copyFile(dir[i], new File(toFile7));
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        break;

                    default:
                        (new File(copy_TO_other)).mkdirs();
                        String toFile6 = copy_TO_other + "/" + dir[i].getName();
                        try {
                            copyFile(dir[i], new File(toFile6));
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        break;
                    }

                    }
                    if (dir[i].isDirectory()) {
                        manager.add1(dir[i]);
                    }
    }

}

private void copyFile(File from_File, File to_File) throws IOException {
    // TODO Auto-generated method stub

    FileInputStream infile = new FileInputStream(from_File);// 新建输入流
    BufferedInputStream inbuf = new BufferedInputStream(infile);// 对输入流进行缓冲

    FileOutputStream outfile = new FileOutputStream(to_File); // 新建输出流
    BufferedOutputStream outbuf = new BufferedOutputStream(outfile);// 对输出流进行缓冲

    // 缓冲数组
    byte[] bt = new byte[20 * 1024 * 1024];
    int len;
    while ((len = inbuf.read(bt)) != -1) {

        outbuf.write(bt, 0, len);

    }

    outbuf.flush(); // 刷新缓冲

    infile.close();
    inbuf.close();

    outfile.close();
    outbuf.close();
}

}

import java.io.File;
import java.lang.reflect.Array;
import java.util.LinkedList;
import java.util.Queue;

public class DirManager {

private Queue<File> queue = new LinkedList<File>(); // 放文件夹的队列

private String copy_From = "E:\\awazlik assar"; // 源文件

public DirManager(){

    queue.add(new File(copy_From));

}

public void add1(File dir){

     queue.add(dir);

     System.out.println(queue.size());

}

synchronized public File[] getFirstDirectory() {
    if (!queue.isEmpty()) {
        File dir = queue.poll();// 如果队列不空,返回队列中的头文件
        File [] get=(new File(dir.getAbsolutePath())).listFiles();
        return get;
    } else
        return null;
}

}

public class Main{

public static void main(String[] args) {

    DirManager dirManager = new DirManager();

    for (int i = 0; i <10; i++) {    
        Worker worker = new Worker(dirManager);
        worker.start();

    }

}

}

14人推荐
随时随地看视频
慕课网APP

热门评论

不错,你可以更简化一下

Good. I like. It

查看全部评论