Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java printing squares (unknown characters) alongwith regular text

Tags:

java

file

csv

Code:

public class MainApplication {

      public static void main(String[] args) throws IOException {

              try{
                  // Open the file that is the first 
                  // command line parameter
                  FileInputStream fstream = new FileInputStream("data/temp.CSV");
                  BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
                  String strLine;
                  //Read File Line By Line
                  while ((strLine = br.readLine()) != null)   {
                  // Print the content on the console
                  System.out.println (strLine);
                  }
                  //Close the input stream
                  in.close();
                    }catch (Exception e){//Catch exception if any
                  System.err.println("Error: " + e.getMessage());
                  }
      }
}

CSV file data:

19/1/13 13:58:04    0   1610    0   419 0   0
19/1/13 13:58:05    0.01    1599    66  432 0   1
19/1/13 13:58:06    0.02    1603    47  423 0   2
19/1/13 13:58:07    0.03    1602    26  413 0   3
19/1/13 13:58:08    0.04    1605    130 412 0   4

Output:

Error screen

like image 929
neetinnagap Avatar asked Jan 29 '13 17:01

neetinnagap


1 Answers

Use

BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-16LE"));

Instead of

BufferedReader br = new BufferedReader(new InputStreamReader(in));
like image 90
Swapnil Dinkar Avatar answered Sep 30 '22 03:09

Swapnil Dinkar