JAVA随笔篇三(读写Properties配置文件以及路径问题详解)

首先记录下读写配置文件的方法,很简单的两个方法,主要就是路径问题,稍后会详细解释。

1、读Properties配置文件

public static void loadConfig(Properties Config, String filename) {
		InputStream in = null;
		in = FileUtil.class.getClassLoader().getResourceAsStream(filename);
		try {
			Config.load(in);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

2、写Properties配置文件

public static void writePropertiesFile(String filename,
			Map<String, String> map) {
		Properties properties = new Properties();
		try {
			InputStream fis = FileUtil.class.getClassLoader().getResourceAsStream(filename);
			// 从输入流中读取属性列表(键和元素对)
			properties.load(fis);
			OutputStream outputStream = new FileOutputStream(filename);
			Iterator iter = map.entrySet().iterator();
			while (iter.hasNext()) {
				Map.Entry entry = (Map.Entry) iter.next();
				String key = (String) entry.getKey();
				String value = (String) entry.getValue();
				properties.setProperty(key, value);
			}
			properties.store(outputStream, "author: CFF");
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void writeProperties(String filename, String key, String value) {
		Properties properties = new Properties();
		try {
//			Logger.info(FileUtil.class.getClass().getResource("/").getPath());
			InputStream fis = FileUtil.class.getClassLoader().getResourceAsStream(filename);
			if(null==fis)Logger.info("文件不存在");
			
			// 从输入流中读取属性列表(键和元素对)
			properties.load(fis);
			Logger.info(properties.getProperty("endRunning"));
			Logger.info(properties.getProperty("pageNo"));
			OutputStream outputStream = new FileOutputStream(filename);
			properties.setProperty(key, value);
			properties.store(outputStream, "author: CFF");
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

两个方法是没错的,可是每次写入却莫名其妙不能修改配置文件,思前想后,发现路径问题乱的一塌糊涂。下面一点一点找到问题所在。
有一种说法是这样的:
Class.getResourceAsStream() 会指定要加载的资源路径与当前类所在包的路径一致。

例如你写了一个MyTest类在包com.test.mycode 下,那么MyTest.class.getResourceAsStream("name") 会在com.test.mycode包下查找相应的资源。如果这个name是以 '/' 开头的,那么就会从classpath的根路径下开始查找。

ClassLoader.getResourceAsStream()  无论要查找的资源前面是否带'/' 都会从classpath的根路径下查找。

首先配置文件名字写成/context-config.properties。
这里我们首先通过FileUtil.class.getClassLoader().getResource("/").getPath()拿到ClassLoader的读取资源路径
我这里路径是/F:/Work_Study/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/TimerTask/WEB-INF/classes/
下面果然有配置文件,修改其中配置文件值,同时将程序中resources目录(F:\Work_Study\TimerTask\src\main\resources)下的配置文件也修改了,修改成不同的值,跑起来看一下读取配置文件的过程到底是哪一个。
我这里做为Maven web项目启动的。运行之后发现,当resources目录下配置文件没修改,则直接读/F:/Work_Study/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/TimerTask/WEB-INF/classes/下的内容,resources目录下配置文件修改了则所有配置文件都修改成resources目录下的配置文件值。这个问题不大,因为是web项目重新部署了。
将配置文件名字写成context-config.properties。结果一样,看来ClassLoader的读取资源路径就是位置是不变的,都可以读取resources目录内容

这是读文件,下面说写文件。
写文件就很奇葩了,这里读的文件是resources目录下配置文件写文件却是写在了F盘根目录!思前想后,终于发现问题,原来代码在拷贝过程中没注意到输出路径,将输出路径修改成ClassLoader路径:
OutputStream outputStream = new FileOutputStream(FileUtil.class.getClassLoader().getResource("/").getPath()+filename);
果然修改了F:/Work_Study/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/TimerTask/WEB-INF/classes下的内容。但是怎么修改resources目录下的内容呢?
直接用"src/main/resources"+filename,不行;
System.getProperty("user.dir")拿到的不是工程的位置,却是eclipse的安装目录,不行。
用FileUtil.class.getClassLoader().getResource(".").getPath()拿到的是tomcat下lib的位置,醉了。

(1) 对于eclipse中的Java Application,System.getProperty("user.dir")的值为:E:\MyWorkspace\xiaoyingWebTest【项目路径】

(2) 对于eclipse中的动态web工程,System.getProperty("user.dir")的值为:D:\eclipse【eclipse安装路径】。

那怎么办呢?
虽然java application可以直接修改,但是对于web项目不适用,目前找不到修改resources目录下配置文件的方法,除非指定它的绝对路径,然而这完全没意义,所以。。。就不修改呗,,
如果有大神知道怎么修改,请指导一下小弟,感激不尽。。




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值