Web工程中,想在java代码中读取src文件夹下的文件,代码如下:
Properties props = new Properties();
props.load(getClass().getClassLoader().getResourceAsStream("param.ini"));
System.out.println(props.getProperty("pageURL"));它会抛出java.lang.NullPointerException异常。那应如何读取src文件夹下的文件?

解决方案 »

  1.   

    用绝对路径吧,或者把文件放到WebRoot下,直接去访问
      

  2.   

    读取classpath下的资源文件 InputStream in=new BufferedInputStream(new FileInputStream(System.getProperty("java.class.path")+"\\test.ini")); 
    Properties p=new Properties();
    p.load(in);
    System.out.println(p.getProperty("aaa"));
      

  3.   

    String name = null;
    String password = null;
    String path = null;
    String className = null;
    InputStream is = ReadPropertyFile.class
    .getResourceAsStream("/test.ini");
    // 获取文件流
    // 实例化属性读取器
    Properties p = new Properties();
    // 加载文件流
    p.load(is);
    // 根据键 获取值
    name = p.getProperty("user");
    password = p.getProperty("password");
    path = p.getProperty("url");
    className = p.getProperty("driverclass");
    System.out.println(name);
    System.out.println(password);
    System.out.println(path);
    System.out.println(className);
      

  4.   

    //文件被放在对应classes的根路径下,既是调用类的class文件和资源文件放在一块
            props.load(getClass().getClassLoader().getResourceAsStream("param.ini")); 
      

  5.   

    sorry。说错了
    //文件被放在对应classes的根路径下,即是在src根目录下。编译时候在bin根目录下
            props.load(getClass().getClassLoader().getResourceAsStream("param.ini"));