拷贝文件:
package test;
import javax.swing.*;
import java.io.*;
publicclass test extends JFrame{
publicstaticvoid copy(File s1,File s2){
FileInputStream f1=null;
FileOutputStream f2=null;
try{
f1=newFileInputStream(s1);
f2=new FileOutputStream(s2);
int read=-1;
while((read = f1.read())>=0){
f2.write(read);
}
}catch(Exception e){
}
finally{
try{
if(f1!=null)f1.close();
if(f2!=null)f2.close();
System.exit(0);
}catch(Exception e){
}
}
}
publicstaticvoid main(String[] args) {
File s1;
File s2;
int i=-1;
JFileChooser chooser=new JFileChooser();
i=chooser.showOpenDialog(chooser);
if(i==JFileChooser.APPROVE_OPTION)
{
s1=chooser.getSelectedFile();
i=chooser.showSaveDialog(chooser);
if(i==JFileChooser.APPROVE_OPTION){
s2=chooser.getSelectedFile();
copy(s1,s2);
}
}
}
}