java读取在resources目录下的文件内容

该文章已生成可运行项目,

总结:

1.非静态方法

getClass().getClassLoader().getResourceAsStream("config.properties"))

2.静态方式

ClassName.class.getClassLoader().getResourceAsStream("config.properties"))

1.非静态方法调用

//在代码工程src子目录main目录下,有java和resources两个目录,

//在resources目录下,假若有projectData.json文件,读取方式如下,

//注意:要先编译,这样在与src并列的target目录的子目录classes下才有这个projectData.json文件

InputStream inputStream = this.getClass().getResourceAsStream("/projectData.json");
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();
            while (line != null) {
                sb.append(line);
                line = br.readLine();
            }
            String longJsonString = sb.toString();
           //处理具体业务逻辑
        } catch (IOException e) {
            e.printStackTrace();
        }

2.静态方法调用getResourceAsStream ,使用ClassName.class而不是getClass()

package com.mkyong;
 
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
 
public class FileHelper {
 
    public static void main(String[] args) {
 
        System.out.println(getFilePathToSaveStatic());
    }    
 
    public static String getFilePathToSaveStatic() {
 
        Properties prop = new Properties();
 
        String result = "";
 
        try (InputStream inputStream = FileHelper.class
                .getClassLoader().getResourceAsStream("config.properties")) {
                
            prop.load(inputStream);
            result = prop.getProperty("json.filepath");
 
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        return result;
 
    }
 
}

3.应用


3.1读文件

public class FileUtil {
    public static String importFileData(String filePath) throws Exception {
        InputStream inputStream = null;
        try {
            //1.非静态方法:
//getClass().getClassLoader().getResourceAsStream("config.properties"))

            //2.静态方法
//ClassName.class.getClassLoader().getResourceAsStream("config.properties"))

            inputStream = FileUtil.class.getResourceAsStream(filePath);
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();
            while (line != null) {
                sb.append(line);
                line = br.readLine();
            }
            String longJsonString = sb.toString();
            log.info("longJsonString=" + longJsonString);
            return longJsonString;
        } catch (IOException e) {
            log.error(e.getMessage());
        } finally {
            if (Objects.nonNull(inputStream)) {
                inputStream.close();
            }
        }
        return null;
    }
}

3.2JSONObject.parseArray解析并保存

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

String jsonContent = FileUtil.importFileData("/traffic_line.json");
/*
JSONArray jsonArray = JSONObject.parseArray(jsonContent);
for (int i = 0; i < jsonArray.size(); i++) {
    JSONObject obj = jsonArray.getJSONObject(i);
    log.info("doProcessData=" + obj.toString());
}
*/
List<CityTrafficLine> poList = JSONArray.parseArray(jsonContent, CityTrafficLine.class);
cityTrafficLineDao.saveAll(poList);

本文章已经生成可运行项目
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值