properties属性文件读写

在打包jar包中,和本地运行都没有问题
我本地的位置是
在这里插入图片描述
服务器的位置是
在这里插入图片描述

package com.ktamr.common.utils;



import java.io.*;
import java.util.Properties;


/**
 * 属性文件的IO操作
 * @author by小可爱
 *
 */
public class PropertiesUtil {

    private static String properiesName = "config/user.properties";

    public PropertiesUtil() {

    }

    public PropertiesUtil(String fileName) {
        this.properiesName = fileName;
    }

    /**
     * 按key获取值
     * @param key
     * @return
     */
    public static String readProperty(String key) {
        String value = "";
        InputStream is = null;
        try {
            try {
                is = new FileInputStream(properiesName);
            } catch (Exception e) {
                is = PropertiesUtil.class.getClassLoader().getResourceAsStream(properiesName);
            }
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            Properties p = new Properties();
            p.load(br);
            value = p.getProperty(key);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return value;
    }

    /**
     * 获取整个配置信息
     * @return
     */
    public static Properties getProperties() {
        Properties p = new Properties();
        InputStream is = null;
        try {
            try {
                is = new FileInputStream(properiesName);
            } catch (Exception e) {
                is = PropertiesUtil.class.getClassLoader().getResourceAsStream(properiesName);
            }
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            p.load(br);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return p;
    }

    /**
     * key-value写入配置文件
     * @param key
     * @param value
     */
    public static void writeProperty(String key, String value) {
        InputStream is = null;
        OutputStream os = null;
        Properties p = new Properties();
        try {
            try {
                //本地文件位置
                is = PropertiesUtil.class.getClassLoader().getResourceAsStream(properiesName);
                os = new FileOutputStream(PropertiesUtil.class.getClassLoader().getResource(properiesName).getFile());
            } catch (Exception e) {
                //如果报错就是文件未找到,服务器的位置
                is = new FileInputStream(properiesName);
                os = new FileOutputStream(properiesName);
            }
            System.out.println();
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            p.load(br);


            System.out.println();
            p.setProperty(key, value);
            p.store(os, key);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != is)
                    is.close();
                if (null != os)
                    os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }






}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hello Bug

谢谢老板,老板大气,老板硬邦邦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值