properties文件的存取,追加存取

最近在写程序的时候,需要存些临时的数据信息,但是又不想存在数据库中.所以就存在磁盘上的某个文件中.存取的格式为properties

该代码在存取properties的时候,是追加存取.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class FileUtil {

    // 设置文件存取的路径
    public static String path = "C:/logs/tmp.properties";

    // 检测文件及路径是否存在,不存在则创建该文件及相关路径
    static{
        try{
            int index = path.lastIndexOf("/");
            String path1 = path.substring(0,index);
            File file = new File(path1);
            if(!file.exists()  && !file .isDirectory()){
                file.mkdirs();
            }
            file = new File(path);
            if(!file.exists()){
                file.createNewFile();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    // 根据键获取值
    public static String getValue(String key, String defaultValue) {
        try{
            Properties properties = new Properties();
            // 加载文件,获取值
            properties.load(new FileInputStream(path));
            return properties.getProperty(key,defaultValue);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static void setValue(String key, String value) {
        FileOutputStream outputStream = null;
        try{
            Properties properties = new Properties();
            // 加载文件
            properties.load(new FileInputStream(path));
            // 设置新值
            properties.setProperty(key,value);
            outputStream = new FileOutputStream(path);
            // 将修改后的值保存
            properties.store(outputStream,"保存数据");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(outputStream != null){
                    outputStream.close();
                    outputStream = null;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    // 根据Key删除相应的值
    public static void remove(String key) {
        FileOutputStream outputStream = null;
        try{
            Properties properties = new Properties();
            properties.load(new FileInputStream(path));
            properties.remove(key);
            outputStream = new FileOutputStream(path);
            properties.store(outputStream,"保存数据");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(outputStream != null){
                    outputStream.close();
                    outputStream = null;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    // 测试
    public static void main(String [] args){
        setValue("aaa","aaa");
        setValue("bbb","bbb");
        remove("bbb");
        System.out.println(getValue("aaa",null));
        System.out.println(getValue("bbb","bca"));
    }
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值