文件 结构
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);报错 为什么 ?我路径 对的 啊 

解决方案 »

  1.   

    我也这样 干的 
    InputStream in = ClassLoader.getSystemResourceAsStream("test.properties");
    Properties p = new Properties();
    p.load(in);
    我新建了个  WEB 项目  这样 写 OK 的 
    但是   在我 原来的 项目中 就错了 
    他 读出来时 null。所以 我搞不懂了 
    只不过  多几个   包 应该没区别啊 。
    或者 你们是怎么读  Properties 文件的   
    我用你们的 方法 也行 
      

  2.   

    我用的是
        System类的  static String getProperty(String key)  ;
    这个方法读取的
      

  3.   

    我觉得 我错  的 可能 是在  静态 方面 
    因为  是 实验 是  用  public static void main(String[] args)
    调用   这个方法的 
    但  我 自己 做的项目 是 不是  public static void main(String[] args)
    调用的 会不会 是 这个问题 ?
      

  4.   

    System.getProperty("test.properties")
    key 写什么 ?
    我这样 写   读出来 null....