java架构搭建(五)--properties文件

来源:互联网 发布:java 日志框架 性能 编辑:程序博客网 时间:2024/09/21 09:29

properties文件大家都用过,在spring中配置数据源都会使用。

同时我们也需要另外一个properties文件用于管理系统需要配置的参数。

这个一般会和配置spring的那个分离开,因为这个大多是业务上的。


比如我新建一个resource.properties,在源文件夹下。

把一些需要配置的参数就可以放在这个文件中,如果需要写数据,更需要和spring的分离。

因为写数据比较危险。

开始我把这个和spring的config.properties都放在了源文件夹,这样在读的时候没问题。但是在写的时候你会发现,程序实际上是对

发布到classes目录下的这个文件读写的。所以你在源文件夹中的那个其实只是一个备份而已。

所以我放到了webapp下,这样就是一个文件的读写。


下面是个工具类:

package org.base.util;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.Properties;/** * 对webapp\cfg\resource.properties的属性读写 * 必须保证这个文件存在 * @author lushuaiyin * */public class PropertiesUtil {public static Properties props=null;//把resource.properties文件放在了源文件夹,这样在写properties时,实际是针对发布到classes中的文件写数据的。//即源文件夹中的文件总是会被拷贝到classes目录下。所以你在源文件夹手动修改内容也没用。程序只修改classes中的那个,也读取那个。//为了统一路径,把文件放到webapp下。改用resourceWebAppPath。这样是为了实现手动修改文件,程序可以实时地从这个文件取值。    public static String resourcePath=PropertiesUtil.class.getClassLoader().getResource("").getPath()+"resource.properties";//作废    public static String resourceWebAppPath="";static{String basepath=resourceWebAppPath=PropertiesUtil.class.getClassLoader().getResource("").getPath();File fa=new File(basepath).getParentFile().getParentFile();String aa=(fa.getPath()+"\\cfg\\resource.properties").replace("\\", "/");//这里需要在wabapp下有cfg目录和resource.properties文件,不做空判断了,保证有就行File resourceFile=new File(aa);resourceWebAppPath=resourceFile.getPath();System.out.println(resourceWebAppPath);}     //根据key读取valuepublic static String readProperty(String key){String value=null;if (props == null) {props = new Properties();}BufferedInputStream bis=null;FileInputStream fis=null;try{fis=new FileInputStream(resourceWebAppPath);bis=new BufferedInputStream(fis);props.load(bis);value=props.getProperty(key);}catch (Exception e) {e.printStackTrace();}finally{           try {        if(bis!=null){bis.close();}if(fis!=null){fis.close();}} catch (IOException e) {e.printStackTrace();}}return value;}  public static void writeProperty(String key,String value) {if (props == null) {props = new Properties();}BufferedInputStream bis=null;FileInputStream fis=null;OutputStream os=null;BufferedOutputStream bos=null;try{fis=new FileInputStream(resourceWebAppPath);bis=new BufferedInputStream(fis);props.load(bis);os= new FileOutputStream(resourceWebAppPath);bos=new BufferedOutputStream(os);props.setProperty(key, value);//props.put(key, value);//这个方法和setProperty一样props.store(os,key);//第二个参数其实是注释,例#context}catch (Exception e) {e.printStackTrace();}finally{           try {           if(bos!=null){bos.close();}           if(os!=null){os.close();}           if(bis!=null){bis.close();}   if(fis!=null){fis.close();}} catch (IOException e) {e.printStackTrace();}}} public static void main(String[] ss){/* 原内容: context=123 */String sss=PropertiesUtil.readProperty("context");System.out.println("context:"+sss);//PropertiesUtil.writeProperty("context","sss1");String sssss=PropertiesUtil.readProperty("context");System.out.println("context修改后:"+sssss);/* * 打印: D:\ChinaDevelopmentBankAct\workSpace\frame\webapp\cfg\resource.propertiescontext:123context修改后:qqq这时你去打开resource.properties,发现里面的值已经变了:#context#Tue Apr 02 23:00:32 CST 2013context=qqq */}}








原创粉丝点击