-1
  public static void main(String[] args) throws IOException {
        String dataRow;
     BufferedReader CSVFile = 
                new BufferedReader(new FileReader("test.csv"));

          while ((dataRow = CSVFile.readLine()) != null) {
                System.out.println(dataRow.split(";")[0]);
            }
          // Close the file once all data has been read.
          CSVFile.close();

          // End the printout with a blank line.
          System.out.println("Done");

}

The CSV file I am Trying to read in normal text view

ID;Numbers
12;234
343;233

All I am able to print is space no strings in it.

Output

      1. ��I
      2. 
      3. 
      4. 
      Done

The File Encoding is Only "UNICODE"

How to read a Unicode file in java. Do I have to set a parameter setting the encoding type of the file in FileReader java class construtor??

Kindly advise.

ycb
  • 11
  • 5
  • Maybe the CSV file ins not encoded in your local machine default encoding? – Ricardo Vila Aug 10 '15 at 09:34
  • @Ricardo I created a new file it read properly. I don't understand what is the problem with the previous file I am trying to read. – ycb Aug 10 '15 at 09:45
  • I have 100 csv files having 10000s of lines each. But all files have the same problem that they are not readable even without splitting(displaying the line directly). There needs to be some way to read the CSV files and process the data inside it. – ycb Aug 10 '15 at 09:50
  • Probably line delimiters are different of what system expects for \n. for example windows and linux systems differs. I suggest to parse manualy the file, spliting each line manually using the right line separator: http://stackoverflow.com/questions/426397/do-line-endings-differ-between-windows-and-linux If you want to handle your files with different encoding you can use SublimeText's 'open with encoding...' option. – Ricardo Vila Aug 10 '15 at 10:46

2 Answers2

1
public static void main(String[] args) throws IOException {
    String dataRow;
    BufferedReader CSVFile = new BufferedReader(new FileReader("F:\\csv.csv"));
    while ((dataRow = CSVFile.readLine()) != null) {
        String []data = dataRow.split(";");
        for (String d : data) {
            System.out.print(d + " ");
        }
        System.out.println();
    }
    CSVFile.close();
    System.out.println("Done");

}

The result is shown below:

ID Numbers 
12 234 
343 233 
Done

The way to get the file encode is shown below

enter image description here

open the csv file with nodepad editor, then click file -> save as

Matt
  • 74,352
  • 26
  • 153
  • 180
Javy
  • 944
  • 5
  • 9
  • I ran your code it gives the same output as I have posted in the question – ycb Aug 10 '15 at 09:27
  • the encode of your csv file may be not right, In my machine, the csv file is encoded with utf-8 – Javy Aug 10 '15 at 09:48
  • see the answer, i make a screenshot for you. – Javy Aug 10 '15 at 10:36
  • @Javy I checked the encoding it is UTF-8 with gedit text editor in my machine. So,the encoding would be the cause of the problem?? – ycb Aug 10 '15 at 10:55
0

if you file encoding is unicode, when you read file,you can pass a file encodeing argument to function

the fllowing code is below

    public static void main(String[] args) throws IOException {
    String dataRow;
    BufferedReader CSVFile = new BufferedReader(new InputStreamReader(new FileInputStream("F:\\csv.csv"),"unicode"));
    while ((dataRow = CSVFile.readLine()) != null) {
        String []data = dataRow.split(";");
        for (String d : data) {
            System.out.print(d + " ");
        }
        System.out.println();
    }
    CSVFile.close();
    System.out.println("Done");
}

if you do not pass the file encoding argument, when you read the file content, the encoding depends on your os

Javy
  • 944
  • 5
  • 9