Page 1 of 1

About Question enthuware.ocpjp.v8.2.1551 :

Posted: Sat Jan 20, 2018 4:01 pm
by frakcio
I think that the answer 'e' is incorrect. new OpenOption[]{StandardOpenOption.READ, StandardOpenOption.SYNC}

1. In explanation you said that it is incorrect +
2. I've tried to implement it and got exception

Code: Select all

        OpenOption[] openOptions = new OpenOption[]{StandardOpenOption.READ, StandardOpenOption.SYNC};
        try {
            Files.newBufferedWriter(Paths.get("C:/test.txt"), openOptions);
        } catch (IOException e) {
        }
Exception in thread "main" java.lang.IllegalArgumentException: READ not allowed

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

Posted: Sat Jan 20, 2018 4:39 pm
by admin
Yes, that is a problem with this topic. Some combinations work on some platforms but not on others. The JavaDoc API description doesn't specify clearly the combinations that should work.

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

Posted: Sat Jan 20, 2018 4:50 pm
by frakcio
I agree, but something should be corrected. Answer or explanation. They are not fit together

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

Posted: Sat Jan 20, 2018 4:53 pm
by admin
Can you post the exact code that you tried and on which platform?

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

Posted: Sat Jan 20, 2018 5:13 pm
by frakcio
Sure. Code:

Code: Select all

public class App {
    public static void main(String[] args) {
        OpenOption[] openOptions = new OpenOption[]{StandardOpenOption.READ, StandardOpenOption.SYNC};
        try {
            Files.newBufferedWriter(Paths.get("C:/test.txt"), openOptions);
        } catch (IOException e) {
        }
    }
}
Output:

Code: Select all

Connected to the target VM, address: '127.0.0.1:63393', transport: 'socket'
Disconnected from the target VM, address: '127.0.0.1:63393', transport: 'socket'
Exception in thread "main" java.lang.IllegalArgumentException: READ not allowed
	at java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:429)
	at java.nio.file.Files.newOutputStream(Files.java:216)
	at java.nio.file.Files.newBufferedWriter(Files.java:2860)
	at java.nio.file.Files.newBufferedWriter(Files.java:2896)
	at pl.mal.App.main(App.java:34)

Process finished with exit code 1
jdk1.8.0_151
win10

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

Posted: Sat Jan 20, 2018 10:30 pm
by admin
thanks! I have updated the answer.

Paul.

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

Posted: Wed Aug 08, 2018 11:15 am
by __JJ__
Hi
TRUNCATE_EXISTING and APPEND are indeed not allowed, as they make zero sense:

Code: Select all

      Path p = Paths.get("c:\\tmp\\test1\\test1.txt");
        BufferedWriter br = Files.newBufferedWriter(p, Charset.forName("UTF-8")
        ,new OpenOption[] {StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.APPEND});

Exception in thread "main" java.lang.IllegalArgumentException: APPEND + TRUNCATE_EXISTING not allowed
        at sun.nio.fs.WindowsChannelFactory.newFileChannel(WindowsChannelFactory.java:160)
        at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:225)
        at java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:434)
        at java.nio.file.Files.newOutputStream(Files.java:216)
        at java.nio.file.Files.newBufferedWriter(Files.java:2860)
        at ocp8.practice2.T31.f3(T31.java:104)
        at ocp8.practice2.T31.main(T31.java:74)