标签:des class blog code java http
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 = null; JTextField des = null; JButton srcSelect = null; JButton desSelect = null; static JProgressBar progressBar = null; JButton OK = null; JButton reset=null; JPanel panel = null; public static void main(String[] args) { new View(); } public View() { this.setBounds(300, 200, 425, 200); this.setTitle("拷贝"); this.setResizable(false); init(); addComponent(); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void init() { panel = new JPanel(); src = new JTextField(25); des = new JTextField(25); src.setEnabled(false); des.setEnabled(false); // src.setBackground(Color.lightGray ); // des.setBackground(Color.lightGray ); srcSelect = new JButton("浏览"); desSelect = new JButton("浏览"); // 创建进度条 progressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100); // 创建进度条当前值 progressBar.setValue(0); // 设置进度条上是否显示%比 progressBar.setStringPainted(true); // 设置进度条默认大小 progressBar.setPreferredSize(new Dimension(350, 20)); progressBar.setBackground(Color.white); progressBar.setForeground(Color.green); OK = new JButton("拷贝"); reset=new JButton("重置"); } private void addComponent() { this.add(panel); // 创建监听器 Listener listener=new Listener(this); panel.add(new JLabel("源文件")); panel.add(src); panel.add(srcSelect); srcSelect.setActionCommand("srcSelect"); srcSelect.addActionListener(listener); panel.add(new JLabel("目标位置")); panel.add(des); panel.add(desSelect); desSelect.setActionCommand("desSelect"); desSelect.addActionListener(listener); panel.add(progressBar); OK.setActionCommand("OK"); OK.addActionListener(listener); panel.add(OK); reset.setActionCommand("reset"); reset.addActionListener(listener); panel.add(reset); } }
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=f.getAbsolutePath(); String newDes=desFile.getAbsolutePath()+"/"+f.getName(); if(f.isFile()){ FileCopy.flieCopy(newSrc, newDes); }else if(f.isDirectory()){ dirCopy(newSrc, newDes); } } } }
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(); } }
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=null; String des=null; static double Length = 0; static long startTime=0; public Listener(View view){ this.view=view; } @Override public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("srcSelect")){ src=getSelectFileName(JFileChooser.FILES_AND_DIRECTORIES ); //System.out.println(src); view.src.setText(src); }else if(e.getActionCommand().equals("desSelect")){ des=getSelectFileName(JFileChooser.DIRECTORIES_ONLY); //System.out.println(des); view.des.setText(des); }else if(e.getActionCommand().equals("reset")){ view.src.setText(""); view.des.setText(""); View.progressBar.setValue(0); }else if(e.getActionCommand().equals("OK")){ startTime=System.currentTimeMillis(); //System.out.println(startTime); if(src!=null&&src!=null){ //获得拷贝文件或文件夹的总大小 Length=getLength(src); new Thread(new Runnable() { @Override public void run() { try { DirCopy.dirCopy(src, des); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start();; } long endTime=System.currentTimeMillis(); } } private String getSelectFileName(int mode) { JFileChooser jfc=new JFileChooser(); jfc.setFileSelectionMode(mode); jfc.showDialog(new JLabel(), "选择"); File file=jfc.getSelectedFile(); if(file==null) return null; return file.getAbsolutePath(); // if(file.isDirectory()){ // System.out.println("文件夹:"+file.getAbsolutePath()); // }else if(file.isFile()){ // System.out.println("文件:"+file.getAbsolutePath()); // } // System.out.println(jfc.getSelectedFile().getName()); } private double getLength(String src) { double length = 0; File file = new File(src); // System.out.println("get:"+src); if (file.isFile()) return file.length(); File fs[] = file.listFiles(); for (File f : fs) { if (f.isDirectory()) { length += getLength(f.getAbsolutePath()); } else { length += f.length(); } } return length; } }
标签:des class blog code java http
原文地址:http://blog.csdn.net/liang5630/article/details/29372757