Java---记录用户名和密码,保存至隐藏文件

package com.lw;

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

public class KeyBoardListener extends JFrame {
    
    /**
     * 记录输入用户名和密码
     * 并保存到指定文件,将文件属性设置为隐藏文件
     */
    private static final long serialVersionUID = 7158985419563418289L;
    private JPanel contentPane;
    private JTextField textField;
    private JPasswordField passwordField;
    
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Throwable e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    KeyBoardListener frame = new KeyBoardListener();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    /**
     * Create the frame.
     */
    public KeyBoardListener() {
        setTitle("\u952E\u76D8\u8BB0\u5F55\u5668");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 250, 160);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new GridLayout(3, 1, 5, 5));
        
        JPanel panel1 = new JPanel();
        contentPane.add(panel1);
        
        JLabel label1 = new JLabel("\u7528\u6237\u540D\uFF1A");
        panel1.add(label1);
        
        textField = new JTextField();
        panel1.add(textField);
        textField.setColumns(10);
        
        JPanel panel2 = new JPanel();
        contentPane.add(panel2);
        
        JLabel label2 = new JLabel("\u5BC6    \u7801\uFF1A");
        panel2.add(label2);
        
        passwordField = new JPasswordField();
        passwordField.setColumns(10);
        panel2.add(passwordField);
        
        JPanel panel3 = new JPanel();
        contentPane.add(panel3);
        
        JButton button = new JButton("\u767B\u5F55");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_button_actionPerformed(e);
            }
        });
        panel3.add(button);
    }
    
    protected void do_button_actionPerformed(ActionEvent e) {
        String username = textField.getText();// 获得用户名
        String password = new String(passwordField.getPassword());// 获得密码
        if ((username.isEmpty()) || (password.isEmpty())) {// 判断用户名或密码是否为空
            JOptionPane.showMessageDialog(this, "用户名或密码为空!", null, JOptionPane.WARNING_MESSAGE);
            return;
        }
        File file = new File("d:/account.txt");// 创建文件保存账号信息
        String content = "用户名:" + username + " 密码:" + password;// 获得要写入的字符串
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);// 创建文件输出流对象
            //设置文件的隐藏属性
            String set = "attrib +H " + file.getAbsolutePath();
            System.out.println(set);
            Runtime.getRuntime().exec(set); 
            //将字符串写入到文件中
            fos.write(content.getBytes());
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        } finally {
            try {
                fos.close();// 关闭文件输出流
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        
    }
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值