java(包括springboot)读取resources下文件方式实现

 更新时间:2020年09月16日 10:53:08   作者:抄手砚  
这篇文章主要介绍了java(包括springboot)读取resources下文件方式实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

本文主要介绍了java(包括springboot)读取resources下文件方式实现,分享给大家,具体如下:

1、使用项目内路径读取,该路径只在开发工具中显示,类似:src/main/resources/resource.properties。只能在开发工具中使用,部署之后无法读取。(不通用)

File file = new File("src/main/resources/resource.properties");

@Test
 public void testReadFile2() throws IOException {
  File file = new File("src/main/resources/resource.properties");
  FileInputStream fis = new FileInputStream(file);
  InputStreamReader isr = new InputStreamReader(fis);
  BufferedReader br = new BufferedReader(isr);
  String data = null;
  while((data = br.readLine()) != null) {
   System.out.println(data);
  }
  
  br.close();
  isr.close();
  fis.close();
 }

2、使用org.springframework.util.ResourceUtils,读取。在linux环境中无法读取。(不通用)

File file = ResourceUtils.getFile("classpath:resource.properties");
FileInputStream fis = new FileInputStream(file);
@Test
 public void testReadFile3() throws IOException {
  File file = ResourceUtils.getFile("classpath:resource.properties");
  FileInputStream fis = new FileInputStream(file);
  InputStreamReader isr = new InputStreamReader(fis);
  BufferedReader br = new BufferedReader(isr);
  String data = null;
  while((data = br.readLine()) != null) {
   System.out.println(data);
  }
  
  br.close();
  isr.close();
  fis.close();
 }

3、使用org.springframework.core.io.ClassPathResource,各种环境都能读取。(通用)

Resource resource = new ClassPathResource("resource.properties");
InputStream is = resource.getInputStream();

@Test
 public void testReadFile() throws IOException {
//  ClassPathResource classPathResource = new ClassPathResource("resource.properties");
  Resource resource = new ClassPathResource("resource.properties");
  InputStream is = resource.getInputStream();
  InputStreamReader isr = new InputStreamReader(is);
  BufferedReader br = new BufferedReader(isr);
  String data = null;
  while((data = br.readLine()) != null) {
   System.out.println(data);
  }
  
  br.close();
  isr.close();
  is.close();
 }

4、结合spring注解,使用org.springframework.core.io.ResourceLoader;类的注解。(通用)

package com.tsinkai.ettp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class EttpCustomApplicationTests {

 @Autowired
 ResourceLoader resourceLoader;
 
 
 @Test
 public void testReaderFile() throws IOException {
  Resource resource = resourceLoader.getResource("classpath:resource.properties");
  InputStream is = resource.getInputStream();
  InputStreamReader isr = new InputStreamReader(is);
  BufferedReader br = new BufferedReader(isr);
  String data = null;
  while((data = br.readLine()) != null) {
   System.out.println(data);
  }
  
  br.close();
  isr.close();
  is.close();
 }

}

到此这篇关于java(包括springboot)读取resources下文件方式实现的文章就介绍到这了,更多相关java 读取resources内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • SpringBoot注入静态属性或静态对象的方法

    SpringBoot注入静态属性或静态对象的方法

    我们在使用SpringBoot为一些静态属性或者静态对象注入时会发现注入不成功,我们可以以下这几种方式把需要注入的值注入到静态属性中,感兴趣的朋友一起看下
    2024-12-12
  • SpringBoot项目中使用AOP的方法

    SpringBoot项目中使用AOP的方法

    本篇文章主要介绍了SpringBoot项目中使用AOP的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • java 读写锁的使用及它的优点

    java 读写锁的使用及它的优点

    这篇文章主要介绍了java 读写锁的使用及它的优点,读写锁的特点就是是读读不互斥、读写互斥、写写互斥,下面具体使用分享需要的小伙伴可以参考一下
    2022-05-05
  • JAVA中常见异常类

    JAVA中常见异常类

    本文主要介绍了JAVA中的常见异常类。具有很好的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • 一文带你掌握SpringBoot中常见定时任务的实现

    一文带你掌握SpringBoot中常见定时任务的实现

    这篇文章主要为大家详细介绍了Spring Boot中定时任务的基本用法、高级特性以及最佳实践,帮助开发人员更好地理解和应用定时任务,提高系统的稳定性和可靠性,需要的可以参考下
    2024-03-03
  • Java查看本机端口是否被占用源码

    Java查看本机端口是否被占用源码

    这篇文章主要介绍了Java查看本机端口是否被占用的主要原理,并结合具体实例给出了操作方法,需要的朋友可以参考下
    2017-09-09
  • spring-retry简单使用方法

    spring-retry简单使用方法

    这篇文章主要介绍了spring-retry简单使用方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04
  • Java 使用POI生成带联动下拉框的excel表格实例代码

    Java 使用POI生成带联动下拉框的excel表格实例代码

    本文通过实例代码给大家分享Java 使用POI生成带联动下拉框的excel表格,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2017-09-09
  • 详解Maven profile配置管理及激活profile的几种方式

    详解Maven profile配置管理及激活profile的几种方式

    这篇文章主要介绍了详解Maven profile配置管理及激活profile的几种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • SpringBoot与rabbitmq的结合的示例

    SpringBoot与rabbitmq的结合的示例

    这篇文章主要介绍了SpringBoot与rabbitmq的结合的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03

最新评论