java gradle 资源访问_java在gradle工程访问src/test/resources目录下的资源配置文件
package com.jiepu;
import java.io.File;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.Properties;
//java在gradle工程訪問src/test/resources或者src/main/resources目錄下的資源配置文件
public class TestMain
{
public static void main(String args[]) throws URISyntaxException {
System.out.println(new File(".").getAbsolutePath());
Properties properties=new Properties();
try {
// properties.load(new FileInputStream("config.properties"));
System.out.println(TestMain.class.getResource("/config.properties").toExternalForm());
System.out.println(Thread.currentThread().getContextClassLoader().getResource("config.properties"));
properties.load(TestMain.class.getResource("/config.properties").openStream());
} catch (Exception e) {
e.printStackTrace();
}
String version=properties.getProperty("version");
System.out.println(version);
for(Map.Entry entry:properties.entrySet())
{
Object key=entry.getKey();
Object value=entry.getValue();
System.out.println(key+"="+value);
}
}
}
[ java中讀取src文件下屬性文件(支持跨服務(wù)器) 目前的代碼如下: ? ? view plaincopy to clipboardprint import?java.io.File;?? import?java.io.Fil
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class MyUrlDemo {
public static void main(String[] args) {
MyUrlDemo muDemo = new MyUrlDemo();
try {
muDemo.showURL();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void showURL() throws IOException {
// 第一種:獲取類加載的根路徑 D:\git\daotie\daotie\target\classes
File f = new File(this.getClass().getResource("/").getPath());
System.out.println(f);
// 獲取當(dāng)前類的所在工程路徑; 如果不加“/” 獲取當(dāng)前類的加載目錄 D:\git\daotie\daotie\target\classes\my
File f2 = new File(this.getClass().getResource("").getPath());
System.out.println(f2);
// 第二種:獲取項(xiàng)目路徑 D:\git\daotie\daotie
File directory = new File("");// 參數(shù)為空
String courseFile = directory.getCanonicalPath();
System.out.println(courseFile);
// 第三種: file:/D:/git/daotie/daotie/target/classes/
URL xmlpath = this.getClass().getClassLoader().getResource("");
System.out.println(xmlpath);
// 第四種: D:\git\daotie\daotie
System.out.println(System.getProperty("user.dir"));
/*
* 結(jié)果: C:\Documents and Settings\Administrator\workspace\projectName
* 獲取當(dāng)前工程路徑
*/
// 第五種: 獲取所有的類路徑 包括jar包的路徑
System.out.println(System.getProperty("java.class.path"));
}
}
總結(jié)
以上是生活随笔為你收集整理的java gradle 资源访问_java在gradle工程访问src/test/resources目录下的资源配置文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql数据库(1):连接与断开服务器
- 下一篇: R之Excel文件读取与程序包的安装调用