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

Files.createXXX throws confusing NoSuchFileException when parent is not a directory

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 7
    • core-libs
    • Cause Known
    • x86
    • windows_7

      FULL PRODUCT VERSION :
      java version "1.7.0-ea"
      Java(TM) SE Runtime Environment (build 1.7.0-ea-b134)
      Java HotSpot(TM) Client VM (build 21.0-b04, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Windows 7 Professional Version 6.1(Build 7601:Service Pack 1)

      A DESCRIPTION OF THE PROBLEM :
      The createFile() method in the Files class throws NoSuchFileException if the file already exists. I think this should be FileAlreadyExistsException. This is not mentioned in the documentation for b134.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute the method to create a file twice.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      FileAlreadyExistsException thrown.
      ACTUAL -
      NoSuchFileException is thrown.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class Test {
        public static void main(String[] args) {
          FileSystem fileSystem = FileSystems.getDefault();
          Path path = Paths.get("C:/Garbage/dir/dir1/dir2/junk.txt");
          try {
            Files.createDirectories(path.getParent());
            Files.createFile(path);
            Files.createFile(path);
          } catch(NoSuchFileException e) {
            System.err.println("File not created.\n" + e);
          } catch(FileAlreadyExistsException e) {
            System.err.println("File not created.\n" + e);
          } catch(IOException e) {
            System.err.println(e);
          }
        }
      }

      ---------- END SOURCE ----------

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: