1. package com.jiepu;  

  2. import java.io.File;  

  3. import java.net.URISyntaxException;  

  4. import java.util.Map;  

  5. import java.util.Properties;  

  6.   

  7. //java在gradle工程访问src/test/resources或者src/main/resources目录下的资源配置文件  

  8. public class  TestMain  

  9. {  

  10.     public static  void main(String args[]) throws URISyntaxException {  

  11.         System.out.println(new File(".").getAbsolutePath());  

  12.         Properties properties=new Properties();  

  13.         try {  

  14.             // properties.load(new FileInputStream("config.properties"));  

  15.             System.out.println(TestMain.class.getResource("/config.properties").toExternalForm());  

  16.             System.out.println(Thread.currentThread().getContextClassLoader().getResource("config.properties"));  

  17.             properties.load(TestMain.class.getResource("/config.properties").openStream());  

  18.   

  19.         } catch (Exception e) {  

  20.             e.printStackTrace();  

  21.         }  

  22.         String version=properties.getProperty("version");  

  23.         System.out.println(version);  

  24.         for(Map.Entry<Object,Object>  entry:properties.entrySet())  

  25.         {  

  26.             Object key=entry.getKey();  

  27.             Object value=entry.getValue();  

  28.             System.out.println(key+"="+value);  

  29.         }  

  30.     }  

  31.   

  32. }  

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片

  1. import java.io.File;  

  2. import java.io.IOException;  

  3. import java.net.URL;  

  4.   

  5. public class MyUrlDemo {  

  6.   

  7.   

  8.     public static void main(String[] args) {  

  9.         MyUrlDemo muDemo = new MyUrlDemo();  

  10.         try {  

  11.             muDemo.showURL();  

  12.         } catch (IOException e) {  

  13.             // TODO Auto-generated catch block  

  14.             e.printStackTrace();  

  15.         }  

  16.     }  

  17.   

  18.     public void showURL() throws IOException {  

  19.   

  20.         // 第一种:获取类加载的根路径   D:\git\daotie\daotie\target\classes  

  21.         File f = new File(this.getClass().getResource("/").getPath());  

  22.         System.out.println(f);  

  23.   

  24.         // 获取当前类的所在工程路径; 如果不加“/”  获取当前类的加载目录  D:\git\daotie\daotie\target\classes\my  

  25.         File f2 = new File(this.getClass().getResource("").getPath());  

  26.         System.out.println(f2);  

  27.   

  28.         // 第二种:获取项目路径    D:\git\daotie\daotie  

  29.         File directory = new File("");// 参数为空  

  30.         String courseFile = directory.getCanonicalPath();  

  31.         System.out.println(courseFile);  

  32.   

  33.   

  34.         // 第三种:  file:/D:/git/daotie/daotie/target/classes/  

  35.         URL xmlpath = this.getClass().getClassLoader().getResource("");  

  36.         System.out.println(xmlpath);  

  37.   

  38.   

  39.         // 第四种: D:\git\daotie\daotie  

  40.         System.out.println(System.getProperty("user.dir"));  

  41.          /* 

  42.           * 结果: C:\Documents and Settings\Administrator\workspace\projectName 

  43.           * 获取当前工程路径 

  44.           */  

  45.   

  46.         // 第五种:  获取所有的类路径 包括jar包的路径  

  47.         System.out.println(System.getProperty("java.class.path"));  

  48.   

  49.     }  

  50. }