Results 1 to 5 of 5
  1. #1
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Unhappy [SOLVED] File Write in File

    Guys i need your help to share some light in my work.

    A little Background:

    My System works fine until i met this special character (UTF-8 extended character in one of the records). I talked to the bosses here, they said it is a legal character so i must do something. The system is OK in displaying and in overall functionally except when producing a feedfile (outfile file to other departments). And the system works efficiently.

    What caused the problem:

    This --> " Cte d'Ivo " the character "" when im writing it in a flat file it transform to this --> "Côte d'Ivo"
    I've been working on this problem since friday and thinking about it over this weekend.

    here is the my solution but still doesnt work for the feedfile output.

    ForArgon <-- My String to write
    ForArgonOut <-- converted String
    fileName <-- FileLocation with Filename convention

    Java Code:
    Charset charset = Charset.forName("ISO-8859-1");
            CharsetDecoder decoder = charset.newDecoder();
            CharsetEncoder encoder = charset.newEncoder();
            
            try {
                // Convert a string to ISO-LATIN-1 bytes in a ByteBuffer
                // The new ByteBuffer is ready to be read.
                ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(ForArgon));
    
                // Convert ISO-LATIN-1 bytes in a ByteBuffer to a character ByteBuffer and then to a string.
                // The new ByteBuffer is ready to be read.
                CharBuffer cbuf = decoder.decode(bbuf);
                System.out.println(cbuf.toString());
                String ForArgonOut = cbuf.toString();
                
                try {
                    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName),"8859_1"));
        //            FileWriter fstream = new FileWriter(fileName, true);
        //            BufferedWriter out = new BufferedWriter(fstream);
                    out.write(ForArgonOut);
                    out.close();
        //            fstream.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                
            } catch (CharacterCodingException e) {
            }
    Thanks you.
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default

    The has value \u00f4. Which is past the ASCII value of 0x7f
    When you convert the Unicode char to UTF8 it takes 2 bytes to hold its value. When converting from bytes to Unicode you need to use UTF8 for charset to convert the 2 bytes back to a single char.

    To find the right conversions, write a short simple program with only the character. Print out the values of the bytes in hex to see what is happening. Integer.toHexString(theChar or theByte);

  3. #3
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    Thank you Norm. I had no problem in processing it and displpaying it in my JSP page. (They have UTP-8 encoding).

    Actually the Records came from MYSQL. Im getting a little light. =)
    I store the records in a String Array. It works fine except for that situation.
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  4. #4
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    norm i found the answer. java is innocent. T_T it is MYSQL. here is a link.

    www (dot) bugs (dot) mysql (dot) com/bug.php?id=12030

    Is there any work around that you can share with me ^_^ My Boss specified to use this database because its free. But i think i need to talk to him and let me use oracle instead
    Last edited by Eku; 10-27-2008 at 03:24 AM.
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  5. #5
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    Thanks for the Help Norm.
    I got it. It wasnt java or Mysql.
    it was the encoding on URL between JSP and Servlet =)

    I just set this in my servlet before requesting any values

    request.setCharacterEncoding("UTF-8");
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

Similar Threads

  1. how to write onto a file
    By mirage_87 in forum New To Java
    Replies: 6
    Last Post: 09-08-2009, 03:54 PM
  2. write nio.CharBuffer to file
    By Beju in forum New To Java
    Replies: 5
    Last Post: 10-19-2008, 08:20 PM
  3. Write to file
    By esadeghi in forum Advanced Java
    Replies: 1
    Last Post: 05-21-2008, 01:13 PM
  4. File Write Error
    By vikain in forum Advanced Java
    Replies: 5
    Last Post: 01-02-2008, 04:38 AM
  5. Write unicode into file
    By vata2999 in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 03:04 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
  •