首页 > 代码库 > java 文件及文件夹拷贝

java 文件及文件夹拷贝



View.java

package com.liang;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class View extends JFrame {
	JTextField src = http://www.mamicode.com/null;>


DirCopy.java


package com.liang;

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

public class DirCopy {
	static double cruent=0;
	public static void dirCopy(String src,String des)throws Exception{
		File srcFile=new File(src);
		
		File desFile=new File(des);
		if(!desFile.exists()){
			desFile.mkdir();
			//System.out.println("YYY:"+desFile.getAbsolutePath());
		}
		File []fs=null;
		//如果srcFiles是文件 
		if(srcFile.isFile()){
			fs=new File[]{srcFile};
		}else if(srcFile.isDirectory()){
			desFile=new File(desFile.getAbsolutePath()+"/"+srcFile.getName());
			desFile.mkdir();
			fs=srcFile.listFiles();
		}
		
		for(File f:fs){
			String newSrc=http://www.mamicode.com/f.getAbsolutePath();>

FileCopy.java


package com.liang;

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

public class FileCopy {
	static final int SIZE=1024*1024*15;
	public static void flieCopy(String src,String des)throws Exception{
		File srcFile=new File(src);
		File desFile=new File(des);
		FileInputStream fis=new FileInputStream(srcFile);
		FileOutputStream fos=new FileOutputStream(desFile);
		byte b[]=new byte[FileCopy.SIZE];
		int n;
		long length=srcFile.length();
		while((n=fis.read(b))!=-1){
			fos.write(b,0,n);
			DirCopy.cruent+=n;
			//System.out.println("cruent:"+DirCopy.cruent);
			//System.out.println("Listener.Length:"+Listener.Length);
			int precent=(int)( (DirCopy.cruent/Listener.Length)*100);
			View.progressBar.setValue(precent);
			if(precent==100){
				long endTime=System.currentTimeMillis();
				System.out.println(endTime-Listener.startTime);
			}
			//System.out.println(precent);
			
		}
		fos.close();
		fis.close();
	}

}

Listener.java

package com.liang;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JLabel;

public class Listener implements ActionListener{
	View view=null;
	String src=http://www.mamicode.com/null;>