java .propertise.json文件读写

properties便捷读取

  1. ResourceBundle

    ResourceBundle res = ResourceBundle.getBundle("ipConfig");
    String testIP = res.getString("testIP");
    
  2. spring注解

    //全局变量
    @Value("${testIP}")
    private String testIP;
    

properties

  1. 读取

    private String readPropertiesValue(String path, String key) {
    Properties prop = new Properties();
    String url = String.valueOf(String.class.getClassLoader().getResource(""));
    InputStream in = null;
    try {
        in = new BufferedInputStream(new FileInputStream(url.split("file:/")[1].split("WEB-INF")[0] + path));
        //读取属性文件a.properties
        prop.load(in);     ///加载属性列表
        Iterator<String> it = prop.stringPropertyNames().iterator();
        while (it.hasNext()) {
            String temp = it.next();
            if (key.equals(temp)) {
                return prop.getProperty(temp);
            }
        }
    
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (null != in) {
                in.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
    }
    

    2.写入 (只修改一个key,value)

    private static void writeProperties(String path, String key, String value) {
    Properties prop = new Properties();
    String url = String.valueOf(String.class.getClassLoader().getResource(""));
    
    InputStream in = null;
    FileOutputStream out = null;
    try {
    
        in = new BufferedInputStream(new FileInputStream(url.split("file:/")[1].split("WEB-INF")[0] + path));
        //读取属性文件a.properties
        prop.load(in);     ///加载属性列表
        out = new FileOutputStream((url.split("file:/")[1].split("WEB-INF")[0] + path));
        Iterator<String> it = prop.stringPropertyNames().iterator();
        while (it.hasNext()) {
            String temp = it.next();
            if (!kay.equals(temp)) {
                prop.setProperty(temp, prop.getProperty(temp));
            }
        }
        prop.setProperty(key, value);
        prop.store(out, (url.split("file:/")[1].split("WEB-INF")[0] + path));
    
    
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (null != out) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if (null != in) {
                in.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    }

.json

  1. 读取

    public static JSONObject getJSONObject(String path){
    String url=String.valueOf(String.class.getClassLoader().getResource(""));
    String path = url.split("file:/")[1].split("WEB-INF")[0]+path;
    BufferedReader reader = null;
    String jsonStr = "";
    try {
    	FileInputStream fileInputStream = new FileInputStream(path);
    	InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8");
    	reader = new BufferedReader(inputStreamReader);
    	String tempString = null;
    	while ((tempString = reader.readLine()) != null) {
    		jsonStr += tempString;
    	}
    	reader.close();
    } catch (IOException e) {
    	e.printStackTrace();
    } finally {
    	if (reader != null) {
    		try {
    			reader.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    if(null==jsonStr||"".equals(jsonStr)){
    	return null;
    }
    return JSONObject.fromObject(jsonStr);
    

    }

2.写入

public static void writeJson(String path, JSONObject json){
	OutputStreamWriter out = null;
	String url=String.valueOf(JsonUtil.class.getClassLoader().getResource(""));
	String path = url.split("file:/")[1].split("WEB-INF")[0]+name;
	try {
		File file = new File(path);
		if (!file.exists()) {
			file.createNewFile();
		}
		out = new OutputStreamWriter(new FileOutputStream(path),"utf-8");
		out.write(json.toString());
		out.close();
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		if (out != null) {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值