copyFile

本文介绍了一个简单的Java应用程序,用于实现文件的查找与复制功能。通过图形界面,用户可以选取原始文件和目标位置,程序将文件从源位置复制到指定的目标位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package copyfile;

import java.awt.BorderLayout;

import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.filechooser.FileSystemView;
import java.awt.Rectangle;

public class CopyFile extends JFrame {
    JLabel lbl_resoure = new JLabel();
    JTextField txt_resoure = new JTextField();
    JButton btn_find = new JButton();
    JButton btn_copy = new JButton();
    private File fileOld = null;
    private File fileNew = null;
    Container container;
    JLabel lbl_new = new JLabel();
    JTextField txt_new = new JTextField();
    JButton btn_mark = new JButton(); ;

    public CopyFile() {
        try {
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        container = this.getContentPane();
        getContentPane().setLayout(null);
        lbl_resoure.setText("原文件");
        lbl_resoure.setBounds(new Rectangle(16, 13, 54, 24));
        txt_resoure.setBounds(new Rectangle(85, 10, 115, 28));
        btn_find.setToolTipText("");
        txt_resoure.setEditable(false);
        btn_copy.setBounds(new Rectangle(101, 73, 117, 27));
        btn_copy.setText("复制");
        btn_find.setBounds(new Rectangle(224, 12, 29, 26));
        btn_find.setText("..");
        lbl_new.setToolTipText("");
        lbl_new.setText("新文件");
        lbl_new.setBounds(new Rectangle(16, 45, 54, 24));
        txt_new.setEditable(false);
        txt_new.setBounds(new Rectangle(83, 41, 115, 28));
        btn_mark.setBounds(new Rectangle(225, 43, 29, 26));
        btn_mark.setToolTipText("");
        btn_mark.setText("..");
        container.add(btn_copy);
        container.add(txt_resoure);
        container.add(lbl_new);
        container.add(txt_new);
        container.add(lbl_resoure);
        container.add(btn_find);
        container.add(btn_mark);

        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        this.setSize(300, 150);
        this.setTitle("复制文件练习");
        this.setLocation((screen.width - 200) / 2, (screen.height - 100) / 2);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        btn_find.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fileOld = findFile();
                if (fileOld == null) {
                    JOptionPane.showMessageDialog(container, "没有找到要复制的文件");
                } else {
                    txt_resoure.setText(fileOld.getName());
                }
            }
        });
        btn_mark.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                copyFile();
            }
        });
        btn_copy.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                toCopy() ;
            }
        });
    }

    private void copyFile() {
        JFileChooser jfilechooser = new JFileChooser(FileSystemView.
                getFileSystemView());
        /* jfilechooser.set
                 String[] str = {"*.mdb", "*.Txt", "*.doc"};
                 for (int i = 0; i < str.length; i++) {
            myFileFilter mff = new myFileFilter(str[i]);
            jfilechooser.addChoosableFileFilter(mff);
                 }*/

        int open_bool = jfilechooser.showSaveDialog(this);
        if (jfilechooser.ERROR_OPTION != open_bool) {
            {
                fileNew = jfilechooser.getSelectedFile();
                txt_new.setText(fileNew.getName());
            }
        }
    }

    private void toCopy() {
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        try {
            fis = new FileInputStream(fileOld);
            bis = new BufferedInputStream(fis);
            fos = new FileOutputStream(fileNew);
            bos = new BufferedOutputStream(fos);
            int count = bis.available();
            byte[] b = new byte[count];
            bis.read(b, 0, count);
            bos.write(b);
            bos.flush();
            //while()
        } catch (Exception eIO) {
            System.out.println(eIO.getMessage());
            eIO.printStackTrace();
        } finally {
            try{
                bis.close();
                fis.close();
                bos.close();
                fos.close();
            }catch(Exception eeee)
            {}
        }
    }

    private File findFile() {
        JFileChooser jfilechooser = new JFileChooser(FileSystemView.
                getFileSystemView());
        int boolOpen = jfilechooser.showOpenDialog(this);
        File file = null;
        if (jfilechooser.ERROR_OPTION != boolOpen) {
            file = jfilechooser.getSelectedFile();
        }
        return file;
    }

    public static void main(String[] args) {
        new CopyFile();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值