第一种:
File f = new File(this.getClass().getResource("/").getPath()); System.out.println(f);
效果:
C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin
猎取当前类的地点工程途径。
假如不加“/”
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
猎取当前工程途径。
ki4网,有大批免费的JAVA入门教程,迎接人人进修!
以上就是java怎样猎取文件途径的细致内容,更多请关注ki4网别的相干文章!