I have a requirement like:
My input will be a csv file where I will be having values like below:
action, userId, firstName, email, lastName 1,2,sample,[email protected],test 2,3,justtest,[email protected],testI have to read this csv file based on headers. Say for ex: if action =1 and email is null, then I have to change from action to 2 or email to null or something like this.
I have no idea on how to read and parse the values based on the headers. This is my code what I tried:
String csvFile = "C:\\Test.csv"; // create BufferedReader to read csv file BufferedReader br; String line = ""; br = new BufferedReader(new FileReader(csvFile)); br.readLine(); // Read the file line by line starting from the second line while ((line = br.readLine()) != null) { // Get all tokens available in line String[] tokens = line.split(","); if (tokens.length > 0) { for (int i = 0; i < tokens.length; i++) { System.out.println("All Rows ------->" + tokens[i]); } } }
This is just printing all the values in new line like below:
All Rows ------->1411184866
All Rows ------->category
All Rows ------->123456
All Rows ------->Test
All Rows ------->TestFullName
All Rows ------->[email protected]
All Rows ------->3423131
Please help me completing this code. Thanks in advance.
.hasNextLine()instead of the if you currently have, that'll sinch it for the project. Very readable.