About Question enthuware.ocpjp.v8.2.1207 :

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

Moderator: admin

Post Reply
romulo8000@gmail.com
Posts: 2
Joined: Thu Dec 08, 2016 3:40 pm
Contact:

About Question enthuware.ocpjp.v8.2.1207 :

Post by romulo8000@gmail.com »

I tried this exactly code and it compiled for me and it is running properly, with no exceptions.

In this question, answer says:
java.nio.file.NoSuchFileException: test.txt

when trying to get the BufferedReader from Files.newBufferedReader(myFile, CharSet.forName("US-ASCII"))

In: https://docs.oracle.com/javase/8/docs/a ... t.Charset-

says the signature is:
public static BufferedReader newBufferedReader(Path path, Charset cs) throws IOException

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

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

Post by admin »

Then you probably have test.txt in the directory from which you are running the program. I checked it and it does throw NoSuchFileException.
-Paul.
If you like our products and services, please help us by posting your review here.

romulo8000@gmail.com
Posts: 2
Joined: Thu Dec 08, 2016 3:40 pm
Contact:

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

Post by romulo8000@gmail.com »

I found what was wrong here! Now it's working as described... :thumbup:

And java.nio.file.NoSuchFileException extends from java.io.IOException, so signature is fine:
public static BufferedReader newBufferedReader(Path path) throws IOException

ty

lenalena
Posts: 56
Joined: Tue Feb 21, 2017 4:24 pm
Contact:

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

Post by lenalena »

In javadoc, NoSuchFileException is explicitly listed only for the delete method of the Files class. The method asked about in this question - newBufferedReader - just lists IOException and a few others, but not this. For the exam - do we have to know which other methods throw this exception? And if so, is there some documentation on where this exception is used?

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

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

Post by admin »

No, no need to know about this exception for other methods. This particular method is important for the exam.
If you like our products and services, please help us by posting your review here.

lenalena
Posts: 56
Joined: Tue Feb 21, 2017 4:24 pm
Contact:

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

Post by lenalena »

OK, thank you!

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

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

Post by __JJ__ »

Hi
As has been said, the API states that Files.newBufferedReader throws java.io.IOException, not java.nio.file.NoSuchFileException

In the OCA 8 exam you made the point in a number of questions that some method or other of String in practice throws StringIndexOOBE, but according to the API it throws IOOBE, and that that is what we should remember and answer if it comes up on the exam.

Here it looks like you are doing the opposite: the actual error in practice is nio.file.NSFE, the API lists it as io.IOE, but you are testing us on our knowledge of the actual error not the official error as per the API.

Am I misunderstanding?
Thank you.

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

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

Post by admin »

No, you are correct. But you need to realize that not everything will ever be 100% consistent because questions are developed by multiple people and they are developed based on feedback from multiple test takers. The people who developed questions for the real exam are also humans and so you will see some inconsistency there as well. However, since the real exam questions and answers are not revealed, you do not hear about them.

So, the only thing we can do is make you aware of the issues involved so that even if you do not agree with our answer, you can at least arrive at an informed decision yourself.

So yes, in this case, it is not consistent with the question in other exam but that is probably because the author felt this is closer to the real exam question. Again, I am only guessing. Either way, the answer will have some uncertainty and you have to go with one the option that you feel is the most logical.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

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

Post by __JJ__ »

OK I understand. Thanks very much.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

Post by Javier »

Hi Paul!

Why this code is not printing anything? I wrote "Hello World".

Code: Select all

public class IOTest {
    public static void main(String[] args) throws IOException {
        Path myfile = Paths.get("test.txt");
        File file = myfile.toFile();
        FileWriter fw = new FileWriter(file);
        BufferedWriter bfw = new BufferedWriter(fw);
        bfw.write("Hello World");
        myfile = file.toPath();
        try(BufferedReader bfr = Files.newBufferedReader(myfile, Charset.forName("US-ASCII") )){
            String line = null;
            while( (line = bfr.readLine()) != null){
                System.out.println(line);
            }
        }catch(Exception e){
            System.out.println(e);
        }
    }
}
What I have to do to write to this file?

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

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

Post by admin »

call bfw.flush() and bfw.close() after writing hello world to the file before reading.
If you like our products and services, please help us by posting your review here.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

Post by Javier »

Thank you Paul!

Post Reply

Who is online

Users browsing this forum: No registered users and 70 guests