关于JAVA 获取src目录包文件下的文件 路径问题

本文介绍了一种使用Java来加载配置文件的方法,展示了多种路径获取和资源读取的技术,包括通过Class、ClassLoader以及File对象等不同途径来定位并加载geocoding.properties文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public static void main(String[] args) {
InputStream in = null;
File file = new File("com/teamax/geocoding/config/geocoding.properties");
String filePath = file.getAbsolutePath();
System.out.println(filePath);

String url = Thread.currentThread().getContextClassLoader().getResource("com/teamax/geocoding/config/geocoding.properties").getPath().substring(1);
System.out.println(url);

URL url2 = ClassLoader.getSystemResource("com/teamax/geocoding/config/geocoding.properties");
System.out.println(url2.toString());

String str = System.getProperty("com/teamax/geocoding/config/geocoding.properties");
System.out.println(str);

in = test.class.getResourceAsStream("../config/geocoding.properties");
System.out.println(in.toString());

ClassLoader cl = Thread.currentThread().getContextClassLoader();
InputStream stream = cl.getResourceAsStream("com/teamax/geocoding/config/geocoding.properties");
System.out.println(stream.toString());

test t = new test();
t.test();

}

public void test(){
File file2=new File(this.getClass().getResource("/").getPath().substring(1)+"com/teamax/geocoding/config/geocoding.xml");
String xmlPath = file2.getAbsolutePath();
System.out.println(xmlPath);
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值