[英]Read from a CSV file and write each line into a Different File
我的输入 CSV 具有如下数据 -
VBS123ER,Abc_123,TEST_REPORT_FOR_SA,15-JUN-2020,76854,SAMPLE_REPORT
VBS123ER,Abc_123,TEST_REPORT_FOR_SA,15-JUN-2020,76855,SAMPLE_REPORT
我想从我的 CSV 中读取每一行,并为每一行创建一个新的 CSV/TXT 文件。 Like-对于第 1 行,将创建一个名为 1.txt/1.csv 的文件,该文件将保存所有第 1 行数据。 第 2 行也是如此,依此类推。在 java 中我们如何实现这一点?
File f = new File("C:\\Test\\AR_POC\\Input.txt");
BufferedReader b = new BufferedReader(new FileReader(f));
String readLine = "";
System.out.println("Reading file using Buffered Reader");
while ((readLine = b.readLine()) != null) {
System.out.println(readLine);
for(int i = 0; i < readLine.length; i++) {
FileOutputStream fo = new FileOutputStream("C:\\Test\\AR_POC\\" + Name + i + ".txt");
fo.write(readLine.getBytes());
fo.close();
}
}
您应该计算输入文件中的行数,而不是当前行的长度:
File f = new File("C:\\Test\\AR_POC\\Input.txt");
BufferedReader b = new BufferedReader(new FileReader(f));
String readLine = "";
System.out.println("Reading file using Buffered Reader");
int i = 0;
while ((readLine = b.readLine()) != null) {
System.out.println(readLine);
FileOutputStream fo = new FileOutputStream("C:\\Test\\AR_POC\\" + Name + i++ + ".txt");
fo.write(readLine.getBytes());
fo.close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.