If Two different machines have different character encodings.How to take from a java program that same file on both machines should be read in similar manner.Is it possible using java or we have to manually set the encodings of both machines?
3 Answers
It sounds like you just want to use something like:
InputStream inputStream = new FileInputStream(...);
Reader reader = new InputStreamReader(reader, "UTF-8"); // Or whatever encoding
Basically you don't have to use the platform default encoding, and you should almost never do so. It's a pain that FileReader
always uses the platform default encoding :( I prefer to explicitly specify the encoding, even if I'm explicitly specifying that I want to use the platform default :)

- 1,421,763
- 867
- 9,128
- 9,194
-
My systems default encoding is ISO-8859-1, I am using BufferedReader bis = new BufferedReader(new InputStreamReader(new FileInputStream("filepath"), "UTF-8")); String str = bis.readLine(); PrintStream ps1 = new PrintStream(System.out, true, "UTF-8"); ps1.println(str); But it is not printting the unicode characters in the file mentioned in the filepath – Ritesh Kaushik Aug 22 '12 at 19:02
You don't need to change the machine's settings.
You can use any java.io.Reader subclass that allows you to set the character encoding. For instance InputStreamReader, like so:
new InputStreamReader(new FileInputStream("file.txt"), "UTF8");

- 1,414
- 12
- 21
You are in control of reading/writing the files on both environment.
Working with text files in Java
You have control control on only read side.
- You know the encoding used to write the file: Identify what encoding is used to write the file and use the same encoding to read it.
- You doesn't know the encoding used to write the file: Best you can do is guess the encoding: Character Encoding Detection Algorithm
UPDATE
If your issues is that you are not seeing the output properly in eclipse console then the issue might be with the encoding setting of the eclipse itself. Read this article on how to fix eclipse.

- 1
- 1

- 78,777
- 46
- 231
- 327
-
If the file encoding is UTF-8, system default encoding is ISO-8859-1, which is my senario, when i read from the file by specifying encoding as UTF-8 and print data using PrintStream ps1 = new PrintStream(System.out, true, "UTF-8"); ps1.println("ss "+str);// i am not able to print unicode data from the file? – Ritesh Kaushik Aug 22 '12 at 18:43