Page 1 of 1

About Question enthuware.ocpjp.v8.2.1207 :

Posted: Sun Jan 15, 2017 5:07 pm
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

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

Posted: Sun Jan 15, 2017 11:32 pm
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.

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

Posted: Mon Jan 16, 2017 12:15 pm
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

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

Posted: Tue May 16, 2017 8:23 pm
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?

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

Posted: Tue May 16, 2017 9:38 pm
by admin
No, no need to know about this exception for other methods. This particular method is important for the exam.

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

Posted: Tue May 16, 2017 9:57 pm
by lenalena
OK, thank you!

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

Posted: Sat Jul 28, 2018 9:03 am
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.

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

Posted: Sat Jul 28, 2018 9:27 am
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.

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

Posted: Sat Aug 04, 2018 8:25 am
by __JJ__
OK I understand. Thanks very much.

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

Posted: Fri Oct 16, 2020 4:55 pm
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?

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

Posted: Fri Oct 16, 2020 7:41 pm
by admin
call bfw.flush() and bfw.close() after writing hello world to the file before reading.

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

Posted: Sat Oct 17, 2020 6:18 am
by Javier
Thank you Paul!