Java复制文件使用进度条显示

第一部分,复制文件的方法,计算文件夹的大小

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;

/**
 * Created by 1520-09 on 2017/5/9.
 */
public class CopyFiles {
    private static String srcDir;
    private static long srcLen = 0;
    private static long trgLen = 0;

    private static File trg;
    private static File src;

    public static long getSrcLen() {
        return srcLen;
    }
    public static long getTrgLen(){
        trgLen = 0;
        return trgSize(trg);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("SRC:");
        String srcString = scanner.nextLine();
        src = new File(srcString);
        if (src.exists()){
			System.out.println("Scanning...");
            srcLen = 0;
            srcSize(src);
			System.out.println("Scanning Finished.");
        }
        System.out.println("TRG:");
        String trgString = scanner.nextLine();
        trg = new File(trgString);
        if (src.isFile()){
            singleCopy(src,trg);
        }else if (src.isDirectory()){
            srcDir = src.getName();
			System.out.println("Copying...");
            new CopyProgressBar().start();
            copyFiles(src,trg);
        }
        System.out.println("--------------------------Dnoe-------------------------");
    }

    /**
     * copy a directory and its sub-files
     * @param src a source directory
     * @param trg a target directory
     */
    public static void copyFiles(File src,File trg){
        File[] files = src.listFiles();
        for (File file :files) {
            if (file.isDirectory()){
                copyFiles(file,trg);
            }else if (file.isFile()){
                String srcPath = file.getParent().substring(file.getParent().indexOf(srcDir));
                File trgParentPath = new File(trg.getAbsolutePath(),srcPath);
                if (!trgParentPath.exists()){
                    trgParentPath.mkdirs();
                }
                singleCopy(file,new File(trgParentPath,file.getName()));
            }
        }
    }

    /**
     * sopy a file to other side,and you can set a new file name
     * @param src a source file name with ext-name
     * @param trg a target file name with ext-name
     */
    public static  void singleCopy(File src,File trg){
        if (src.exists()){
            if(trg.exists() && trg.length() == src.length()){
                return;
            }
            try{
                FileInputStream fis = new FileInputStream(src);
                FileOutputStream fos = new FileOutputStream(trg);
                int len;
                byte[] arr = new byte[102400];
                while ((len = fis.read(arr)) != -1){
                    fos.write(arr,0,len);
                }
                fis.close();
                fos.close();
                //System.out.println("Copied: "+src.getName()+" to "+trg.getAbsolutePath());
                CopyProgressBar.setInfo(src.getName());
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
    public static long srcSize(File file){
        if(file.isDirectory()){
            File[] files = file.listFiles();
            for (File f : files) {
                 if (f.isDirectory()){
                     srcSize(f);
                 }else if(f.isFile()){
                     srcLen += f.length();
                 }
            }
        }
        return srcLen;
    }
    public static long trgSize(File file){
        if(file.isDirectory()){
            File[] files = file.listFiles();
            for (File f : files) {
                if (f.isDirectory()){
                    trgSize(f);
                }else if(f.isFile()){
                    trgLen += f.length();
                }
            }
        }
        return trgLen;
    }
}

第二部分,使用多线程开启进度条,并实时计算文件大小
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * Created by 1520-09 on 2017/5/12.
 */
public class CopyProgressBar extends Thread{
    private Frame frame;
    private JProgressBar progressBar;
    private Label percentLable;
    private static Label copyInfoLable;

    public static void setInfo(String fileInfo){
        copyInfoLable.setText(fileInfo);
    }

    CopyProgressBar(){
        frameInit();
        events();
    }

    private void events() {
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    private void frameInit() {
        frame = new Frame();
        frame.setLayout(null);
        frame.setBounds(400,100,400,300);
        lableInit();

        frame.setResizable(false);

        frame.setVisible(true);
    }

    private void lableInit() {
        progressBar = new JProgressBar(0,100);
        percentLable = new Label();
        copyInfoLable = new Label();

        progressBar.setBounds(8,120,386,30);
        progressBar.setBorderPainted(false);
        progressBar.setBackground(Color.BLACK);
        progressBar.setForeground(Color.red);
        progressBar.setStringPainted(false);

        copyInfoLable.setBounds(8,200,380,30);

        percentLable.setBounds(180,160,50,30);

        frame.add(progressBar);
        frame.add(copyInfoLable);
        frame.add(percentLable);
    }

    @Override
    public void run() {
        progressBar.setValue(0);
        percentLable.setText(progressBar.getValue()+"%");
        double srcSize = CopyFiles.getSrcLen();
        while (true){
            double percent = CopyFiles.getTrgLen()/srcSize*100;
            progressBar.setValue((int)percent);
            percentLable.setText(progressBar.getValue()+"%");
            frame.setTitle("Copied: "+progressBar.getValue()+"% ALL: "+(int)(srcSize/1024/1024)+"M");
            if (progressBar.getValue() == 100){
                break;
            }
        }
        System.out.println("Filished.");
        System.exit(0);
    }
}

使用命令提示符即可运行

你可能感兴趣的:(java)