使用java创建GUI程序并解压文件进行另外存储


import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.*;

import java.nio.charset.Charset;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

import java.util.zip.ZipInputStream;

import javax.swing.*;

public class GuiFile extends JFrame {

    public GuiFile() {

        this.setTitle("解压文件");

        this.setSize(800, 600);

        this.setLocationRelativeTo(null);

        this.setLayout(null);

        JLabel filepass = new JLabel("输入文件路径:");

        JLabel fileout = new JLabel("输出文件路径:");

        //创建输出文件路径的字体的位置
        fileout.setBounds(400, 5, 80, 30);

        //创建输入文件路径的字体的位置
        filepass.setBounds(20, 5, 80, 30);

        //创建输入文件文本框
        JTextField varfilepath = new JTextField(20);

        varfilepath.setBounds(100, 5, 200, 30);

        //创建输出文件文本框
        JTextField varfileoutpath = new JTextField(20);

        varfileoutpath.setBounds(490, 5, 200, 30);

        //创建读取压缩文件按钮
        JButton btnShow = new JButton("读取压缩文件内的数据");

        btnShow.setBounds(220, 350, 150, 50);

        //创建写入文件按钮
        JButton btnWrite = new JButton("将读取的文件写入其他目录");

        btnWrite.setBounds(420, 350, 150, 50);

        //创建数据显示文本区域
        JTextArea ct2 = new JTextArea();

        ct2.setBounds(90, 50, 600, 300);

        ct2.setLineWrap(true);

        //创建文本框的滚动条
        JScrollPane scrollPane = new JScrollPane(ct2); // 创建滚动条面板

        scrollPane.setBounds(90, 50, 600, 300); // 设置面板大小和位置

        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        this.add(filepass);

        this.add(fileout);

        this.add(varfilepath);

        this.add(varfileoutpath);

        this.add(btnShow);

        this.add(btnWrite);

        this.add(ct2);

        this.add(scrollPane);

        btnShow.addActionListener(new ActionListener() {// 为按钮btnShow添加一个动作监听器,当按钮被点击时触发

            public void actionPerformed(ActionEvent e) {// 监听器的回调方法,当按钮被点击时执行

                String path = varfilepath.getText();// 获取文本框varfilepath中的文本内容,并赋值给变量path

                String outpath = varfileoutpath.getText();// 获取文本框varfileoutpath中的文本内容,并赋值给变量outpath

                File file = new File(path); // 要读取以上路径的input。txt文件,使用path创建一个File对象,表示一个文件或目录

                System.out.println(file.getName());// 打印文件名

                StringBuffer sb = new StringBuffer(); // 创建一个StringBuffer对象,用于存储读取的文本内容

                //创建zip文件路径
                try {

                    ZipFile zf = new ZipFile(path);// 使用path创建一个ZipFile对象,表示一个ZIP文件

                    InputStream an = new BufferedInputStream(new FileInputStream(path));// 从path文件中创建一个InputStream对象

                    Charset gbk = Charset.forName("utf-8");  // 创建一个Charset对象,表示使用的字符集编码为utf-8

                    ZipInputStream zin = new ZipInputStream(an, gbk);// 使用上面的InputStream对象和字符集utf-8创建一个ZipInputStream对象

                    ZipEntry ze;// 声明一个ZipEntry对象ze,用于表示ZIP文件中的一个条目

                    while ((ze = zin.getNextEntry()) != null) {// 循环遍历ZIP文件中的条目,直到遍历完成或ze为null

                        if (ze.toString().endsWith("txt")) {// 如果当前条目以"txt"结尾(即当前条目是一个文本文件)

                            // 创建一个BufferedReader对象br,用于读取当前条目的内容
                            BufferedReader br = new BufferedReader(new InputStreamReader(zf.getInputStream(ze)));

                            String line;// 声明一个字符串变量line,用于存储读取的每一行文本内容

                            while ((line = br.readLine()) != null) { // 循环读取当前条目的每一行内容,直到读取完成或line为null

                                sb.append(line);

                            }

                            br.close();

                        }

                        System.out.println();

                    }

                    zin.closeEntry();

                }

                catch (FileNotFoundException e1) {

                    e1.printStackTrace();

                    JOptionPane.showConfirmDialog(null, "操作错误!!!");
                }

                catch (IOException e1) {

                    e1.printStackTrace();

                    JOptionPane.showConfirmDialog(null, "操作错误!!!");

                }

                System.out.println(sb.toString());

                ct2.setText(sb.toString());// 将缓冲区中的文本设置为文本区域的内容
            }
        });
        btnWrite.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                String path = varfileoutpath.getText();

                try (BufferedWriter out = new BufferedWriter(

                        new OutputStreamWriter(new FileOutputStream(path, true)))) {

                    out.write(ct2.getText() + "\n");

                    out.close();

                } catch (IOException e1) {

                    e1.printStackTrace();

                    JOptionPane.showConfirmDialog(null, "操作错误!!!");
                }

                JOptionPane.showConfirmDialog(null, "已经将内容写入文件中,请查看!!");
            }
        });

        this.setVisible(true);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        {
            try {

                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {

                    if ("Nimbus".equals(info.getName())) {

                        javax.swing.UIManager.setLookAndFeel(info.getClassName());

                        break;

                    }

                }

            } catch (Exception e) {

                System.out.println(e);

            }

        }

        new GuiFile();
    }
}
  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值