getClass().getResource() 方法获得相对路径( 此方法在jar包中无效。返回的内容最后包含/)。
例如 项目在/D:/workspace/MainStream/Test。
在javaProject中,getClass().getResource("/").getFile().toString() 返回:/D:/workspace/MainStream/Test/bin/。
public String getCurrentPath(){ 。
//取得根目录路径 。
String rootPath=getClass().getResource("/").getFile().toString(); 。
//当前目录路径 。
String currentPath1=getClass().getResource(".").getFile().toString(); 。
String currentPath2=getClass().getResource("").getFile().toString(); 。
//当前目录的上级目录路径 。
String parentPath=getClass().getResource("../").getFile().toString(); 。
return rootPath; 。
}
参考资料:http://blog.csdn.net/hpf911/article/details/5852127。
File f = new File(this.getClass().getResource("").getPath());。
System.out.println(f);结果:C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test。
获取当前类的绝对路径;第二种:File directory = new File("");//参数为空。
String courseFile = directory.getCanonicalPath() ;。
System.out.println(courseFile);结果:C:\Documents and Settings\Administrator\workspace\projectName。
获取当前类的所在工程路径;第三种:URL xmlpath = this.getClass().getClassLoader().getResource("selected.txt");。
System.out.println(xmlpath);结果:file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selected.txt。
获取当前工程src目录下selected.txt文件的路径第四种:System.out.println(System.getProperty("user.dir"));结果:C:\Documents and Settings\Administrator\workspace\projectName。
获取当前工程路径第五种:System.out.println( System.getProperty("java.class.path"));结果:C:\Documents and Settings\Administrator\workspace\projectName\bin获取当前工程路径。
因为你的项目的执行文件文件就在tomcat路径下呀,你可以把tomcat的项目路径指定为你项目的路径,这样运行文件就是在你的项目下了,获取的路径就是你真正项目的路径了。
在jsp和class文件中调用的相对路径不同。在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getProperty("user.dir")获取工程的绝对路径。
另:在Jsp,Servlet,Java中详细获得路径的方法!。
1.jsp中取得路径:
以工程名为TEST为例:
(1)得到包含工程名的当前页面全路径:request.getRequestURI()。
结果:/TEST/test.jsp。
(2)得到工程名:request.getContextPath()。
结果:/TEST
(3)得到当前页面所在目录下全名称:request.getServletPath()。
结果:如果页面在jsp目录下 /TEST/jsp/test.jsp。
(4)得到页面所在服务器的全路径:application.getRealPath("页面.jsp")。
结果:D:\resin\webapps\TEST\test.jsp。
(5)得到页面所在服务器的绝对路径:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();。
结果:D:\resin\webapps\TEST。
2.在类中取得路径:
(1)类的绝对路径:Class.class.getClass().getResource("/").getPath()。
结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/。
(2)得到工程的路径:System.getProperty("user.dir")。
结果:D:\TEST
3.在Servlet中取得路径:
(1)得到工程目录:request.getSession().getServletContext().getRealPath("") 参数可具体到包名。
结果:E:\Tomcat\webapps\TEST。
(2)得到IE地址栏地址:request.getRequestURL()。
结果:http://localhost:8080/TEST/test。
(3)得到相对地址:request.getRequestURI()。
结果:/TEST/test
File类有两个常用方法可以得到文件路径一个是:getCanonicalPath(),另一个是:getAbsolutePath(),可以通过File类的实例调用这两个方法例如file.getAbsolutePath()其中file是File的实例对象。下面是一个具体例子:
public class PathTest。
public static void main(String[] args)。
{
File file = new File(".\\src\\baidu");。
System.out.println(file.getAbsolutePath());。
try
{
System.out.println(file.getCanonicalPath());。
} catch (IOException e)。
{
e.printStackTrace();。
}
}
getAbsolutePath()和getCanonicalPath()的不同之处在于,getCanonicalPath()得到的是一个规范的路径,而getAbsolutePath()是用构造File对象的路径+当前工作目录。例如在上面的例子中.(点号)代表当前目录。getCanonicalPath()就会把它解析为当前目录但是getAbsolutePath()会把它解析成为目录名字(目录名字是点号)。
下面是上面程序在我电脑上的输出:
G:\xhuoj\konw\.\src\baidu。
G:\xhuoj\konw\src\baidu。