【SpringBoot下的文件读取】如何读取SpringBoot项目中resources中的文件(配置类、txt文件示例)

之前一直没有注意,当去加载resources下的配置文件时,忘记怎么做了,这次整理了一下这篇文章

一、加载配置文件方法

在这里插入图片描述
config.properties

appId=*******
apiKey=*******

PropertiesConfig

public class PropertiesConfig {
    private static final String APP_ID;
    private static final String API_KEY;
        static {
        Properties properties = new Properties();
        try {
            ClassPathResource resource = new ClassPathResource("config.properties");
            InputStream in = resource.getInputStream();
            BufferedReader bf = new BufferedReader(new InputStreamReader(in,"UTF-8"));
            properties.load(bf);
        } catch (IOException e) {
            throw new RuntimeException("加载配置文件失败");
        }
        APP_ID = properties.getProperty("appId");
        API_SECRET = properties.getProperty("apiSecret");
        }
    public static String getAppId() {
        return APP_ID;
    }

    public static String getApiKey() {
        return API_KEY;
    }

二、加载resources中的txt到map

public static Map<String,String> readTxtFile() {
        Map<String,String> map = new HashMap<>();
        try {
            final String[] txtFiles = new String[]{"xxxx.txt","xxxx.txt"};
            for (String fileName : txtFiles) {
                byte[] bytes;
                ClassPathResource classPathResource = new ClassPathResource(fileName);
                //获取文件流
                InputStream keyStream = classPathResource.getInputStream();
                bytes = IOUtils.toByteArray(keyStream);

                keyStream.read(bytes);
                keyStream.close();

                ByteArrayInputStream certBis = new ByteArrayInputStream(bytes);
                InputStreamReader input = new InputStreamReader(certBis);
                BufferedReader bf = new BufferedReader(input);
                String line;
                while((line=bf.readLine()) != null){
					//取到第一行的字符 line ,根据业务需要随意处理,这里是处理 :分隔 存了下map
                    String[] ms = line.split(":");
                    map.put(ms[0],ms[1]);
                }

            }
            log.info("------加载Q&A预设文件:  " + map);
            return map;
        } catch (IOException e) {
            log.error("加载Q&A预设文件失败",e);
        }
        return map;
    }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot项目,可以通过在`application.properties`或`application.yml`配置文件定义配置属性,然后在项目启动时使用`@Value`注解注入属性值来读取配置文件数据。 以`application.properties`为例,首先在`src/main/resources`目录下创建该文件,并定义需要读取配置属性,例如: ```properties # 数据库连接配置 spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=password ``` 然后在需要使用该配置属性的使用`@Value`注解注入属性值,例如: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class DatabaseConfig { @Value("${spring.datasource.url}") private String url; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; // getters and setters } ``` 在上面的代码,通过在`@Value`注解使用`${}`来引用配置属性,然后将属性值注入到对应的变量。 需要注意的是,使用`@Value`注解注入属性值的必须是Spring Bean,因此需要在上添加`@Component`注解或其它符合条件的注解,以便Spring能够扫描并创建该的实例。同时,需要在Spring Boot应用程序的入口上添加`@EnableConfigurationProperties`注解,以启用注入属性值的功能。 另外,如果需要读取`application.yml`配置文件的属性值,可以使用似的方式,并在`@Value`注解使用`:`来引用属性,例如: ```yaml # 数据库连接配置 spring: datasource: url: jdbc:mysql://localhost:3306/mydb username: root password: password ``` ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class DatabaseConfig { @Value("${spring.datasource.url}") private String url; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; // getters and setters } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值