关于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
获取以后工程门路

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理