java中获取路径的几种方式
总是忘记, 备份一下,方便下次用. 第一种: File directory = new File("");//参数为空 String courseFile = directory.getCanonicalPath() ;System.out.println(courseFile); 结果:C:\Documents and Settings\Administrator\workspace\projectName获取当前类的所在工程路径; 第二种: File f = new File(this.getClass().getResource("/").getPath()); System.out.println(f); 结果:C:\Documents and Settings\Administrator\workspace\projectName\bin 如果不加“/” File f = new File(this.getClass().getResource("").getPath()); System.out.println(f); 结果:C:\Documents and Settings\Administrator\workspace\projectName\bin\com\test 第三种:URL xmlpath = this.getClass().getClassLoader(