分批次读取csv文件,并保存到数据库

读取上百万行的csv文件,由于数据量太大,一次性将csv的内容读取出来,保存在内存中,会导致内存严重吃不消,最后直接宕机,所以建议采取分批次读数据然后保存数据库中,以下是简单测试方法,可根据具体需求做修改。对大批量数据的操作,建议用jdbc直接批量添加,修改,删除等操作。

import java.util.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class ReadCsv {
public static void main(String[] args) throws FileNotFoundException{
File csv = new File("D:\\test.csv"); // CSV文件路径
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(csv));
String line = "";
while ((line = br.readLine()) != null){ //读取到的内容给line变量
while(getList(br)){}
}
} catch (IOException e){
e.printStackTrace();
}
}

public static boolean getList(BufferedReader br){
List<String[]> allString = new ArrayList<>();
boolean status = false;
String everyLine = "";
try {
int index = 0;
while((everyLine = br.readLine()) != null){
String [] strList = everyLine.split(",");
System.out.println(everyLine);
allString.add(strList);
index ++;
if(index == 3){
status = true;
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("总条数为:"+ allString.size());
//取值
for (int i = 0; i < allString.size(); i++) {
System.out.println(allString.get(i)[0]);
}
//这里做新增操作,保存到数据库。。。。。。
return status;
}
}

时间: 2024-08-29 09:10:27

分批次读取csv文件,并保存到数据库的相关文章

Spring Batch示例: 读取CSV文件并写入MySQL数据库

Spring Batch示例: 读取CSV文件并写入MySQL数据库 GitHub版本: https://github.com/kimmking/SpringBatchReferenceCN/blob/master/01_introduction/Spring_Batch_MySQL.md 原文链接: Reading and writing CVS files with Spring Batch and MySQL 原文作者: Steven Haines - 技术架构师 下载本教程的源代码: S

读取csv文件,写入oracle数据库

/* * @(#)DataParse.java 2014年4月28日 */ package com.yihaodian.sa.doData; import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.sql.Connection;import java.

Spring Batch使用示例: 读取CSV文件并写入MySQL数据库

GitHub版本: https://github.com/kimmking/SpringBatchReferenceCN/blob/master/01_introduction/Spring_Batch_MySQL.md ------------ 编写批处理程序来处理GB级别数据量无疑是种海啸般难以面对的任务,但我们可以用Spring Batch将其拆解为小块小块的(chunk). Spring Batch 是Spring框架的一个模块,专门设计来对各种类型的文件进行批量处理. 本文先讲解一个简

Cocos2d-x Lua 读取Csv文件,更方便的使用数据

我的书上或者是我曾经出售的源码里,都有Csv文件的影子. 也许是先入为主吧,我工作那会用的最久的配置文件就是Csv,所以我在很多游戏里都会情不自禁地优先选择它. Csv文件,格式很简单,就是一行一条数据,字段之间用逗号分隔,策划也可以方便地使用Excel进行编辑. Csv格式的文件,解析起来也很简单,所以自己动手写写很快~(小若:我就喜欢拿来主义,你怎么着) 最近在用Lua写游戏,对于技能.怪物等配置,我还是选择用Csv~ 不得不说,Lua等脚本语言,在某些方面是C++没法比的,这次我就用Csv

【 D3.js 进阶系列 — 1.2 】 读取 CSV 文件时乱码的解决方法

在 D3 中使用 d3.csv 读取 CSV 文件时,有时会出现乱码问题.怎么解决呢? 1. 乱码问题 使用 d3.csv 读取 xxx.csv 文件时,如果 xxx.csv 文件使用的是 UTF-8 编码,不会有什么问题.当然,个人认为尽量使用 UTF-8 编码,可以在同一编码内使用各国文字. 但是,如果 xxx.csv 文件使用的是 utf-8 编码,使用 Microsoft Excel 打开的时候,可能会出现乱码,因为国内的 Excel 默认使用 GB2312 打开,而且在打开的时候不能选

Python 中读取csv文件中有中文的情况

Python 中读取csv文件中有中文的情况,提示编码问题: 读取的时候: import sys reload(sys) #中文错误 sys.setdefaultencoding( "utf-8" ) save 存储的时候: dataframe可以使用to_csv方法方便地导出到csv文件中,如果数据中含有中文,一般encoding指定为"utf-8″,否则导出时程序会因为不能识别相应的字符串而抛出异常,index指定为False表示不用导出dataframe的index数据

Lua读取CSV文件到table中

创建Lua函数载入CSV文件并保存到表中的函数: function GetLines(fileName) indx = 0 myLines ={} for line in io.line(string.format("%s%s", "c:/lua_scripts/",filename)) do indx = indx + 1 myLines[indx] = line end return indx, myLines --returns number of lines

C++ 把数组数据存入 CSV 文件,以及读取 CSV 文件的数据

1. CSV-百度百科 2. 代码 #pragma once //Microsoft Visual Studio 2015 Enterprise #include<iostream> #include<fstream> #include<string> #include<vector> #include<cstdio> #include<cstdlib> using namespace std; template<typenam

sparkR读取csv文件

sparkR读取csv文件 The general method for creating SparkDataFrames from data sources is read.df. This method takes in the path for the file to load and the type of data source, and the currently active SparkSession will be used automatically. SparkR suppo