Skip to main content

Encoding problem

2 replies [Last post]
Joined: 2006-05-15
Points: 0

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);
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.
Joined: 2004-12-24
Points: 0

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 ->

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 .

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


By Avatar Ng

Joined: 2004-04-20
Points: 0

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.