[英]How To Transfer specific Columns based on column name not by column index From One CSV File Into Another CSV Using Java
I am writing one Java utility where I want to transfer specific column based on Column Name from one CSV to another CSV file instead of ArrayIndex . 我正在编写一个Java实用程序,我想将基于列名的特定列从一个CSV传输到另一个CSV文件而不是ArrayIndex 。
Below is the program which is working absolutely fine when I am selecting specific columns based on array Index but I want to filter my columns based on Column Name only. 下面的程序在我根据数组索引选择特定的列,但我只想根据列名过滤列时,运行得很好。
package be.ing.data;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.concurrent.TimeUnit;
public class ImportExportCSV{
public static void main(String[] args){
// Set your splitter, mostly "," or ";"
String csvFile = "C:/Data/input_file.csv";
String exportCSVFile = "C:/Data/output_file.csv";
// String csvSplitter = "\t";
String csvSplitter = ",";
String line = "";
BufferedReader br = null;
PrintWriter output = null;
Date date = new Date();
try {
// A buffered reader on the input file
br = new BufferedReader(new FileReader(csvFile));
// A writer to the output file
output = new PrintWriter(exportCSVFile, "UTF-8");
// Read line after line until EOF
int count = 0;
long startTime = System.nanoTime();
System.out.println("Start Execution Time : "+startTime);
while ((line = br.readLine()) != null) {
String[] cols = line.split(csvSplitter);
output.println(cols[0] + csvSplitter +
cols[20]);
count++;
}
System.out.println("Total Record count :"+count);
long endTime = System.nanoTime();
System.out.println("Completion Time : "+ (endTime-startTime));
System.out.println("Time in seconds"+ TimeUnit.SECONDS.convert(endTime, TimeUnit.NANOSECONDS));
}catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
You have to convert String[] cols
to a List<String>
in order to get the index of a column based on his name and then use that index to access the value for every lines of your csv file : 您必须将
String[] cols
转换为List<String>
以获得基于列名的列索引,然后使用该索引访问csv文件每一行的值:
For example if you want to print for every lines of your csv file the value of the column "myColumnName"
: 例如,如果要为csv文件的每一行打印
"myColumnName"
列的值:
String[] cols = line.split(csvSplitter);
List<String> columns = Arrays.asList(cols);
System.out.println(cols[columns.indexOf("myColumnName")]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.