Skip to main content

Encoding problem

2 replies [Last post]
bjmanu
Offline
Joined: 2006-05-15

I have a file whose contents are written in french. I want to read that file and edit the file and then write the contents back to the same file. How do I do that?

I am reading file content is instanceof ByteBlock only. Then I am using
byte[] blk = ((ByteBlock) content).bytes();
FileOutputStream fs = new FileOutputStream("a.txt");
fs.write (blk, 0, blk.length);
fs.close();
During writing a file I want use encoding like ISO-8859-1. I can't use Writer because my contents are byte stream. Same way I can't use BufferedOutputStream or DataOutputStream because its not supporting encoding. How do I solve this. Please help me.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
avatar21
Offline
Joined: 2004-12-24

I had written a UTF-8 tools for Java' properties and HTML files encoding, called "Unicaode to ASCII" tool.

It has some simple UI, thought it provides more user-friendliness.

Please find the free tool here in my blog -> http://avatar21.superihost.com/index.php?entry=entry060128-133758

You remind me to take care of the other standards like ISO-8859-1. Perhaps if you found any conversion formula for those encodings, please mail me at avatar21@dev.java.net .

I'll add them in the next release of the tool as well.

Thanks.

By Avatar Ng

dominikweifieg
Offline
Joined: 2004-04-20

If everything you have is a byte[] and you want to write it as char[]/String in a specified encoding to some Stream, you could e. g. do something like:

java.nio.ByteBuffer byteBuffer = java.nio.ByteBuffer.wrap(blk); //blk is your data
java.nio.CharBuffer charBuffer = byteBuffer.asCharBuffer();

From the char buffer you can read char or char[] and write those to a writer with your chosen encoding.