java读取大文件并添加入库,按行读取

文中流程有注释

public static void main(String[] args) {
        //文件地址(绝对路径)
        String Path = "D:\\tools\\products.json";
        BufferedReader reader = null;
        //读取数据拼接字符串
        String laststr = "";
        List reviewList = new ArrayList<>();
        //读取所用时间
        long timer = System.currentTimeMillis();
        try {
            //读取
            FileInputStream fileInputStream = new FileInputStream(Path);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
            reader = new BufferedReader(inputStreamReader);
            String tem = "";
            int i = 1;
            //循环读取,一次读取一行
            while ((tem = reader.readLine()) != null) {
                //创建对象 解析一行数据中的json数据
                Review review = new Review();
                review.setDate(JsonPath.read(tem, "$.time"));
                review.setReviewer(JsonPath.read(tem, "$.name"));
                review.setContent(JsonPath.read(tem, "$.text"));
                String images = JsonPath.read(tem, "$.image").toString();
                review.setImages(images.substring(1, images.length() - 1));
                review.setContent(JsonPath.read(tem, "$.country"));
                review.setStars(JsonPath.read(tem, "$.star"));
                try {
                    review.setProductId(Long.parseLong(JsonPath.read(tem, "$.product_id").toString()));
                } catch (PathNotFoundException p) {
                    continue;
                }
                //添加到list
                reviewList.add(review);
                //由于虚拟机内存原因,list一次我选择放入一万条数据后就清空
                if (reviewList.size() == 10000) {
                    //插入数据到数据库
                    int m = reviewService.addReviewList(reviewList);
                    reviewList.clear();
                }
                System.out.println(i);
                i++;
            }
            //将不足一万条的数据插入数据库
            int n=reviewService.addReviewList(reviewList);
            System.out.println(n);
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                    timer = System.currentTimeMillis() - timer;
                    System.out.println("处理时间:" + timer);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

你可能感兴趣的:(io)