Results 1 to 5 of 5
  1. #1
    ranoosh is offline Member
    Join Date
    Feb 2008
    Posts
    1
    Rep Power
    0

    Wink writing and reading unicode characters from a file

    Hello everybody!

    I have been trying for ages now to read and write unicode characters form a file and into a file but instead of characters i get funny shapes and question marks.

    any idea how java handles unicode characters? and why i can print out unicode characters to screen just fine but I cannot save the into a file properly?

    I am using linux, i have tried using windows but it did not work at all.


    :cool:

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Java strings are natively UTF-16

    Use filetypes with the name Reader in them.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    In simple word, try something like this. That's what Jordan try to pointed you.

    Java Code:
    BufferedReader bufReader =
        new BufferedReader(
            new InputStreamReader(new FileInputStream(file_name), "UTF-16"));

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    i can print out unicode characters to screen just fine but I cannot save the into a file properly?
    What class are you using to write the unicode characters?
    Do you really want unicode or do you want ASCII?
    Unicode characters use 2 bytes. With ASCII text every other byte will be a binary 0 which will display as a ? or square with most text editors. Do you have a hex editor that you can use to look at the files you are reading and writing?

  5. #5
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by Norm View Post
    Unicode characters use 2 bytes.
    This is mostly true. For most languages, the characters are encoded in 16 bit values, which are typically called "two bytes".

    But the point of unicode is to be able to write software that supports any language spoken or written on this planet. There are some lanugages that use three byte representations. These can still be stored as UTF-16, altho I think some of them end up being more like UTF-32.

    More importantly, all String objects in Java are Unicode. Its easy inside Java.
    Storing the strings to/from disks, printers, etc. is the challenge.

Similar Threads

  1. Reading/Writing files through Applet
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 03-09-2009, 11:45 AM
  2. Reading data from Micrsoft excel and writing to notepad
    By abhishek.jain in forum New To Java
    Replies: 4
    Last Post: 01-29-2009, 08:12 AM
  3. Reading/Writing a File using byte array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-16-2008, 10:41 AM
  4. Write unicode into file
    By vata2999 in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 03:04 PM
  5. Help with File reading and writing
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 06:47 PM

Posting Permissions

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