我想把一些配置文件放在WEB-INF目录下,这样可以和class文件分开,利于管理,但不知道该怎样访问,请高手支招

解决方案 »

  1.   

    package com.test;import java.util.*;
    import java.io.*;public class Test1 {public String getPara(String fileName) {
    Properties prop= new Properties();
    try {
    InputStream is = getClass().getResourceAsStream(fileName);
    prop.load(is);
    if(is!=null) is.close();
    }
    catch(Exception e) {
    System.out.println(e+"file "+fileName+" not found");
    }
    return prop.getProperty("file_name");
    }
    public static void main(String[] args) {
    Test1 test = new Test1();
    System.out.println(test.getPara("db.property"));
    }
    }
      

  2.   

    我按你的方法,出现这个异常
    java.lang.NullPointerExceptionfile log4j_init.properties not found
    null
      

  3.   

    InputStream in = sc.getResourceAsStream("/WEB-INF/xxx.properties");sc是ServletContext