您的位置:首页 > 编程语言 > Java开发

如何获得java工程或者java web工程中的src目录中的文件和webinf目录路径

2012-05-19 01:01 736 查看
1.获取java工程中src目录中的文件

示例:获取src目录下的test.properties文件

public class GetFileFromSrcPath

{

public static void main(String[] args)

{

InputStream is = GetFileFromSrcPath.class.getResourceAsStream("/test.properties");

}

}

2.获取java web工程中的webinf路径

request.getSession().getServletContext().getRealPath("/WEB-INF");

3.java web工程中的普通类获取webinf路径

public clas Test

{

public static void main(String[] args)

{

String tempPath = Test.class.getResource("/").getPath();    // 获取了工程中的classes目录(该目录位于webinf下面)

String webinfPath = tempPath.substring(0, tempPath.lastIndexOf("classes"));
// 取得webinf的路径

System.out.println(webinfPath);
// 在控制台中输出webinf的路径

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java web string class
相关文章推荐