📄 filecopywithbar.java
字号:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import javax.swing.JFrame;
import java.awt.FlowLayout;
import javax.swing.JProgressBar;
import javax.swing.JLabel;
class FileCopyWithBar{
public static void main(String[] args){
if(args.length!=2)
System.out.println("Usage:FileCopyWithBar file1 file2");
else{
File f_obj1=new File(args[0]);
File f_obj2=new File(args[1]);
try{
if(!f_obj1.isFile())
System.out.println(args[0]+"文件并不存在,无法拷贝.");
else{
FileInputStream fis=new FileInputStream(f_obj1);
BufferedInputStream bis=new BufferedInputStream(fis);
FileOutputStream fos=new FileOutputStream(f_obj2);
BufferedOutputStream bos=new BufferedOutputStream(fos);
long file_length=f_obj1.length();
long block=file_length/10000;
ShowJProgressBar showJProgressBar1=new ShowJProgressBar();
int f_data;
long count=1;
int bar_value=1;
do{
f_data=bis.read();
if(f_data!=-1){
bos.write(f_data);
count++;
if(count%block==0){
bar_value++;
showJProgressBar1.setValue(bar_value);
}
}
}while(f_data!=-1);
showJProgressBar1.setValue(100);
bis.close();
bos.close();
}
}catch(IOException e){
System.out.println("文件IO出错!");
}
}
}
}
class ShowJProgressBar extends JFrame{
FlowLayout flowLayout1=new FlowLayout();
JProgressBar jProgressBar1=new JProgressBar(1,100);
JLabel jLabel1=new JLabel("拷贝进度:");
ShowJProgressBar(){
super("进度条示例");
setSize(300,80);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(flowLayout1);
this.getContentPane().add(jLabel1);
jProgressBar1.setStringPainted(true);
this.getContentPane().add(jProgressBar1);
show();
}
public void setValue(int n){
jProgressBar1.setValue(n);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -