迟到的第14周作业

一、题目

编写一个应用程序,输入一个目录和一个文件类型,显示该目录下符合该类型的所有文件。之后,将这些文件中的某一个文件剪切到另外一个目录中。

二、源代码

package TewntyFiveth;
/*
 * 
 * 文件通过textfiled模块接受信息,然后根据不同的button按钮来进行不同的具体方法实现
 * 
 * 
 * 
 */

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.io.Writer;

@SuppressWarnings("serial")
public class Test extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;
    private JTextArea textArea;
    private JLabel label_2;
    private JTextField textField_2;
    private JButton button_1;
    private JTextField textField_3;

    class Ff implements FilenameFilter {
        String str = null;

        Ff(String str) {
            this.str = str;
        }

        public boolean accept(File dir, String name) {
            return name.endsWith(str);
        }
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Test frame = new Test();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Test() {
        setTitle("\u6587\u4EF6\u7B80\u5355\u64CD\u4F5C");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 650);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel label = new JLabel("\u8F93\u5165\u6587\u4EF6\u8DEF\u5F84\uFF1A");
        label.setBounds(43, 85, 105, 15);
        contentPane.add(label);

        textField = new JTextField();
        textField.setBounds(138, 82, 192, 21);
        contentPane.add(textField);
        textField.setColumns(10);

        JLabel label_1 = new JLabel("\u6587\u4EF6\u540E\u7F00\u540D\uFF1A");
        label_1.setBounds(43, 152, 72, 15);
        contentPane.add(label_1);

        textField_1 = new JTextField();
        textField_1.setBounds(138, 149, 192, 21);
        contentPane.add(textField_1);
        textField_1.setColumns(10);

        JButton button = new JButton("\u67E5\u8BE2");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {                        //显示文件目录下的所有匹配文件名称
                File f = new File(textField.getText());
                FilenameFilter Filename = new Ff(textField_1.getText());
                String str[] = f.list(Filename);
                for (int i = 0; i < str.length; i++) {
                    textArea.append(str[i] + "\t\n");
                }
            }
        });
        button.setBounds(147, 210, 93, 23);
        contentPane.add(button);

        textArea = new JTextArea();
        textArea.setBounds(43, 252, 340, 178);
        contentPane.add(textArea);

        label_2 = new JLabel("\u6587\u4EF6\u8F6C\u79FB\u8DEF\u5F84\uFF1A");
        label_2.setBounds(43, 516, 93, 15);
        contentPane.add(label_2);

        textField_2 = new JTextField();
        textField_2.setBounds(138, 467, 192, 21);
        contentPane.add(textField_2);
        textField_2.setColumns(10);

        button_1 = new JButton("\u64CD\u4F5C");

        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {                        //获取textfiled值,将不同的字符拼接组成路径,然后复制读取文件并写入新的文件里
                File readf = new File(textField.getText()+ textField_2.getText());
                File writef = new File(textField_3.getText()+ textField_2.getText());
                InputStream in = null;
                BufferedInputStream bin = null;
                Writer w = null;
                BufferedWriter bw = null;
                String Sget = new String();
                try {
                    in = new FileInputStream(readf);
                    bin = new BufferedInputStream(in);
                    byte[] b = new byte[1024];
                    int count = 0;
                    while ((count = bin.read(b, 0, 1024)) != -1) {
                        Sget = new String(b, 0, count);
                        // System.out.println(new String(b,0,count));
                    }
                    if (!writef.exists()) {// 判断文件夹是否创建,没有创建则创建新文件
                        writef.createNewFile();
                    }
                    //System.out.println(writef);
                    w = new FileWriter(writef);
                    bw = new BufferedWriter(w);
                    // Scanner sc=new Scanner(System.in);
                    // String s=sc.nextLine();
                    bw.write(Sget);
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                } finally {
                    try {
                        bin.close();
                        in.close();
                        bw.close();
                        w.close();
                        readf.delete();
                    } catch (Exception e) {
                        // TODO: handle exception
                        e.printStackTrace();
                    }
                }

            }
        });
        button_1.setBounds(147, 552, 93, 23);
        contentPane.add(button_1);

        JLabel lblNewLabel = new JLabel(
                "\u9700\u8F6C\u79FB\u6587\u4EF6\u540D\u79F0:");
        lblNewLabel.setBounds(43, 470, 93, 15);
        contentPane.add(lblNewLabel);

        textField_3 = new JTextField();
        textField_3.setBounds(138, 513, 192, 21);
        contentPane.add(textField_3);
        textField_3.setColumns(10);
    }
}

三、运行结果

 

 

posted @ 2019-12-07 09:54  薄暮JIM  阅读(153)  评论(0编辑  收藏  举报