java笔记----文件复制

/**---------------------2007年07月18号文件拷贝程序-------------------------
-------本程序还要进行完善本程序只是成功的让程序来拷贝文件-----
---------下一步完成文件夹及文件的创建复制------------------------
------------------让程序更有人性话-哈哈--------------------------------------
*/
import java.io.* ;
import java.awt.* ;
import javax.swing.* ;
import java.awt.event.* ;

public class FileCopy extends JFrame implements ActionListener{
    JTextField t_from = new JTextField() ;
    JTextField t_to = new JTextField() ;

    FileCopy(){
        //设置界面属性
        this.setTitle("文件拷贝") ;
        this.setSize(300 , 180) ;
        this.setResizable(false) ;
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
        
        //设置窗体居中
        int width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth() ;
        int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight() ;
        this.setLocation((width-300)/2 , (height-180)/2) ;
        
        //新建一大堆组件
        JLabel l_from = new JLabel("源文件") ;
        JLabel l_to = new JLabel("目标文件") ;
        
                
        JButton b_from = new JButton("源文件" ) ;
        JButton b_to = new JButton("目标文件") ;
        
        JButton b_copy = new JButton("复制");
        JButton b_reset = new JButton("重写") ;
                
        //注册监听器
        b_from.addActionListener(this) ;
        b_to.addActionListener(this) ;
        b_copy.addActionListener(this) ;
        b_reset.addActionListener(this) ;
        
        //设置输入面板
        JPanel p_input = new JPanel() ;
        p_input.setLayout(new GridLayout(2 , 2)) ;
        
        p_input.add(l_from) ;
        p_input.add(t_from) ;
        p_input.add(b_from) ;
        
        p_input.add(l_to) ;
        p_input.add(t_to) ;
        p_input.add(b_to) ;
        
        //设置按钮面板
        JPanel p_button = new JPanel() ;
        p_button.setLayout(new FlowLayout()) ;
        
        p_button.add(b_copy) ;
        p_button.add(b_reset) ;
        
        //布置整体面板
        this.setLayout(new GridLayout(2 , 1)) ;
        
        this.add(p_input ) ;
        this.add(p_button) ;
        
    }
    public static void main(String []args){
        FileCopy fc =  new FileCopy() ;
        
        fc.setVisible(true) ;
    }
    
    public void actionPerformed(ActionEvent e){
     if(e.getActionCommand().equals("源文件"))    {
         try {
             JFileChooser jfc = new JFileChooser() ;
             jfc.showOpenDialog(this) ;
             File f = jfc.getSelectedFile() ;
             
             if(f == null){
                 JOptionPane.showMessageDialog(this , "选择文件") ;
             }
             t_from.setText(f.toString()) ;    
            }
         catch (Exception ex) {
             JOptionPane.showMessageDialog(this , "选择文件") ;
         }
         
     }
    
     if(e.getActionCommand().equals("目标文件")){
         try {
             JFileChooser jfc = new JFileChooser() ;
             jfc.showSaveDialog(this) ;
             File f = jfc.getSelectedFile() ;
             
             if(f == null){
                 JOptionPane.showMessageDialog(this , "选择目录") ;
             }
             t_to.setText(f.toString()) ;    
         }
         catch (Exception ex) {
             JOptionPane.showMessageDialog(this , "选择目录") ;
         }
         
     }
    
     if(e.getActionCommand().equals("复制")){
            try {
                File f = new File(t_from.getText()) ;
                File outf = new File(t_to.getText()) ;
                
                byte tmp[] = new byte[8192] ;
                
                FileInputStream fis = new FileInputStream(f) ;
                FileOutputStream fos = new FileOutputStream(outf) ;
                
                int size = fis.available() ;
                
                if(size  < 8192){
                    for (int i = 0; i<size; i++){
                        fos.write(fis.read()) ;
                    }
                }else{
                    size = fis.available()/8192 ;
                    
                    for (int i = 0; i<size; i++){
                        fis.read(tmp) ;
                        fos.write(tmp) ;
                    }
                    
                    size = fis.available() ;
                    for (int i = 0; i<size; i++){
                        fos.write(fis.read()) ;
                    }
            
                }
                
                
            }
            catch (Exception ex) {
            }         
     }
    
     if(e.getActionCommand().equals("重写")){
            t_from.setText("") ;
            t_to.setText("") ;         
     }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值