文件 结构
test
test.java
test.propertiespackage test;import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Properties;public class test {
public String getpath() {
return this.getClass().getResource("").getPath();
} public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
String name = URLDecoder.decode(new test().getpath(),"utf-8")+ "test.properties";
System.out.println(name.substring(1, name.length()));
InputStream in = ClassLoader.getSystemResourceAsStream(name.substring(1, name.length()));
Properties p = new Properties();
try {
p.load(in);
} catch (IOException e) {
e.printStackTrace();
} }}String name = URLDecoder.decode(new test().getpath(),"utf-8")+ "test.properties";
/C:/Documents and Settings/Administrator/workspace/web/WebRoot/WEB-INF/classes/test/test.properties
为什么前面还加一个/
name.substring(1, name.length())我改成这样
下面 打印出来 C:/Documents and Settings/Administrator/workspace/web/WebRoot/WEB-INF/classes/test/test.properties
p.load(in);报错 为什么 ?我路径 对的 啊
test
test.java
test.propertiespackage test;import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Properties;public class test {
public String getpath() {
return this.getClass().getResource("").getPath();
} public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
String name = URLDecoder.decode(new test().getpath(),"utf-8")+ "test.properties";
System.out.println(name.substring(1, name.length()));
InputStream in = ClassLoader.getSystemResourceAsStream(name.substring(1, name.length()));
Properties p = new Properties();
try {
p.load(in);
} catch (IOException e) {
e.printStackTrace();
} }}String name = URLDecoder.decode(new test().getpath(),"utf-8")+ "test.properties";
/C:/Documents and Settings/Administrator/workspace/web/WebRoot/WEB-INF/classes/test/test.properties
为什么前面还加一个/
name.substring(1, name.length())我改成这样
下面 打印出来 C:/Documents and Settings/Administrator/workspace/web/WebRoot/WEB-INF/classes/test/test.properties
p.load(in);报错 为什么 ?我路径 对的 啊
解决方案 »
- java判断文本末尾的回车符
- this.getClass().getClassLoader().getResource("/").getPath()问题
- 小问题一个!!!
- java connected to sqlserver2000!非常郁闷
- 急!!!请教时间问题!!
- 匹配这个字符串的正则表达式怎么写?"用户1-ww-1-8#用户2-eeeee-1-9#……"其中……代表可能有一个或者多个!谢谢!
- 请教deprecated methods的用法
- 简单负值问题
- JDBC问题:如何查询SQL的"用户"类型表??
- awt类和Swing类在功能上有何区别?
- 来看看这个问题,子页面向父页面传值
- 高手帮忙(swing编程问题)
InputStream in = ClassLoader.getSystemResourceAsStream("test.properties");
Properties p = new Properties();
p.load(in);
我新建了个 WEB 项目 这样 写 OK 的
但是 在我 原来的 项目中 就错了
他 读出来时 null。所以 我搞不懂了
只不过 多几个 包 应该没区别啊 。
或者 你们是怎么读 Properties 文件的
我用你们的 方法 也行
System类的 static String getProperty(String key) ;
这个方法读取的
因为 是 实验 是 用 public static void main(String[] args)
调用 这个方法的
但 我 自己 做的项目 是 不是 public static void main(String[] args)
调用的 会不会 是 这个问题 ?
key 写什么 ?
我这样 写 读出来 null....