About Question enthuware.ocpjp.v8.2.1551 :

Moderator: admin

Post Reply
frakcio
Posts: 4
Joined: Sat Dec 14, 2013 4:15 pm
Contact:

About Question enthuware.ocpjp.v8.2.1551 :

Post by frakcio » Sat Jan 20, 2018 4:01 pm

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

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

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

Post by admin » Sat Jan 20, 2018 4:39 pm

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.
If you like our products and services, please help us by posting your review here.

frakcio
Posts: 4
Joined: Sat Dec 14, 2013 4:15 pm
Contact:

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

Post by frakcio » Sat Jan 20, 2018 4:50 pm

I agree, but something should be corrected. Answer or explanation. They are not fit together

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

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

Post by admin » Sat Jan 20, 2018 4:53 pm

Can you post the exact code that you tried and on which platform?
If you like our products and services, please help us by posting your review here.

frakcio
Posts: 4
Joined: Sat Dec 14, 2013 4:15 pm
Contact:

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

Post by frakcio » Sat Jan 20, 2018 5:13 pm

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

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

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

Post by admin » Sat Jan 20, 2018 10:30 pm

thanks! I have updated the answer.

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.1551 :

Post by __JJ__ » Wed Aug 08, 2018 11:15 am

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)

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest