Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Force same character encoding in UNIX as in Windows

898130Nov 4 2011 — edited Nov 7 2011
My program reads a simple text file and outputs the same on the console. The file contains special characters like àéèéàöäül etc. The goal of this exercise is to make java recognize special characters both on the UNIX and on the Windows system in the same manner.

I wrote the program on a Windows machine and then transferred the class file to the UNIX system and ran it there. It is giving two different outputs for the different systems.

Here is the code

public class LocaleFile {

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

System.out.println("Deafult Locale :: "+Locale.getDefault());
System.out.println("Deafult Characterset :: "+Charset.defaultCharset());
System.out.println("Resetting defailt local to de_DE ... ");
Locale newLocale=new Locale(Locale.GERMAN.toString(), Locale.GERMANY.toString());
Locale.setDefault(newLocale);
System.out.println("After resetting Locale :: "+Locale.getDefault());

BufferedReader bufferedReader=new BufferedReader( new InputStreamReader( new FileInputStream("helloworld.txt"), "ISO-8859-15"));

String line=bufferedReader.readLine();
do{
System.out.println(line);

line=bufferedReader.readLine();
}
while(line!=null);

bufferedReader.close();

}

}

Windows Output
Deafult Locale :: de_CH
Deafult Characterset :: ISO-8859-1
Resetting defailt local to de_DE ...
After resetting Locale :: de_DE_DE
Character set 1: Arijit
Character set 2: è!éà£
Character set 3: ü?öä$
Character set 4: []{}
End of Test

UNIX Output
bash-3.00$ java LocaleFile
Deafult Locale :: en
Deafult Characterset :: US-ASCII
Resetting defailt local to de_DE ...
After resetting Locale :: de_DE_DE
Character set 1: Arijit
Character set 2: ?!???
Character set 3: ????$
Character set 4: []{}
End of Test
bash-3.00$

*Structure of helloworld.txt*
Character set 1: Arijit
Character set 2: è!éà£
Character set 3: ü?öä$
Character set 4: []{}
End of Test

Hope to hear from you soon guys.

Thanks
Arijit
This post has been answered by baftos on Nov 4 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 5 2011
Added on Nov 4 2011
7 comments
755 views