乐趣区

关于java:获取路径的多种方式

第一种:
File file=newFile(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
获取以后工程门路

退出移动版