java项目中加载资源文件

在java项目中加载资源文件有三种方式:

  1. 使用绝对路径加载
  2. 利用classpath路径加载资源文件。
  3. 利用调用资源文件类的字节码所在的路径加载资源文件。
public class LoadResoureces2 {
	public static void main(String[] args) throws IOException {
		 load1();
         load3();
		 load2();


	}
	// 从classpath根目录下加载资源文件
	public static void load1() throws IOException {
		InputStream inputStream =LoadResoureces2.class.getClassLoader().getResourceAsStream("db.properties");
		//InputStream inputStream =Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties");
		Properties p = new Properties();
		p.load(inputStream);
		System.out.println(p);
		inputStream.close();
	}
	//从访问资源文件的类的字节码同级目录加载资源文件
	public static void load2() throws IOException {
		InputStream inputStream =LoadResoureces2.class.getResourceAsStream("dbio.properties");
		Properties p = new Properties();
		p.load(inputStream);
		System.out.println(p);
		inputStream.close();
	}

	//从资源的绝对路径来加载资源文件
	public static void load3() throws IOException {
		InputStream inputStream = new FileInputStream("E:/idea-workspace/LearnJavaByMike/resources/db1.properties");
		Properties p = new Properties();
		p.load(inputStream);
		System.out.println(p);
		inputStream.close();
	}
}

在项目中使用方式2来加载资源文件。

把资源文件放在项目resources 文件夹下,编译之后,资源文件就在classPath 路径下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot可以使用多种方式加载源文件。其一种方式是使用Spring的ResourceLoader接口来加载源文件。通过ResourceLoader的getResource()方法,可以传入源文件路径参数,返回一个Resource对象。然后可以通过Resource对象获取源文件的输入流,如下所示:\[1\] ```java @Autowired private ResourceLoader resourceLoader; public void readResourceFile() throws IOException { Resource resource = resourceLoader.getResource("classpath:file.txt"); InputStream inputStream = resource.getInputStream(); } ``` 另一种方式是使用FileSystemResource类加载源文件。这种方式适用于在使用Spring Boot插件打成jar包后,启动路径与IDEA的路径发生变化的情况。通过FileSystemResource类可以直接指定源文件的路径,如下所示:\[2\] ```java Resource resource = new FileSystemResource("path/to/file.txt"); InputStream inputStream = resource.getInputStream(); ``` 还可以使用ResourceUtils类来加载文件。ResourceUtils提供了一些静态方法,可以根据不同的前缀(如"classpath:"、"file:"等)来加载源文件,如下所示:\[3\] ```java Resource resource = ResourceUtils.getURL("classpath:file.txt"); InputStream inputStream = resource.getInputStream(); ``` 以上是Spring Boot加载源文件的几种常用方式。根据具体的需求和场景,选择合适的方式来加载源文件即可。 #### 引用[.reference_title] - *1* *3* [springboot项目读取 resources 目录下的文件的9种方式(总结)](https://blog.csdn.net/weixin_43025151/article/details/129819888)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [SpringBoot:加载和读取jar包外面的源文件](https://blog.csdn.net/weixin_45492007/article/details/122519728)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值