Results 1 to 5 of 5
Thread: [SOLVED] File Write in File
- 10-27-2008, 03:05 AM #1
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 13
[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 --> " Côte 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) { }
Mind only knows what lies near the heart, it alone sees the depth of the soul.
- 10-27-2008, 03:25 AM #2
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);
- 10-27-2008, 03:58 AM #3
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 13
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.
- 10-27-2008, 04:21 AM #4
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 13
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 insteadLast edited by Eku; 10-27-2008 at 04:24 AM.
Mind only knows what lies near the heart, it alone sees the depth of the soul.
- 10-27-2008, 05:01 AM #5
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 13
Similar Threads
-
how to write onto a file
By mirage_87 in forum New To JavaReplies: 6Last Post: 09-08-2009, 04:54 PM -
write nio.CharBuffer to file
By Beju in forum New To JavaReplies: 5Last Post: 10-19-2008, 09:20 PM -
Write to file
By esadeghi in forum Advanced JavaReplies: 1Last Post: 05-21-2008, 02:13 PM -
File Write Error
By vikain in forum Advanced JavaReplies: 5Last Post: 01-02-2008, 05:38 AM -
Write unicode into file
By vata2999 in forum New To JavaReplies: 1Last Post: 08-08-2007, 04:04 PM
Bookmarks