• Post Reply Bookmark Topic Watch Topic
  • New Topic

Encode Data

 
ssaharshitha bobba
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
ssaharshitha bobba
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why they have kept InputStreamReader and Charset for the same purpose or is there any difference?
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
process one makes the things simple for you.from file it gets you a fileinput stream and then from that you get a input stream reader(and while creating the reader you specify that the bytes are utf-8).using the reader you can access character by character.but still i do not feel that your purpose of converting ascii to utf is done ..nor i could think of any use of this.Say for example the file is a simple text file containing some english text.in that case utf would not get you any different set of bytes.As the Ascii codepage is subset of UTF.Please correct me if i am wrong.
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, anything encoded in ASCII (without any 8bit extension) is already encoded in UTF-8. This is a feature of UTF-8.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!