java中Properties对文件进行读写

本文介绍了一种利用Java的Properties类来实现配置文件的创建、写入和读取的方法。通过具体实例演示了如何根据当前用户的home目录来确定配置文件的位置,并详细展示了如何用Properties类来进行数据的存储和加载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

       1.获取系统用户目录的一个文件

       2.创建文件,用Propertes类写入数据到文件

       3.用Properties类读取文件的数据

 

public class CreateGroup extends PropertyPlaceholderConfigurer
{   
    public CreateGroup ()
    {
        init();
    }
    
    private void init()
    {
        try {
            String path = System.getProperty("user.home")+
                    File.separator+"dms-group.ini";
            Properties p = new Properties ();
            File f = new File (path);
            if (!f.exists ()||f.isDirectory ())
            {
                if (!f.createNewFile ())
                {
                    System.setProperty ("groupNameLC", "groupNameLC");
                    return;
                }
                OutputStream fos = new FileOutputStream(f);
                String name = "groupName"+new Date ().getTime ()+"GP";
                p.setProperty ("groupNameLC", name);
                p.store (fos, "######");
                fos.close ();
            }
            InputStream in = new FileInputStream (f); 
            p.load(in); 
            String gname = p.getProperty("groupNameLC").trim(); 
            System.setProperty ("groupNameLC", gname);
            in.close ();
        } catch (Exception e) { 
            e.printStackTrace();
        } 
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值