拷贝文件

来源:互联网 发布:php oa办公系统源代码 编辑:程序博客网 时间:2024/05/16 12:06

拷贝文件:

package test;

import javax.swing.*;

import java.io.*;

publicclasstest 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);

       }

       }

    }

   

}

原创粉丝点击