About Question enthuware.ocpjp.v8.2.1775 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
RoyEL1
Posts: 10
Joined: Tue Aug 09, 2011 3:23 pm

About Question enthuware.ocpjp.v8.2.1775 :

Post by RoyEL1 »

In the explanation:
FileWriter is a concrete subclass of java.io.Writer that writes data to the underlying file in default encoding. If you want to write text in a different encoding, you will need to create an OutputStreamWriter with that encoding. For example,

Code: Select all

OutputStreamWriter osw = new OutputStreamWriter(
new FileOutputStream("utf8.txt"), Charset.forName("UTF-8").newEncoder()  ); 
You can then create a BufferedWriter over this OutputStreamWriter.
Wouldn't you really need something like:

Code: Select all

        CharsetEncoder ce = Charset.forName("UTF-8").newEncoder(); 
        try (            
            FileOutputStream   fos = new FileOutputStream("utf8.txt");
            OutputStreamWriter osw = new OutputStreamWriter(fos, ce);
            ObjectOutputStream oos = new ObjectOutputStream(fos); ){
            
            oos.writeUTF("hello");
         }
Since ObjectOutputStream.writeUTF is supported and a BufferedWriter does not support writeUTF?

admin
Site Admin
Posts: 10045
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1775 :

Post by admin »

Why do you need BufferedWriter to support writeUTF method? BufferedWriter doesn't care about encoding. The encoding is taken care of by the underlying OutputStreamWriter.

Also, ObjectOutputStream is an entirely different thing. It is not relevant here.
-Paul.
If you like our products and services, please help us by posting your review here.

RoyEL1
Posts: 10
Joined: Tue Aug 09, 2011 3:23 pm

Re: About Question enthuware.ocpjp.v8.2.1775 :

Post by RoyEL1 »

True statement, but the original question was about using writeUTF, the BufferedWriter would just use write, and the encoding would already be taken care of, hence maybe the question should be using write with a comment of // using UTF. ?

admin
Site Admin
Posts: 10045
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1775 :

Post by admin »

The code in the question is trying to call writeUTF on BufferedReader, which is not possible because writeUTF is not available in BufferedReader.

The explanation shows one possible way to write in UTF, while still using a BufferedReader. It builds upon the concept mentioned in the explanation in a prior sentence, which says, "BufferedWriter only adds the functionality of buffering on top of a Writer. It doesn't directly deal with encoding. Encoding is handled by the underlying Writer object."

So while there could be other ways of doing so, that don't make the question or the explanation incorrect!
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 45 guests