Reputation: 11
Wondering how to load a csv file into java and print out the results. So far I can't get it to print out the whole csv sheet. The sheet contains white spaces and looks like this with a header:
Sales num, Account num, Date, Delivery, Codes
Sales 1, 2345 , May17, Oct318, 3345
4435
2234
Sales 2, 3345, May 18, Oct318, 4456
There are multiple codes per a sale, but all the other info (account num, data, delivery) only has one. After the codes the second sales is put in, so there is alot of white space between sales 1 and sale 2.
I have this code that I found, but it only prints the first two words.
public static void main(String[] args) throws IOException{
try {
ArrayList < String > ar = new ArrayList < String > ();
File csvFile = new File("C:\\Users\\AZ0000\\Documents\\Book1.csv\\");
BufferedReader br = new BufferedReader(new FileReader(csvFile));
String line = "";
StringTokenizer st = null;
int lineNumber = 0;
int tokenNumber = 0;
while ((line = br.readLine()) != null) {
String[] arr = line.split(",");
for (int i = 0; i < arr.length; i++) {
//System.out.println(arr[0]+" " + arr[1] + arr); // h
}
lineNumber++;
//use comma as token separator
}
} catch (IOException ex) {
ex.printStackTrace();
}
I will eventually need to take out the data from the csv and import it to certain lines in a text file.
Upvotes: 0
Views: 1017
Reputation: 1277
If the format looks like you pase, then its simply not a csv ... proper format should be like
Sales num, Account num, Date, Delivery, Codes
Sales 1, 2345 , May17, Oct318, 3345
Sales 1, 2345 , May17, Oct318, 4435
Sales 1, 2345 , May17, Oct318, 2234
Sales 2, 3345, May 18, Oct318, 4456
Or use another separator to split multiple values in one column, eg.
Sales num, Account num, Date, Delivery, Codes
Sales 1, 2345 , May17, Oct318, 3345;4435;2234
Sales 2, 3345, May 18, Oct318, 4456
From my point of view, the best approach is to have file like
Sales num,Account num,Date,Delivery,Codes
Sales 1,2345,May 17,Oct318,3345;4435;2234
Sales 2,3345,May 18,Oct318,4456
Then simply read row, split by comma ,
, last column split by ;
Upvotes: 0
Reputation: 6289
You should just use proper parser as it's more reliable.
univocity-parsers removes any unwanted whitespaces by default, is WAY faster than using line.split(",");
, and will handle things such as quotes, line endings and delimiters in your fields.
Try this code:
CsvParserSettings settings = new CsvParserSettings();
settings.detectFormatAutomatically();
CsvParser parser = new CsvParser(settings);
List<String[]> allRows = parser.parseAll(new File("C:\\Users\\AZ0000\\Documents\\Book1.csv"));
Hope it helps.
Disclaimer: I'm the author of this library. It's open-source and free (Apache 2.0 license)
Upvotes: 1