java - 如何使用java仅基于第一列元素复制.CSV的一部分
问题描述
像这样复制部分(从日期到日期)我试图根据第一列(开始日期和时间)数据仅复制 .CSV 文件的一部分(2019-01-28 10:22:00 AM)但是用户必须这样说(2019/01/28 10:22:00)
这适用于 windows,java opencsv,这是我找到的,但不做我需要的 exaclty:
像这样:
int startLine = 从列 csv 中获取 value1 ;int endLine = 从列 csv 中获取 value2;
public static void showLines(String fileName, int startLine, int endLine) throws IOException {
String line = null;
int currentLineNo = 1;
// int startLine = 20056;//40930;
// int currentLineNo = 0;
File currentDirectory = new File(new File(".").getAbsolutePath());
String fromPath = currentDirectory.getCanonicalPath() + "\\Target\\part.csv";
PrintWriter pw = null;
pw = new PrintWriter(new FileOutputStream(fromPath), true);
//pw.close();
BufferedReader in = null;
try {
in = new BufferedReader (new FileReader(fileName));
//read to startLine
while(currentLineNo<startLine) {
if (in.readLine()==null) {
// oops, early end of file
throw new IOException("File too small");
}
currentLineNo++;
}
//read until endLine
while(currentLineNo<=endLine) {
line = in.readLine();
if (line==null) {
// here, we'll forgive a short file
// note finally still cleans up
return;
}
System.out.println(line);
currentLineNo++;
pw.println(line);
}
} catch (IOException ex) {
System.out.println("Problem reading file.\n" + ex.getMessage());
}finally {
try { if (in!=null) in.close();
pw.close();
} catch(IOException ignore) {}
}
}
public static void main(String[] args) throws FileNotFoundException {
int startLine = 17 ;
int endLine = 2222;
File currentDirectory = new File(new File(".").getAbsolutePath());
try {
showLines(currentDirectory.getCanonicalPath() + "\\Sources\\concat.csv", startLine, endLine);
} catch (IOException e) {
e.printStackTrace();
}
// pw.println();
}
解决方案
public static void main(String[] args) throws IOException {
File currentDirectory = new File(new File(".").getAbsolutePath());
String targetPath = currentDirectory.getCanonicalPath() + "\\Target\\part.csv";
String inputFilePath = currentDirectory.getCanonicalPath() + "\\Sources\\concat.csv";
String line = null;
ArrayList<String> lines = new ArrayList<String>();
boolean add = false;
String startLine = "2019/01/28 10:22:00";
String endLine = "2019/04/06 10:30:00";
try(PrintWriter pw = new PrintWriter(new FileOutputStream(targetPath), true)){
try (BufferedReader input = new BufferedReader(new FileReader(inputFilePath))){
while((line = input.readLine()) != null) {
String date = line.split(",")[0];
if(date.contains(startLine)) {
add = true;
}else if(date.contains(endLine)) {
break;
}
if(add) {
lines.add(line);
}
}
}
for(String currentLine : lines) {
pw.append(currentLine + "\n");
}
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
}
推荐阅读
- jenkins - Jenkins Cobertura 插件 - 重置 Ratcheting / autoUpdateHealth 值
- python - 基于一组子字符串递归查找文件的有效方法,存储它们的路径
- php - 如何使用 php 从多维 json 响应中获取键和值
- javascript - 为什么Quasar抽屉覆盖Vue应用程序中的Quasar工具栏?
- html - Flexbox 等高 4x4 网格
- testing - 开玩笑地使用组合 api Vue3 quasar 获取变量
- regex - 需要正则表达式来返回倒数第二个目录表单路径
- c++ - 创建你自己的仿函数,C++
- linux - bash 使用 =~ 运算符测试内置单词正则表达式
- list - 列出函数内的变量分配导致对全局变量的不必要更改