Encode data
I want to encode the US-ASCII data into UTF-8.
For that what is the procedure.
I have to use
1.InputStreamReader and OutPutStreamReader
2.Charset/CharsetEncoder/CharsetDecoder
What is the difference between the two processes?
Process 1:
FileInputStream fis = new FileInputStream(inputFile);
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
FileOutputStream fos = new FileOutputStream(outputFile);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
Process 2:
Charset charset = Charset.forName(encode);
CharsetDecoder decoder = charset.newDecoder();
CharsetEncoder encoder = charset.newEncoder();
my question is:
is InputStreamReader, OutputStreamWriter and CharsetDecoder &CharsetEncoder are for the same purpose.
or they different functionality?
Here is the difference,
InputStreamReader
================
An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset.
CharsetDecoder
=============
An engine that can transform a sequence of bytes in a specific charset
into a sequence of sixteen-bit Unicode characters
similarly,
OutputStreamWriter
================
An OutputStreamWriter is a bridge from character streams to byte streams: Characters written to it are encoded into bytes using a specified charset.
CharsetEncoder
=============
An engine that can transform a sequence of sixteen-bit Unicode characters into a sequence of bytes in a specific charset.
check api for further differences.
http://java.sun.com/j2se/1.4.2/docs/api/java/io/OutputStreamWriter.html
http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/CharsetEncoder.html