GUI记录上次关闭位置,读取配置文件型

自己琢磨的一个Demo,运用到IO流,字符输入输出流,map集合,迭代器等等。。
作用:
打开软件时会检查D盘根目录是否有javaconfig.txt配置文件,没有则自动创建并设置隐藏,有就会读取文件里的x和y坐标,并赋予窗口;当关闭窗口时,如果坐标发生变化就会将当前新的坐标录入到配置文件中。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReadCofing {
    public static void main(String[] args) throws IOException {
        int windowx = 400;
        int windowy = 100;
        HashMap hm = new HashMap<String, String>();
        File file = new File("D:\\javaconfig.txt");
        //文件不存在则创建文件
        if (file.exists() == false) {
            file.createNewFile();
            System.out.println("创建文件成功");
        } else System.out.println("文件已存在");
        //将配置文件设置隐藏
        String sets = "attrib +H \"" + file.getAbsolutePath() + "\"";
        System.out.println(sets);
        Runtime.getRuntime().exec(sets);
        //当配置文件不为空时才读取
        if (file.length() != 0) {
            BufferedReader br = new BufferedReader(new FileReader(file));
            //创建字符串接收字符流读取的字符串
            String lines;
            while ((lines = br.readLine()) != null) {
                //通过String的split方法切割字符串并存入数组中
                String[] split = lines.split("=");
                //将切割好的字符串存入map集合中,确保成对才写入集合中
                if (split.length > 1)
                    //写入key和value
                    hm.put(split[0].trim(), split[1].trim());
            }
            //判断map集合是否有键值对
            if (hm.size() == 0) {
                System.out.println("配置文件内容为空");
            } else {
                //获取配置文件中x和y键对应的value值
                String x = (String) hm.get("x");
                String y = (String) hm.get("y");
                if (x == null || y == null) {
                    System.out.println("x或y参数为空,将使用默认坐标");
                } else {
                    //将string类型的数字转换为int类型的值
                    try {
                        windowx = Integer.parseInt(x);
                        windowy = Integer.parseInt(y);
                    } catch (NumberFormatException e) {
                        System.out.println(e);
                    }
                }
                br.close();
            }
        }

        JFrame jFrame = new JFrame();
        //窗口关闭,程序终止运行
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setLayout(new FlowLayout());
        jFrame.setBounds(windowx, windowy, 300, 100);
        jFrame.setTitle("读取上次位置");
        JButton jButton = new JButton("测试按钮");
        jFrame.add(jButton);
        jFrame.setVisible(true);
        //窗口关闭事件:获取坐标位置
        jFrame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                String x = String.valueOf(jFrame.getX());
                String y = String.valueOf(jFrame.getY());
                String x1 = null;
                String y1 = null;
                //有键有值时再进行字符串转换
                if (hm.get("x") != null && hm.get("y") != null) {

                    x1 = hm.get("x").toString();

                    y1 = hm.get("y").toString();
                }
                //坐标有不同时
                if (!(x.equals(x1)) || !(y.equals(y1))) {
                    try {
                        //将配置文件去除隐藏才有权限读写
                        String sets = "attrib -H \"" + file.getAbsolutePath() + "\"";
                        System.out.println(sets);
                        Runtime.getRuntime().exec(sets);
                        Thread.sleep(500);
                        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
                        //将当前坐标写入hashmap集合中
                        hm.put("x", jFrame.getX());
                        hm.put("y", jFrame.getY());
                        //将hashmap转换为键值对数组
                        Set set = hm.entrySet();
                        //使用迭代器遍历键值对数组
                        Iterator its = set.iterator();
                        while (its.hasNext()) {
                            Object next = its.next();
                            String s = next.toString();
                            //每次写入字符串并换行
                            bw.write(s);
                            bw.newLine();
                        }
                        bw.close();
                        //保存结束再配置文件设置隐藏
                        sets = "attrib +H \"" + file.getAbsolutePath() + "\"";
                        System.out.println(sets);
                        Runtime.getRuntime().exec(sets);

                    } catch (IOException e1) {
                        e1.printStackTrace();
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }

                }
            }
        });
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("按钮触发");

            }
        });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值