[英]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.