Page 1 of 1

About Question enthuware.ocpjp.v7.2.1538 :

Posted: Mon Sep 23, 2013 3:32 pm
by Student
"Observe that some combinations such as StandardOpenOption.CREATE, StandardOpenOption.READ do not make sense if put together and therefore an IllegalArgumentException will be thrown in such cases."

I think it might be that you can't specify READ for a Writer:

Code: Select all

import java.nio.charset.*;
import java.nio.file.*;
import java.io.*;
public class C15 {
    public static void main (String...args){
        try{
            Path p = Paths.get("C:\\Users\\XXX\\Documents\\java\\certification\\1z0-804\\xxx\\abc.txt");
            BufferedWriter bw = Files.newBufferedWriter(p
                                                        ,Charset.forName("UTF-8")
                                                        ,new OpenOption[]{StandardOpenOption.READ});                                                         
            bw.write("abc");
            bw.close();
        }
        catch(java.io.IOException e) {
            e.printStackTrace();
        }
    }
}

Exception in thread "main" java.lang.IllegalArgumentException: READ not allowed
        at java.nio.file.spi.FileSystemProvider.newOutputStream(Unknown Source)
        at java.nio.file.Files.newOutputStream(Unknown Source)
        at java.nio.file.Files.newBufferedWriter(Unknown Source)
        at C15.main(C15.java:9)
Cheers