Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read CSV with Scanner()

My csv is getting read into the System.out, but I've noticed that any text with a space gets moved into the next line (as a return \n)

Here's how my csv starts:

first,last,email,address 1, address 2 john,smith,[email protected],123 St. Street, Jane,Smith,[email protected],4455 Roger Cir,apt 2 

After running my app, any cell with a space (address 1), gets thrown onto the next line.

import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner;  public class main {      public static void main(String[] args) {         // -define .csv file in app         String fileNameDefined = "uploadedcsv/employees.csv";         // -File class needed to turn stringName to actual file         File file = new File(fileNameDefined);          try{             // -read from filePooped with Scanner class             Scanner inputStream = new Scanner(file);             // hashNext() loops line-by-line             while(inputStream.hasNext()){                 //read single line, put in string                 String data = inputStream.next();                 System.out.println(data + "***");              }             // after loop, close scanner             inputStream.close();           }catch (FileNotFoundException e){              e.printStackTrace();         }      } } 

So here's the result in the console:

 first,last,email,address  1,address  2 john,smith,[email protected],123  St.  Street, Jane,Smith,[email protected],4455  Roger  Cir,apt  2 

Am I using Scanner incorrectly?

like image 457
coffeemonitor Avatar asked Jan 11 '13 08:01

coffeemonitor


People also ask

How do I read a CSV file in Java by line?

We can read a CSV file line by line using the readLine() method of BufferedReader class. Split each line on comma character to get the words of the line into an array. Now we can easily print the contents of the array by iterating over it or by using an appropriate index.

How read and write CSV file in Java?

You can Download OpenCSV Jar and include in your project class path. CSVReader – This class provides the operations to read the CSV file as a list of String array. CSVWriter – This class allows us to write the data to a CSV file.


2 Answers

Please stop writing faulty CSV parsers!

I've seen hundreds of CSV parsers and so called tutorials for them online.

Nearly every one of them gets it wrong!

This wouldn't be such a bad thing as it doesn't affect me but people who try to write CSV readers and get it wrong tend to write CSV writers, too. And get them wrong as well. And these ones I have to write parsers for.

Please keep in mind that CSV (in order of increasing not so obviousness):

  1. can have quoting characters around values
  2. can have other quoting characters than "
  3. can even have other quoting characters than " and '
  4. can have no quoting characters at all
  5. can even have quoting characters on some values and none on others
  6. can have other separators than , and ;
  7. can have whitespace between seperators and (quoted) values
  8. can have other charsets than ascii
  9. should have the same number of values in each row, but doesn't always
  10. can contain empty fields, either quoted: "foo","","bar" or not: "foo",,"bar"
  11. can contain newlines in values
  12. can not contain newlines in values if they are not delimited
  13. can not contain newlines between values
  14. can have the delimiting character within the value if properly escaped
  15. does not use backslash to escape delimiters but...
  16. uses the quoting character itself to escape it, e.g. Frodo's Ring will be 'Frodo''s Ring'
  17. can have the quoting character at beginning or end of value, or even as only character ("foo""", """bar", """")
  18. can even have the quoted character within the not quoted value; this one is not escaped

If you think this is obvious not a problem, then think again. I've seen every single one of these items implemented wrongly. Even in major software packages. (e.g. Office-Suites, CRM Systems)

There are good and correctly working out-of-the-box CSV readers and writers out there:

  • opencsv
  • Ostermiller Java Utilities
  • Apache Commons CSV

If you insist on writing your own at least read the (very short) RFC for CSV.

like image 163
Scheintod Avatar answered Sep 17 '22 21:09

Scheintod


scanner.useDelimiter(","); 

This should work.

import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner;   public class TestScanner {      public static void main(String[] args) throws FileNotFoundException {         Scanner scanner = new Scanner(new File("/Users/pankaj/abc.csv"));         scanner.useDelimiter(",");         while(scanner.hasNext()){             System.out.print(scanner.next()+"|");         }         scanner.close();     }  } 

For CSV File:

a,b,c d,e 1,2,3 4,5 X,Y,Z A,B 

Output is:

a|b|c d|e 1|2|3 4|5 X|Y|Z A|B| 
like image 38
Pankaj Avatar answered Sep 16 '22 21:09

Pankaj