springboot读取resource下的文件

本文介绍了一种从特定文件中读取设备类型映射表的方法,通过使用Java的BufferedReader和InputStreamReader,实现对UTF-8编码的文本文件进行逐行读取并解析。该方法能够处理以逗号分隔的数据,将每行数据分解为格式名称、说明和单位三部分。

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

 主要记录下读取方法 。内容不重要

public class TypeSimpleService {

    private Logger logger = LoggerFactory.getLogger(TypeSimpleService.class);

    /**
     * 读取deviceType映射表
     */
    public  void readText(){
        /* 读取数据 */
        InputStream inputStream = null;
        BufferedReader br = null;
        try {
            inputStream = this.getClass().getResourceAsStream("/importSignal");
            br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            String lineTxt;
            while ((lineTxt = br.readLine()) != null) {//数据以逗号分隔
                if(lineTxt.endsWith(",")){
                    lineTxt = lineTxt + " ";
                }
                String[] names = lineTxt.split(",");
                try{
                    //格式 名称,说明,单位
                    String typeName = names[0];
                    String type = names[1];
                    String unit = names[2];
                    logger.info("");
                }catch (Exception e){
                    logger.error("异常:{}",lineTxt);
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("read errors :" + e);
        }finally {
            if(inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        logger.info("初始化  完成  !! ");
    }
}

读取的文件:

importSignal内容:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值