每日一个java算法小分享【081向properties中写入数据】

实例说明

    Properties属性文件是以key,value的形式保存数据,在key与value之间有一个“”=“”相连。如果通过手动的方法相属性文件写数据,可能会出现格式上的问题,本实例实现一个小工具,通过在窗体中输入内容,可实现向属性文件中写数据。

关键技术

    本实例实现设置Properties属性文件的值,使用的时Properties类的setProperty()方法。该方法的语法格式如下:

    setProperty(String key,String value)

参数说明:

1.key:要置于属性列表中的键

2.value:key值对应的value值

要将设置的属性文件信息通过流写入到属性文件中,需要使用Properties类的store()方法,具体语法格式如下:

store(outputStream out,String comments)

参数说明:

1.out:输出流

2.comments:对属性列表的描述信息

SavePropertiesFrame.java

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class SavePropertiesFrame extends JFrame {
    
    private JPanel contentPane;
    private JTextField keyTextField;
    private JTextField valueTextField;
    
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SavePropertiesFrame frame = new SavePropertiesFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    /**
     * Create the frame.
     */
    public SavePropertiesFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 374, 227);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        setTitle("向属性文件中写数据");
        JPanel panel = new JPanel();
        panel.setBounds(0, 0, 364, 262);
        contentPane.add(panel);
        panel.setLayout(null);
        
        JLabel keyLabel = new JLabel("Key值:");
        keyLabel.setBounds(41, 52, 54, 15);
        panel.add(keyLabel);
        
        JLabel valueLabel = new JLabel("Value值:");
        valueLabel.setBounds(34, 101, 61, 21);
        panel.add(valueLabel);
        
        keyTextField = new JTextField();
        keyTextField.setBounds(92, 49, 184, 21);
        panel.add(keyTextField);
        keyTextField.setColumns(10);
        
        valueTextField = new JTextField();
        valueTextField.setBounds(93, 101, 183, 21);
        panel.add(valueTextField);
        valueTextField.setColumns(10);
        
        JButton saveButton = new JButton("写入");
        saveButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                do_saveButton_actionPerformed(arg0);
            }
        });
        saveButton.setBounds(141, 149, 77, 23);
        panel.add(saveButton);
    }
    //写入按钮的单击处理事件
    protected void do_saveButton_actionPerformed(ActionEvent arg0) {
        String key = keyTextField.getText();
        String value = valueTextField.getText();
        if((!key.equals("")&&(key!=null)) && ((!value.equals(""))&&(value != null))){
            SaveProperties properties = new SaveProperties();
            properties.saveProperties(key, value);
            JOptionPane.showMessageDialog(getContentPane(), 
                    "数据写入成功!", "信息对话框", JOptionPane.WARNING_MESSAGE);
        }       
    

    }
}

SaveProperties.java

import java.io.FileOutputStream;
import java.util.Properties;

public class SaveProperties {

	/**
	 * @param args
	 */

	public void saveProperties(String key, String value) {
		Properties properties = new Properties();
		properties.setProperty(key, value);// 设置属性文件值
		try {
			FileOutputStream out = new FileOutputStream("message.properties"); // 创建输出流对象
			properties.store(out, "test"); // 将信息通过流写入文件
			out.close(); // 关闭输出流对象
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值