properties 配置文件找不到路径

mimixi666 2013-08-29 08:20:31
 public class Utils {
private static Properties props = null;
private static final String PATH = "/config.properties";

/**
* 得到config.properties配置文件中的所有配置属性
*
* <A href="http://home.51cto.com/index.php?s=/space/34010" target=_blank>@return</A> Properties对象
*/
public static Properties getConfig() {
if (null == props) {
props = new Properties();
//InputStream in = Utils.class.getResourceAsStream(PATH);
InputStream in;
try {
in = new FileInputStream(Utils.class.getClass()
.getClassLoader().getResource(PATH).getPath());
props.load(in);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return props;
}

/**
* 获得config.properties配置文件的属性值
* */
public static String getValue(String name) {
if (null == props)
getConfig();
return props.getProperty(name);
}

/**
* 设置config.properties配置文件的属性
* */
public static void setConfig(String name, String value) {
if (props == null)
getConfig();
props.setProperty(name, value);
// saveConfig();
}

/**
* 保存数据
*/
public static void saveConfig() {

try {
FileOutputStream out = new FileOutputStream(new File(PATH));
props.store(out, null);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


自己写了个配置文件的工具类。。
用这个就能正确获得路径
in = Utils.class.getResourceAsStream(PATH);

而这种的话就不行
in = new FileInputStream(Utils.class.getClass().getClassLoader().getResource(PATH).getPath());

我本来是想这样的,可以修改配置文件的内容,但是百度了,
in = Utils.class.getResourceAsStream(PATH);
这种方法貌似是不能修改配置文件的。。。
只能通过其它方法,例如这种:
in = new FileInputStream(Utils.class.getClass().getClassLoader().getResource(PATH).getPath());
但是发觉无论我怎么改,都是找不对路径。。。
我的路径是放在src文件夹下 下面的:

现在主要的问题是:
props.store(out, null);
这个不起作用的,修改不了配置文件。。。

in = new FileInputStream(Utils.class.getClass().getClassLoader().getResource(PATH).getPath());
这种方法,百度上说,是能让
props.store(out, null);这个起作用
的,但是路径不行,找不对。。
求大家指教下。。。
有点点无奈,崩溃了。。。
居然被这种问题,拖住。。。
...全文
1152 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
tony4geek 2013-08-30
  • 打赏
  • 举报
回复
你把两种方法的path 打印出来看区别。
mimixi666 2013-08-30
  • 打赏
  • 举报
回复
引用 2 楼 luobuxiadeye 的回复:
你看看你的tomcat安装路径是不是有空格,这样获取的路径会自动把空格转义的,所以你无法获取正确路径。
不是tomcat服务器那些的,普通的,java程序。。。
剑神一笑 2013-08-30
  • 打赏
  • 举报
回复
package com.spring.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class PropertiesUtil {
	
	private static final String PROPERTIES_DEFAULT_PATH = "/config.properties";
	
	public static String getProp(String key){
		String result = "";
		Properties prop = new Properties();
		InputStream in = PropertiesUtil.class.getResourceAsStream(PROPERTIES_DEFAULT_PATH);
		try {
			prop.load(in);
			if(prop.containsKey(key)){
				result = (prop.get(key)+"").trim();
			}else{
				result = "";
			}
		} catch (IOException e) {
			e.printStackTrace();
		} 
		return result;
	}
	
	public static String getProp(String key,String path){
		String result = "";
		Properties prop = new Properties();
		InputStream in = PropertiesUtil.class.getResourceAsStream(path);
		try {
			prop.load(in);
			if(prop.containsKey(key)){
				result = (prop.get(key)+"").trim();
			}else{
				result = "";
			}
		} catch (IOException e) {
			e.printStackTrace();
		} 
		return result;
	}
	
	/**
	 *<p>功能描述:</p>
	 * @param args
	 * @return void
	 * @throws 	
	 **/
	public static void main(String[] args) {
		System.out.println(PropertiesUtil.getProp("jdbc.url"));
	}

}
我用的
Landor2004 2013-08-30
  • 打赏
  • 举报
回复
改了一点,我这好使。这里改的是classpath下的config.properties,而不是src下的
public class Utils {

	private static Properties props = null;
	private static final String PATH = "/config.properties";

	/**
	 * 得到config.properties配置文件中的所有配置属性
	 * 
	 * <A href="http://home.51cto.com/index.php?s=/space/34010"
	 * target=_blank>@return</A> Properties对象
	 */
	public static Properties getConfig() {
		if (null == props) {
			props = new Properties();
			// InputStream in = Utils.class.getResourceAsStream(PATH);
			InputStream in;
			try {
				in = Utils.class.getResourceAsStream(PATH);
				props.load(in);
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return props;
	}

	/**
	 * 获得config.properties配置文件的属性值
	 * */
	public static String getValue(String name) {
		if (null == props)
			getConfig();
		return props.getProperty(name);
	}

	/**
	 * 设置config.properties配置文件的属性
	 * */
	public static void setConfig(String name, String value) {
		if (props == null)
			getConfig();
		props.setProperty(name, value);
		// saveConfig();
	}

	/**
	 * 保存数据
	 */
	public static void saveConfig() {

		try {
			FileOutputStream out = new FileOutputStream(Utils.class.getResource("/config.properties").getPath());
			props.store(out, null);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(getConfig().getProperty("aaa"));
		setConfig("aaaa", "cccc");
		saveConfig();
	}

}
luobuxiadeye 2013-08-29
  • 打赏
  • 举报
回复
你看看你的tomcat安装路径是不是有空格,这样获取的路径会自动把空格转义的,所以你无法获取正确路径。
无聊找乐 2013-08-29
  • 打赏
  • 举报
回复
把path打出来看看不就知道了 映象中应该是 realpath或者absolutPath

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧