我的mail.properties文件放在WEB-INF目录下,我如何得到他的路径下面是方法片段/** *//**
* 初始化Configuration类
* @param filePath 要读取的配置文件的路径+名称
*/
public Configuration(String filePath)
{
propertie = new Properties();
try {
inputFile = new FileInputStream(filePath);
propertie.load(inputFile);
inputFile.close();
} catch (FileNotFoundException ex) {
System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在"+filePath.toString());
ex.printStackTrace();
} catch (IOException ex) {
System.out.println("装载文件--->失败!");
ex.printStackTrace();
}
}//end ReadConfigInfo()如何获取filePath用了getContextClassLoader().getResource("").getPath()等方法的的不到,希望大家帮帮忙,jdk1.6
* 初始化Configuration类
* @param filePath 要读取的配置文件的路径+名称
*/
public Configuration(String filePath)
{
propertie = new Properties();
try {
inputFile = new FileInputStream(filePath);
propertie.load(inputFile);
inputFile.close();
} catch (FileNotFoundException ex) {
System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在"+filePath.toString());
ex.printStackTrace();
} catch (IOException ex) {
System.out.println("装载文件--->失败!");
ex.printStackTrace();
}
}//end ReadConfigInfo()如何获取filePath用了getContextClassLoader().getResource("").getPath()等方法的的不到,希望大家帮帮忙,jdk1.6
用这个试试看
要是在.class文件夹下就用类加载器
要是在web应用下就用servletContext.getrealPath(文件相对于web应用根目录的相对url)
Inputstream instream= getClass().getResourceAsStream("sql.properties");
prop.load(instream);
///localhost/loa/WEB-INF/web.xml
System.out.println(url.getPath());
URLConnection conn = url.openConnection();
conn.connect();
InputStream input = conn.getInputStream();