Reputation: 11
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
Upvotes: 1
Views: 1465
Reputation: 111219
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, e.g.
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"));
Upvotes: 1