代码中含有注释
如果自己要写文件复制的代码,建议流程
1、写一个简单的程序完成文件复制
2、利用JProgressBar完成进度条的设置,以及进度条的刷新展示
3、gui界面的TtextArea以及button十分简单,不解释!你懂得!
4、将上述步骤串在一起就可,这里注意thread running不能一直跑,要留给程序一定的时间去检测copy,也就是thread running per 1000ms,时间太短或者说不设置的话会出问题的,自己去理解,不做过多解释
package homework;
import java.lang.*;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
//@author=BUPT:huanglin
public class File_Copy extends Frame implements ActionListener{
public int rate = 0;
String old_filepath;
String new_filepath;
private JProgressBar pro = new JProgressBar();
JPanel frame;
Button copy = new Button("COPY");
JTextArea From_Path = new JTextArea("");
JTextArea To_Path = new JTextArea("");
Run A = new Run();
//__init()
File_Copy(){
frame = new JPanel();
frame.setLayout(null);
//text
final JLabel j1 = new JLabel("From");
final JLabel j2 = new JLabel("To");
j1.setBounds(0,0,100,100);
From_Path.setBounds(100,40,300,40);
j2.setBounds(0,100,100,100);
To_Path.setBounds(100,140,300,40);
copy.setBounds(400,100,80,40);
pro.setBounds(50,200,400,40);
//pro.setBackground(Color.GREEN);
pro.setStringPainted(true);
copy.addActionListener(this);
frame.add(j1);
frame.add(From_Path);
frame.add(To_Path);
frame.add(j2);
frame.add(copy);
frame.add(pro);
this.add(frame);
A.start();
}
//file copy
public void copy(){
try{
long bytesum = 0;
int byteread = 0 ;
File old_file = new File(old_filepath);
if(old_file.exists()){
long filelength = old_file.length();
System.out.println("old_file length"+filelength);
InputStream in = new FileInputStream(old_filepath);
OutputStream out = new FileOutputStream(new_filepath);
//copy 1024 byte per time
byte [] buff = new byte[1024];
while ((byteread = in.read(buff))!=-1){
bytesum += byteread;
if (rate != Integer.parseInt(String.valueOf(bytesum*100/filelength))){
rate = Integer.parseInt(String.valueOf(bytesum*100/filelength));
pro.setValue(rate);
}
System.out.println("rate"+rate);
//System.out.println(bytesum);
//file copy or we can regard it as writing a new file
out.write(buff, 0, byteread);
}
in.close();
}
else{
System.out.println("Old_file open WRONG!!!!!1 ");
}
old_filepath = null;
new_filepath = null;
rate = 0;
}
catch(Exception e){
System.out.println("copy file wrong:"+e);
}
}
//action listener function
public void actionPerformed(ActionEvent e) {
if(e.getSource()==copy){
old_filepath = From_Path.getText();
new_filepath = To_Path.getText();
System.out.println(old_filepath);
}
}
//thread running per 1000ms
private class Run extends Thread{
public void run(){
//file copy test as follows
//String old_filepath = "C:/Users/huanglin/Pictures/Screenshots/屏幕截图.png";
//String new_filepath = "C:/Users/huanglin/Pictures/Screenshots/屏幕截图3.png";
while(true){
System.out.println("thread running");
if(old_filepath!=null){
copy();
}
try{
Thread.sleep(1000);
}
catch(Exception e){
System.out.println(e);
}
}
}
}
public static void main(String[]args){
//setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
File_Copy test = new File_Copy();
//window closed,system exit or close
test.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//window
test.setBounds(0, 0, 500, 300);
test.setVisible(true);
test.setLayout(null);
test.setResizable(false);
}
}