文件复制附带进度条

 

 

package com.io.copyfile;

 

import java.awt.BorderLayout;

import java.awt.Frame;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

 

import javax.swing.JDialog;

import javax.swing.JFileChooser;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JProgressBar;

import org.apache.log4j.Logger;

 

/**

 * 弹出对话框选择任意文件进行复制 复制新文件在项目跟目录

 * 

 * @author <a href="mailto:qiyang199132@163.com">qiyang</a>

 * @version $Revision$

 * @create 2011-5-9 下午02:47:21

 */

public class CopyFile {

private final static Logger log = Logger.getLogger(CopyFile.class);

 

public static void main(String[] args) {

CopyFile cf = new CopyFile();

File file = cf.getFile();

if (file == null) {

JOptionPane.showMessageDialog(null, "错误操作", "提示", 1);

} else {

cf.coypFile(file);

}

}

 

/**

* 弹出对话框选择文件

* @return 返回选择的文件

*/

public File getFile() {

JFileChooser jf = new JFileChooser();

jf.setDialogTitle("选择文件");

jf.showOpenDialog(null);

jf.setVisible(true);

File selectedFile = null;

selectedFile = jf.getSelectedFile();

if (selectedFile != null) {

File[] list = new File(System.getProperty("user.dir")).listFiles();

for (int i = 0; i < list.length; i++) {

if (selectedFile.getName().equals(list[i].getName())) {

JOptionPane.showMessageDialog(null, "改文件已存在", "提示", 1);

return null;

}

}

} else {

System.out.println("No! You did not select the right file.");

}

return selectedFile;

}

 

/**

* 对传进来的文件进行复制并放入项目跟目录

* @param file 传进来的文件

*/

public void coypFile(File file) {

String path = System.getProperty("user.dir");

try {

/**

* 文件字节长度

*/

int length = (int) file.length();

/**

* 输入流 读取文件

*/

FileInputStream is = new FileInputStream(file);

/**

* 输出流写文件

*/

FileOutputStream os = new FileOutputStream(path + "/" + file.getName());

/**

* 缓存

*/

byte[] bt = new byte[256];

/**

* 定义字节接受每次读取的长度

*/

int b = 0;

/**

* 定义一次读取的长度 然后++得到已经复制多少

*/

int size = 0;

/**

* 定义frame

*/

Frame frame=new Frame("进度条");

/**

* 定义一个对话框并设置样式

*/

final JDialog dlg = new JDialog(frame, "Progress Dialog", true);

/**

* 定义一个进度条 size   和  length  为 参数  

*/

JProgressBar dpb = new JProgressBar(size,length);

/**

* 设置对话框样式

*/

       dlg.add(BorderLayout.CENTER, dpb);

       dlg.add(BorderLayout.NORTH, new JLabel("正在复制..."));

       dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

       dlg.setSize(300, 75);

       /**

        * 设置相对值 给frame

        */

       dlg.setLocationRelativeTo(frame);

       /**

        * 定义线程

        * 对进度条进行监控

        */

       Thread t=new Thread(new Runnable() {

@Override

public void run() {

dlg.setVisible(true);

}

});

       /**

        * 开启线程

        */

       t.start();

       /**

        * 循环

        * 读取文件   让以读取的++

        * 写文件 设置 进度条的进度++

        */

while ((b = is.read(bt)) != -1) {

size += b;

os.write(bt);

dpb.setValue(size);

}

/**

* 线程休眠1秒

*/

t.sleep(1000);

/**

* 关闭流

*/

os.close();

is.close();

/**

* 复制完了退出程序

*/

if (dpb.getValue() == length) {

                dlg.setVisible(false);

                System.exit(0);

            }

} catch (FileNotFoundException e) {

log.error("文件找不到");

} catch (IOException e) {

log.error("IO异常");

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

 

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值