关于绝对路径和相对路径:
绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:C:xyz est.txt 表明了test.txt文件的绝对路径。http://www.sun.com/index.htm也表明了一个URL绝对路径。相对路径:相对与某个基准目录的路径。包含Web的相对路径(HTML中的相对目录),例如:在Servlet中,"/"表明Web应用的跟目录。和物理路径的相对表示。例如:"./" 表明当前目录,"../"表明上级目录。这种相似的表示,也是属于相对路径。另外关于URI,URL,URN等内容,请参考RFC相关文档标准。RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax,(http://www.ietf.org/rfc/rfc2396.txt)2.关于JSP/Servlet中的相对路径和绝对路径。2.1服务器端的地址服务器端的相对地址指的是相对于你的web应用的地址,这个地址是在服务器端解析的(不一样于html和java script 中的相对地址,他们是由客户端浏览器解析的)
第一种:
html
File f = new File(this.getClass().getResource("/").getPath()); System.out.println(f);
File f = new File(this.getClass().getResource("").getPath()); System.out.println(f);
File directory = new File("");//参数为空 String courseFile = directory.getCanonicalPath() ; System.out.println(courseFile);
URL xmlpath = this.getClass().getClassLoader().getResource("selected.txt"); System.out.println(xmlpath);
System.out.println(System.getProperty("user.dir"));
System.out.println( System.getProperty("java.class.path"));结果: C:\Documents and Settings\Administrator\workspace\projectName\bin 获取当前工程路径 这些都是针对当前工程的,应该还有其余方式的,欢迎补充!