!!!!!
Files.lines(Paths.get("D:\\workspace\\example\\load\\src\\main\\resources\\baseFieldDataGood.json"), StandardCharsets.UTF_8)
.map(line -> JSON.parseObject(line, new TypeReference<FileDataLine2>() {
}))
.forEach(lineData -> {
System.out.println(lineData);
convterBean(lineData);
});
!!!!!!
package load.service.impl;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import load.domain.JsonBean;
import load.mapper.HzHhmdFgwMapper;
import load.service.LoadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class LoadServiceImpl implements LoadService {
@Autowired
private HzHhmdFgwMapper hzHhmdFgwMapper;
@Override
public void updateToBase() throws IOException {
//splitFile();
for (int i = 1; i < 501; i++) {
String path = ("D:\\linshi\\part" + i + ".txt");
List<JsonBean> jsonBeans = readJsonFile(path);
System.out.println(jsonBeans);
// //hzHhmdFgwMapper.insertList(hzHhmdFgws);
}
}
/**
* @param fileName 读取文件后转成json
* @return
*/
public List<JsonBean> readJsonFile(String fileName) {
List<JsonBean> jsonBeans = new ArrayList<>();
try {
File jsonFile = new File(fileName);
FileReader fileReader = new FileReader(jsonFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(jsonFile), "utf-8"));
String line;
while ((line = reader.readLine()) != null && reader.readLine() != "") {
JsonBean jsonBean = JSON.parseObject(line, new TypeReference<JsonBean>() {
});
jsonBeans.add(jsonBean);
}
fileReader.close();
reader.close();
return jsonBeans;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* @throws IOException 获取文件并且拆分,保存到本地
*/
public void splitFile() throws IOException {
//获取json位置
String path = LoadServiceImpl.class.getClassLoader().getResource("baseFieldDataGood.json").getPath();
String realPath = path.substring(1);
long timer = System.currentTimeMillis();
int bufferSize = 20 * 1024 * 1024;//设读取文件的缓存为20MB
//建立缓冲文本输入流
//File file = new File("D:\\workspace\\example\\load\\src\\main\\resources\\baseFieldDataGood.json");
File file = new File(realPath);
FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
InputStreamReader inputStreamReader = new InputStreamReader(bufferedInputStream);
BufferedReader input = new BufferedReader(inputStreamReader, bufferSize);
int splitNum = 501 - 1;//要分割的块数减一
int fileLines = 236692;//输入文件的行数
long perSplitLines = fileLines / splitNum;//每个块的行数
for (int i = 0; i <= splitNum; ++i) {
//分割
//每个块建立一个输出
FileWriter output = new FileWriter("D:\\linshi\\part" + i + ".txt");
String line = null;
//逐行读取,逐行输出
for (long lineCounter = 0; lineCounter < perSplitLines && (line = input.readLine()) != null; ++lineCounter) {
output.append(line + "\r");
}
output.flush();
output.close();
output = null;
}
input.close();
timer = System.currentTimeMillis() - timer;
System.out.println("处理时间:" + timer);
}
}
读取2G的大json文件后存入数据库
最新推荐文章于 2024-01-23 16:14:19 发布