java 路径获取

  1. 第一种:  
  2. File f = new File(this.getClass().getResource("/").getPath());  
  3. System.out.println(f);  
  4. 结果:  
  5. C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin  
  6. 获取当前类的所在工程路径;  
  7. 如果不加“/”  
  8. File f = new File(this.getClass().getResource("").getPath());  
  9. System.out.println(f);  
  10. 结果:  
  11. C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test  
  12. 获取当前类的绝对路径;  
  13.   
  14. 第二种:  
  15. File directory = new File("");//参数为空  
  16. String courseFile = directory.getCanonicalPath() ;  
  17. System.out.println(courseFile);  
  18. 结果:  
  19. C:\Documents and Settings\Administrator\workspace\projectName  
  20. 获取当前类的所在工程路径;  
  21.   
  22. 第三种:  
  23. URL xmlpath = this.getClass().getClassLoader().getResource("selected.txt");  
  24. System.out.println(xmlpath);  
  25. 结果:  
  26. file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selected.txt  
  27. 获取当前工程src目录下selected.txt文件的路径  
  28.   
  29. 第四种:  
  30. System.out.println(System.getProperty("user.dir"));  
  31. 结果:  
  32. C:\Documents and Settings\Administrator\workspace\projectName  
  33. 获取当前工程路径  
  34.   
  35. 第五种:  
  36. System.out.println( System.getProperty("java.class.path"));  
  37. 结果:  
  38. C:\Documents and Settings\Administrator\workspace\projectName\bin  
  39. 获取当前工程路径  


例子:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.vaolan.util;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.net.URL;  
  6.   
  7. public class PathUtils {  
  8.     /** 
  9.      * 获取工程根目录路径 
  10.      */  
  11.     public static String getRootPath() {  
  12.         return PathUtils.class.getResource("/").getPath();  
  13.     }  
  14.       
  15.     /** 
  16.      * 获取工程根目录路径 
  17.      */  
  18.     public static String getProjectPath() {  
  19.         File dir = new File("");  
  20.         String courseFile = null;  
  21.         try {  
  22.             courseFile = dir.getCanonicalPath();  
  23.         } catch (IOException e) {  
  24.             e.printStackTrace();  
  25.         }  
  26.         return courseFile;  
  27.     }  
  28.       
  29.     /** 
  30.      * 通过 user.dir 获取路径 
  31.      * @return 
  32.      */  
  33.     public static String getProjectPathByUser() {  
  34.         return System.getProperty("user.dir");  
  35.     }  
  36.       
  37.     /** 
  38.      * 获取当前项目的所有路径,包括 jar 包路径 
  39.      * @return 
  40.      */  
  41.     public static String getAllPaths() {  
  42.         return System.getProperty("java.class.path");  
  43.     }  
  44.       
  45.     /** 
  46.      * 获取当前类路径 
  47.      * @return 
  48.      */  
  49.     public static <T> String getCurPath(Class<T> t) {  
  50.         File f = new File(t.getResource("").getPath());  
  51.         return f.toString();  
  52.     }  
  53.       
  54.     /** 
  55.      * 获取当前资源所在路径:资源必存在于 src 目录下 
  56.      * @param resourceName 
  57.      * @return 
  58.      */  
  59.     public static String getCurPath(String resourceName) {  
  60.         URL path = PathUtils.class.getClassLoader().getResource(resourceName);  
  61.         return path.toString();  
  62.     }  
  63.       
  64.     public static void main(String[] args) {  
  65.         System.out.println(getRootPath());  
  66.         System.out.println(getCurPath(PathUtils.class));  
  67.         System.out.println(getCurPath("constant.properties"));  
  68.         System.out.println(getProjectPath());  
  69.         System.out.println(getProjectPathByUser());  
  70.         System.out.println(getAllPaths());  
  71.     }  
  72. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值