简体   繁体   中英

Parse csv file with special character like µ using java

I have a situation where I have to read a CSV file which contains special character like 'µ'. This has to be done using java. I am doing: BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(,"UTF-8"));

in windows it runs OK. But in redhat linux environment, it converts those special characters to '?'. Any help is highly appreciated

Output written to System.out will be encoded using the "platform default encoding" which on linux is determined from locale environment variables (see output of locale command), and those in turn are set in user or system level configuration files.

On server installations, the default encoding is often ASCII . "µ" is not an ASCII character so it will be converted to "?" when it is printed.

There are a couple of ways to change the default encoding:

  • Set the Java file.encoding system property when you run your program, eg

    java -Dfile.encoding=utf-8 yourprogram
  • Set LC_CTYPE env variable before you run your program, for example:

     export LC_CTYPE=en_US.UTF-8 java yourprogram
  • Those methods also change the default encoding for input and file names etc. You can change the encoding specifically for System.out with Java code:

     PrintStream originalOut = System.out; // in case you need it later System.setOut(new PrintStream(System.out, true, "utf-8"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM