Results 1 to 3 of 3
  1. #1
    whatif is offline Member
    Join Date
    Feb 2011
    Posts
    12
    Rep Power
    0

    Default Displaying Unicode Characters

    I wrote a java program that writes strings to a file. The strings contain foreign language characters. When I run the program in Windows, the output file shows the foreign characters. However, when I attempt the same operation in Linux, the output file shows a white question mark in a black background instead of the foreign characters.

    The same Linux system could display the foreign characters if I copy the output file from Windows to Linux. I tried to create the output file using gedit that my program would then add additional strings to and chose Unicode-32 for encoding but still the same problem.

    I read online that Java uses Unicode, while other sources states UTF-16.

    Some of the encoding available on gedit (text editor) are Unicode UCS-2, Unicode UTF-32, Unicode UTF-16LE, Unicode UTF-16BE, Unicode UTF-16, Unicode UTF-7, Unicode UCS-4.

    What could I do to get the program to display the foreign language characters from output text file?

    Your help will be much appreciated,
    Mike

  2. #2
    fxRichard is offline Member
    Join Date
    Dec 2008
    Posts
    64
    Rep Power
    0

    Default

    What encoding are you using to write to the file? Can you paste your code here that shows how you are writing to the file. Also you need to make sure that the encoding you use to write is the same you use to read, Windows uses CP1252 by default whereas Unix uses UTF-8 usually. Simply write your string to the file in UTF-8 format and it should work for both.

    For instance you could do:

    Java Code:
      myFileOutputSteram.write(myString.getBytes("UTF-8");

  3. #3
    whatif is offline Member
    Join Date
    Feb 2011
    Posts
    12
    Rep Power
    0

    Default

    Thank you for taking the time to reply!

    I don't specify the encoding. I'm using the FileWriter class which doesn't have a constructor with encoding parameter. The only reason I'm using it is because it has a write method that takes a string.

    I will try FileWriter.write(stringToWrite.getBytes("UTF-8"));
    I will do the same for FileReader....hopefully it will solve the problem.

    Thanks again,
    Mike

Similar Threads

  1. Replies: 23
    Last Post: 08-12-2010, 10:59 AM
  2. Problem with writing unicode characters in a file
    By ze snow in forum New To Java
    Replies: 1
    Last Post: 02-23-2010, 11:47 PM
  3. Replies: 2
    Last Post: 02-15-2010, 07:49 AM
  4. Form Based Authentication fails with unicode characters
    By davesquire in forum Java Servlet
    Replies: 1
    Last Post: 09-25-2009, 07:00 PM
  5. writing and reading unicode characters from a file
    By ranoosh in forum Advanced Java
    Replies: 4
    Last Post: 09-28-2008, 05:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •