Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8241619

(fs) Files.newByteChannel(path, Set.of(CREATE_NEW, READ)) does not throw a FileAlreadyExistsException when the file exists

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P5 P5
    • 17
    • None
    • core-libs
    • None
    • b15

      The following example will not throw a FileAlreadyExistsException:

          @Test
          public void testit() throws Exception {
              Path osFile = Path.of("Hello.txt");
              Files.deleteIfExists(osFile);
              Files.writeString(osFile, "Hello World");

              try (SeekableByteChannel srcCh = Files.newByteChannel(osFile, Set.of(CREATE_NEW))) {
                  System.out.println("oops");
              }

              try (SeekableByteChannel srcCh = Files.newByteChannel(osFile, Set.of(CREATE_NEW, READ))) {
                  System.out.println("oops");
              }
          }



      If you specify Set.of(CREATE_NEW, WRITE) you will get the expected Exception

            bpb Brian Burkhalter
            lancea Lance Andersen
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: