This question already has an answer here:
- Calculating Average from a CSV File 4 answers
I have pie | separated csv file. I want to read this file in java. I have a java code which read comma separated file in java but it fails at | separated csv file
My file contains "CUSTOMER CODE | PRODUCT CODE | SEND TO BANK | SEND TO BRANCH" This type of data not comma separated.
Java code
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
public class TestPieCSVtoXLs {
public static void main(String[] args) {
String csvFile = "D:\\VijayTest\\csvFile.csv";
DataInputStream myInput;
String thisLine;
ArrayList<ArrayList<String>> arList = new ArrayList<ArrayList<String>>();
ArrayList<String> al = new ArrayList<String>();
BufferedReader br = null;
try {
FileInputStream fis = new FileInputStream(csvFile);
myInput = new DataInputStream(fis);
while ((thisLine = myInput.readLine()) != null) {
al = new ArrayList<String>();
String strar[] = thisLine.split(",");
for (int j = 0; j < strar.length; j++) {
System.out.println(strar[j]);
al.add(strar[j]);
}
}
arList.add(al);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("Done");
}
}