`
itace
  • 浏览: 178315 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

copy

 
阅读更多
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class JCopy {

	
	public static long size_total = 0;
	public static long size_copy = 0;
	public static int percent_process = 0;//进度百分比
	public static JProgressBar jbar;
	public static Container p;
	public static JLabel label_jbar;
	public static JLabel label_result;
	public static JButton confirm;
	
	public static String toUnit(long size){
		if (size>0) {
			if (size<1000) {
				return size+"B";
			}else if (size<1000000l) {
				size=size/1000l;
				return size+"K";
			}else if (size<1000000000l) {
				size=size/1000000l;
				return size+"M";
			}else {
				size=size/1000000000l;
				return size+"G";
			}
		}else {
			return 0+"B";
		}
	}
	/**
	 * 复制单个
	 * @param file_src
	 * @param directory_dest
	 */
	public static void copyOne(final File file_src,final File directory_dest){
		if (file_src.exists()&&file_src.isFile()) {
			size_total=file_src.length();
			new Thread(new Runnable() {
				
				public void run() {
					long s = System.currentTimeMillis(); 
					File file_dest = new File(directory_dest,file_src.getName());
					if (file_src.length()<1024*1024*100) {
						copyChannel(file_src, file_dest);
					}else {
						copyBigFile(file_src, file_dest);
					}
					long e = System.currentTimeMillis(); 
					long a = (e-s)/1000;
					long b = size_total/(1024*1024);
					long c = new Double(b/a).longValue();
					label_result.setText("耗时"+a+"秒,"+c+"M/s");
				}
			}).start();
			new Thread(new Runnable() {
				
				public void run() {
					File file_dest = new File(directory_dest,file_src.getName());
					boolean bool = true;
					while (bool) {
						if (file_dest.length()<=size_total) {
							if(file_dest.length()==size_total){
								bool=false;
							}
							size_copy=file_dest.length();
							//进度
							int percent_process_now = (int) (size_copy*100/size_total);
							if (percent_process!=percent_process_now) {
								if (size_total==size_copy) {
									//开始按钮可以点击
									confirm.setEnabled(true);
								}
								percent_process=percent_process_now;
								jbar.setValue(percent_process); // 随着线程进行,增加进度条值
								jbar.setString(percent_process+"%");
							}
						}else {
							bool=false;
						}
					}
					
				}
			}).start();
			
		}
	}
	/**
	 * 递归复制
	 * @param file_src
	 * @param directory_dest
	 * @throws IOException
	 */
	public static void copyRecursive(File file_src,File directory_dest) throws IOException{
		if (file_src.exists()&&directory_dest.exists()&&directory_dest.isDirectory()) {
			if (file_src.isDirectory()) {
				File directory_dest2 = new File(directory_dest, file_src.getName());
				directory_dest2.mkdirs();
				File[]files = file_src.listFiles();
				for (File file : files) {
					copyRecursive(file, directory_dest2);
				}
			}else {
				File file_dest = new File(directory_dest,file_src.getName());
//				System.out.println(file_src.getAbsolutePath());
//				System.out.println(file_dest.getAbsolutePath());
				if (file_src.length()<1024*1024*100) {
					copyChannel(file_src, file_dest);
				}else {
					copyBigFile(file_src, file_dest);
				}
				size_copy += file_src.length();
				
				if (size_copy<=size_total) {
					//进度
					int percent_process_now = (int) (size_copy*100/size_total);
					if (percent_process!=percent_process_now) {
						if (size_total==size_copy&&percent_process_now==100) {
							//开始按钮可以点击
							confirm.setEnabled(true);
						}
						percent_process=percent_process_now;
						jbar.setValue(percent_process); // 随着线程进行,增加进度条值
						jbar.setString(percent_process+"%");
					}
				}
//				System.out.println(size_copy+"--"+size_total);
			}
		}
	}
	/**
	 * 递归删除
	 * @param file_src
	 * @param directory_dest
	 * @throws IOException
	 */
	public static void deleteRecursive(File file) throws IOException{
		if (file.exists()) {
			if (file.isDirectory()) {
				File[]files = file.listFiles();
				for (File f : files) {
					deleteRecursive(f);
				}
				file.delete();
			}else {
				file.delete();
			}
		}
	}
	public static void getFileSize(File file) throws IOException{
		if (file.exists()&&file.isDirectory()) {
			File[]files = file.listFiles();
			for (File f : files) {
				if (f.isDirectory()) {
					getFileSize(f);
				}else {
					size_total += f.length();
				}
			}
		}else {
			size_total += file.length();
		}
	}
	/**
	 * 复制大文件,1G+
	 * @param src
	 * @param dest
	 */
	public static void copyBigFile(File src, File dest) {
		FileInputStream fin = null;
		FileOutputStream fout = null;
		FileChannel in = null;
	    FileChannel out = null;
		try {
			//缓冲区--设置了100m
			ByteBuffer byteBuf = ByteBuffer.allocate(1024*1024*100);
			
			fin = new FileInputStream(src);
			fout = new FileOutputStream(dest);
			in = fin.getChannel();
		    out = fout.getChannel();
		    
		    while (true) {
				int eof = in.read(byteBuf);
				if (eof == -1)
					break;
				byteBuf.flip();
				out.write(byteBuf);
				byteBuf.clear();
			}
		    
			in.close();
			out.close();
			fin.close();
			fout.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (in!=null) {
					in.close();
				}
				if (out!=null) {
					out.close();
				}
				if (fin!=null) {
					fin.close();
				}
				if (fout!=null) {
					fout.close();
				}
			} catch (Exception e2) {
//				e2.printStackTrace();
			}
		}
	}
	//管道复制
	public static void copyChannel(File src,File dest){
		FileInputStream fin = null;
		FileOutputStream fout = null;
		FileChannel in = null;
	    FileChannel out = null;
		try {
			fin = new FileInputStream(src);
			fout = new FileOutputStream(dest);
			in = fin.getChannel();
		    out = fout.getChannel();
		    in.transferTo(0, in.size(), out);
			in.close();
			out.close();
			fin.close();
			fout.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (in!=null) {
					in.close();
				}
				if (out!=null) {
					out.close();
				}
				if (fin!=null) {
					fin.close();
				}
				if (fout!=null) {
					fout.close();
				}
			} catch (Exception e2) {
//				e2.printStackTrace();
			}
		}
	}
	public static void copy(File src,File dest){
		try {
			InputStream in = new FileInputStream(src);
			OutputStream out = new FileOutputStream(dest);
			int c = -1;
			while ((c=in.read())!=-1) {
				out.write(c);
			}
			in.close();
			out.close();
		} catch (Exception e) {
//			e.printStackTrace();
		}
	}
	private static TextField TEXT_FROM = null;
	private static TextField TEXT_TO = null;
	private static String PATH_FROM = "";
	private static String PATH_TO = "";

	
	/**
	 * 弹出选择框返回选中文件夹或者文件的路径
	 * @param type:1只文件2只文件夹3文件和文件夹
	 * @return
	 */
	public static String openFile(int type) {
		JFrame frame = new JFrame();
		String filename = File.separator + "temp";
		File f = new File(filename);
		JFileChooser fc = new JFileChooser(f);
		int mode = JFileChooser.FILES_AND_DIRECTORIES;
		if (type==1) {
			mode = JFileChooser.FILES_ONLY;
		}else if (type==2) {
			mode = JFileChooser.DIRECTORIES_ONLY;
		}else {
			
		}
		fc.setFileSelectionMode(mode);
		int result = fc.showOpenDialog(frame);
		if (result == 0) {
			File selFile = fc.getSelectedFile();
			return selFile.toString();
		}
		return "";
	}
	
	public static void main(String[] args) {
		JFrame jf = new JFrame("光速复制");
		jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
		jf.setLocation(390, 200);
		jf.setSize(600, 320);
		jf.setResizable(false);
		
		Container p  = jf.getContentPane();
		p.setLayout(null);
		
		//文字
		JLabel label_start = new JLabel("复制");
		label_start.setFont(new Font("Microsoft YaHei", Font.PLAIN, 18));
		label_start.setBounds(50, 50, 80, 30);
		p.add(label_start);
		JLabel label_end = new JLabel("到");
		label_end.setFont(new Font("Microsoft YaHei", Font.PLAIN, 18));
		label_end.setBounds(50, 100, 80, 30);
		p.add(label_end);
		// 组件
		TEXT_FROM = new TextField("", 10);
		TEXT_FROM.setBounds(150, 50, 220, 30);
		TEXT_FROM.setEnabled(false);
		p.add(TEXT_FROM);
		TEXT_TO = new TextField("", 10);
		TEXT_TO.setBounds(150, 100, 220, 30);
		TEXT_TO.setEnabled(false);
		p.add(TEXT_TO);

		//from 浏览
		JButton b = new JButton("浏览...");
		b.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
		b.setBackground(Color.white);
		b.setBounds(420, 50, 80, 30);
		b.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {

				try {
					PATH_FROM = openFile(3);
					TEXT_FROM.setText(PATH_FROM);
					//统计文件总大小
					size_total=0;
					getFileSize(new File(PATH_FROM));
					String text_totalsize = "约"+toUnit(size_total);
					label_jbar.setText(text_totalsize);
					jbar.setValue(0);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		p.add(b);
		//to 浏览
		JButton c = new JButton("浏览...");
		c.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
		c.setBackground(Color.white);
		c.setBounds(420, 100, 80, 30);
		c.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				
				PATH_TO = openFile(2);
				TEXT_TO.setText(PATH_TO);
			}
		});
		p.add(c);
		
		//进度条
		jbar = new JProgressBar(0,100);
		jbar.setBounds(50, 160, 500, 20);
		jbar.setValue(0);
		jbar.setMinimum(0);
		jbar.setMaximum(100);
		jbar.setStringPainted(true);
		jbar.addChangeListener(new ChangeListener() {
			
			public void stateChanged(ChangeEvent e) {
				
			}
		});
		p.add(jbar);
		
		//耗时&速度
		label_result = new JLabel();
		label_result.setFont(new Font("Microsoft YaHei", Font.PLAIN, 16));
		label_result.setBounds(50, 180, 150, 30);
//		label_result.setText("耗时20秒,10M/s");
		p.add(label_result);
		//进度条文字
		label_jbar = new JLabel();
		label_jbar.setFont(new Font("Microsoft YaHei", Font.PLAIN, 18));
		label_jbar.setBounds(450, 180, 80, 30);
		p.add(label_jbar);
		
		//
		confirm = new JButton("开始");
		confirm.setBackground(Color.white);
		confirm.setFont(new Font("SimSun", Font.BOLD, 18));
		confirm.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				final File file_from = new File(PATH_FROM);
				final File file_to = new File(PATH_TO);
				if (!file_from.exists()) {
					
					return ;
				}
				if (!file_to.exists()) {
					
					return ;
				}
				//进度归零
				jbar.setValue(0);
				jbar.setString("0%");
				size_copy = 0;
				percent_process = 0;//进度百分比
				label_result.setText("");
				
				String path = file_to.getAbsolutePath()+File.separator+file_from.getName();
				System.out.println(path);
				File file_exist = new File(path);
				try {
					deleteRecursive(file_exist);
				} catch (IOException e2) {
					e2.printStackTrace();
				}
				System.out.println(file_exist.exists());
				//因为事件监听的原因,必须开启新的线程,否则将等待此程序执行完以后才能执行其他的线程
				new Thread(new Runnable() {
					
					public void run() {
						try {
							if (file_from.isDirectory()) {
								long s = System.currentTimeMillis(); 
								copyRecursive(file_from,file_to);
								long e = System.currentTimeMillis(); 
								long a = (e-s)/1000;
								long b = size_total/(1024*1024);
								long c = new Double(b/a).longValue();
								label_result.setText("耗时"+a+"秒,"+c+"M/s");
							}else {
								copyOne(file_from, file_to);
							}
						} catch (Exception e1) {
							e1.printStackTrace();
						}
					}
				}).start();
				//开始按钮不可点击
				confirm.setEnabled(false);
			}
		});
		confirm.setBounds(350, 220, 100, 40);
		p.add(confirm);
		//
		JButton cancel = new JButton("关闭");
		cancel.setBackground(Color.white);
		cancel.setFont(new Font("SimSun", Font.BOLD, 18));
		cancel.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		cancel.setBounds(100, 220, 100, 40);
		p.add(cancel);
		
		p.setBounds(100, 50, 300, 200);
		
		jf.setVisible(true);
		
		
	}
}

 

分享到:
评论

相关推荐

    高通golden copy验证方案使用说明.docx

    高通 Golden Copy 验证方案使用说明 Golden Copy 验证方案是一种在高通平台上验证 Golden Copy 的方法,该方案旨在确保 Golden Copy 的正确性和可靠性。下面是对 Golden Copy 验证方案的详细说明: 一、 目的 ...

    FastCopy 源代码

    FastCopy is the Fastest Copy/Delete Software on Windows. &lt;br&gt;It can copy/delete unicode and over MAX_PATH(260byte) pathname files. &lt;br&gt;Automatically, after whether the copy origin and copy ...

    iCopy解码软件

    《iCopy解码软件详解与应用》 在数字化办公环境中,高效的数据传输和处理是提升工作效率的关键。iCopy解码软件作为一个专为iCopy设备设计的辅助工具,它旨在帮助用户更加便捷地处理由iCopy设备捕获的各类数据,从而...

    FastCopy 局域网文件拷贝

    FastCopy是一款高效、实用的文件复制工具,尤其在局域网环境中的文件传输表现卓越。它以其快速的拷贝速度和稳定的操作性能赢得了广大用户的喜爱。FastCopy在处理大量数据或者大文件时,能显著提高文件拷贝的效率,极...

    文件资源同步工具 FastCopy

    **FastCopy:高效便捷的文件资源同步工具** FastCopy 是一款高效、易用的文件同步工具,尤其在处理大量数据的拷贝和同步任务时,它展现出了显著的优势。这款工具以其快速、稳定和强大的功能在IT行业内备受青睐。...

    FastCopy 支持断电续传

    FastCopy是一款高效、快速的文件复制工具,专为Windows用户设计,旨在显著提升文件的复制和移动速度。在日常工作中,我们经常需要处理大量的数据迁移,而系统自带的复制功能可能无法满足对速度的需求,FastCopy正是...

    SAP集团拷贝 client copy

    SAP Group Copy,也称为Client Copy,是一种在SAP系统中复制现有客户端数据的过程,用于创建一个新的客户端,以便在其中进行定制、开发和测试,而不会影响生产环境。本教程将详细阐述如何在SAP ECC 6.0 32位系统中...

    fastcopy命令行参数解释

    `Fastcopy`是一款高效、快速的文件复制和删除工具,尤其在处理大量或大体积文件时,它的性能表现优于Windows自带的复制和删除功能。它不仅可以在Windows环境下运行,还能在DOS环境下通过命令行进行操作。以下是`Fast...

    iCopy2.2.4更新文件.rar

    iCopy是一款专为iPhone用户设计的数据管理工具,其2.2.4版本的更新文件主要针对设备的面容识别功能和数据备份与恢复进行了优化。在本次更新中,开发者可能着重提升了iCopy对于iPhone设备上面容ID(Face ID)的兼容性...

    FastCopy快速复制

    《FastCopy:高效文件传输工具详解》 在日常的计算机操作中,我们经常需要进行大量文件的复制或移动,尤其是在处理大容量数据时,Windows自带的资源管理器可能会显得力不从心,出现响应缓慢甚至崩溃的情况。为了...

    fastcopy 快速剪切复制

    标题中的“fastcopy 快速剪切复制”指的是FastCopy这款高效文件拷贝工具,它以其卓越的性能和便捷的使用方式在IT行业中备受推崇。FastCopy是一款开源且免费的软件,它的主要功能是实现文件和目录的快速复制与移动,...

    HPE 3PAR Remote Copy 软件详解指南

    HPE 3PAR Remote Copy 软件详解指南是一份详细指导用户如何配置和管理HPE 3PAR StoreServ Storage系统中远程复制功能的文档。本文将从以下几个方面详解指南中的关键知识点: ### HPE 3PAR Remote Copy 软件概述 ...

    Fastcopy同步复制(完全对比服务器同步工具)

    《Fastcopy:高效精准的服务器同步利器》 在IT领域,数据同步是不可或缺的一部分,尤其在服务器管理和维护中。Fastcopy作为一个优秀的数据同步工具,因其高效、精准的特性,深受广大用户喜爱。本文将深入探讨Fast...

    Fastcopy文件拷贝copy

    标题中的“Fastcopy文件拷贝copy”指的是一款名为Fastcopy的高效文件复制软件。Fastcopy因其极快的文件拷贝速度而闻名,它是由日本开发者设计并开发的一款工具,旨在提供比操作系统自带的文件复制功能更快速、更稳定...

    fastcopy软件 64位

    【Fastcopy软件 64位】是一款高效、专业的文件复制工具,尤其在处理大量数据迁移或备份时,其性能表现卓越。它不仅是一款强大的文件复制利器,还具有精确拷贝文件权限的功能,这对于管理和维护域文件服务器至关重要...

    ssh-copy-id 脚本

    问题:ssh-copy-id 命令无法使用,在linux服务器的ssh服务中没有有这个命令,可以在/usr/bin/的这文件夹中查看没有这个ssh-copy-id 命令。 问题分析:是由于ssh服务的问题,可以直接将ssh-copy-id 命令拷贝/usr/...

    Qt使用WM_COPYDATA消息进行进程通信 示例demo

    其中,利用Windows特有的WM_COPYDATA消息是一种简单而有效的方式。本示例将详细介绍如何在Qt中使用WM_COPYDATA消息进行进程通信。 首先,WM_COPYDATA是Windows消息系统中的一种消息,用于在不同进程间传递数据。...

    FastCopy,一款好用的文件和权限备份的软件,备份速度快

    FastCopy是一款高效、快速的文件复制与备份工具,尤其在处理大量数据时,其性能表现卓越。这款软件在IT行业中广泛被用作替代系统自带的文件复制功能,以实现更快速、更稳定的数据迁移和备份。 FastCopy的核心优势...

    可以COPY损坏文件可以COPY损坏文件COPY专家

    标题"可以COPY损坏文件可以COPY损坏文件COPY专家"暗示了我们关注的重点是如何处理和复制损坏的文件。在这个主题下,我们将深入探讨文件损坏的原因、如何识别损坏文件,以及如何尝试恢复或复制这些文件。 文件损坏...

Global site tag (gtag.js) - Google Analytics