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

来源:互联网 发布:奥凯软件 编辑:程序博客网 时间:2024/05/19 00:42

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的路径

}

}


原创粉丝点击