Uploaded image for project: 'Commons IO'
  1. Commons IO
  2. IO-472

FileUtils.openOutputStream doesn't create file if it doesn't exist

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Incomplete
    • 2.4
    • None
    • Utilities
    • None

    Description

      The javadoc for this method has a pretty unambiguous statement: "The file will be created if it does not exist." However, this isn't happening. The code is pretty clear on this:

      public static FileOutputStream openOutputStream(File file, boolean append) throws IOException {
              if (file.exists()) {
                  if (file.isDirectory()) {
                      throw new IOException("File '" + file + "' exists but is a directory");
                  }
                  if (file.canWrite() == false) {
                      throw new IOException("File '" + file + "' cannot be written to");
                  }
              } else {
                  File parent = file.getParentFile();
                  if (parent != null) {
                      if (!parent.mkdirs() && !parent.isDirectory()) {
                          throw new IOException("Directory '" + parent + "' could not be created");
                      }
                  }
              }
              return new FileOutputStream(file, append);
          }
      

      If it doesn't exist, it will just try to create a FileOutputStream, which throws a FileNotFoundException.

      Attachments

        Activity

          People

            Unassigned Unassigned
            dkarr David M. Karr
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: