java web工程中读取properties文件,路径一直不知道怎么写

javaliuliu 2013-05-29 11:56:07
properties文件放在WEB-INF\config\uas-system.properties

我的代码是:

//根据key读取value
public static String readValue(String key) {
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream(new FileInputStream("/uas-operation.properties"));
props.load(in);
String value = props.getProperty(key);
System.out.println(key + value);
return value;
} catch (Exception e) {
e.printStackTrace();
return "";
}

试了很多路径都不对,请问这个路径问题怎么解决?谢谢
...全文
8691 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
铁衣 2015-04-08
  • 打赏
  • 举报
回复
// 先拿到..../WEB-INF/class/目录 // 此时path是以/开头的,比如/D:/tomcat5/...,需要把开始的/过滤掉 String path = Thread.currentThread().getContextClassLoader().getResource("").getPath(); // 读取文件到Stream流 InputStream in = new BufferedInputStream(new FileInputStream(path+fileName));
NEWS____ 2014-05-30
  • 打赏
  • 举报
回复
public static String readValue(String key) { Properties props = new Properties(); FileInputStream fis=null try { fis=new FileInputStream(new File(UasTagFunction.class.getResource("/").getPath()+"config/uas-system.properties")); props.load(fis); String value = props.getProperty(key); return value; } catch (Exception e) { e.printStackTrace(); return ""; } }
NEWS____ 2014-05-30
  • 打赏
  • 举报
回复
public static String readValue(String key) {
        Properties props = new Properties();
       FileInputStream fis=null
        try {
            fis=new FileInputStream(new File(UasTagFunction.class.getResource("/").getPath()+"config\uas-system.properties"));
            props.load(fis);
            String value = props.getProperty(key);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
kittaaron 2013-05-29
  • 打赏
  • 举报
回复
路径不是试出来的,建议楼主先了解清楚classpath的概念。你就不会不知道文件该放在哪儿了
rumlee 2013-05-29
  • 打赏
  • 举报
回复
为什么不直接放在WEB-INF\classes下面,如果直接放在WEB-INF\classes下面就很容易了。 直接用resourcebundle或者Class.getresourceasstream都能够很容易获取到。 如果放在WEB-INF的config目录,我估计得先获取工程的目录,然后再根据工程目录的相对目录找到文件。
javaliuliu 2013-05-29
  • 打赏
  • 举报
回复
算了,不纠结了,如果谁知道怎么把WEB-INF\config\uas-system.properties的文件读出来希望能把测试后的带发上来~ 我已经把properties文件放入classes文件夹下了,用以下代码可以:

public static String readValue(String key) {
		Properties props = new Properties();
		try {
			InputStream in=UasTagFunction.class.getResourceAsStream("/uas-operation.properties");
			props.load(in);
			String value = props.getProperty(key);
			return value;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
abc422114 2013-05-29
  • 打赏
  • 举报
回复
String path = SysConfig.class.getClassLoader().getResource("").getPath();
来逗她 2013-05-29
  • 打赏
  • 举报
回复
request.getSession().getServletContext().getRealPath("/WEB-INF/config/uas-system.properties");
跳蚤图 2013-05-29
  • 打赏
  • 举报
回复
直接写不行么 WEB-INF\config\uas-system.properties
javaliuliu 2013-05-29
  • 打赏
  • 举报
回复
哎,项目以前就是放在这个文件夹下的。我只是顺势而为,希望有人给出解决办法
ZHOU西口 2013-05-29
  • 打赏
  • 举报
回复
Class.getResourceAsStream(String path) path 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从ClassPath根下获取。
小沙同歇 2013-05-29
  • 打赏
  • 举报
回复
楼主可以在路径上面试试 “../”+你的路径,这种表示方法是往上一层,代码的运行是在classes包里面的,这样就可以跳出这个文件夹在父文件夹中选择包了,下面应该是config\~~~。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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