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

Javadoc in FileUtils does not reflect code for thrown exceptions

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 2.12.0, 2.13.0
    • 2.15.0
    • Utilities
    • None

    Description

      The JavaDoc of FileUtils#readFileToString(...) is stating that a FileNotFoundException is thrown if the file doesn't exist. This isn't true with version 2.12 and above, including the current 2.14 snapshot, where a NoSuchFileException is thrown. I haven't tested, but I guess that other methods like FileUtils#readFileToByteArray(...) are affected, too, as the exception is coming via Files#newByteChannel(...).

      Root cause seems to be, like in IO-800, this commit from 2021, where openInputStream(file) (which is callingĀ 
      new FileInputStream(file) from java.io) is replaced by Files.newInputStream(file.toPath()) from java.nio.

      I'm unsure if just the JavaDoc should be updated or the old behavior be restored: I noticed the bug because we catch a FileNotFoundException, which doesn't work for 2.12 and above.

      Old Stacktrace
      java.io.FileNotFoundException: doesnt-exist.txt (No such file or directory)
      	at java.base/java.io.FileInputStream.open0(Native Method) ~[na:na]
      	at java.base/java.io.FileInputStream.open(FileInputStream.java:219) ~[na:na]
      	at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157) ~[na:na]
      	at org.apache.commons.io.FileUtils.openInputStream(FileUtils.java:2388) ~[commons-io-2.11.0.jar:2.11.0]
      	at org.apache.commons.io.FileUtils.readFileToString(FileUtils.java:2506) ~[commons-io-2.11.0.jar:2.11.0]
      	at org.apache.commons.io.FileUtils.readFileToString(FileUtils.java:2526) ~[commons-io-2.11.0.jar:2.11.0]
      
      New Stacktrace
      java.nio.file.NoSuchFileException: doesnt-exist.txt
      	at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92) ~[na:na]
      	at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111) ~[na:na]
      	at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116) ~[na:na]
      	at java.base/sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:219) ~[na:na]
      	at java.base/java.nio.file.Files.newByteChannel(Files.java:371) ~[na:na]
      	at java.base/java.nio.file.Files.newByteChannel(Files.java:422) ~[na:na]
      	at java.base/java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:420) ~[na:na]
      	at java.base/java.nio.file.Files.newInputStream(Files.java:156) ~[na:na]
      	at org.apache.commons.io.FileUtils.lambda$readFileToString$12(FileUtils.java:2616) ~[commons-io-2.13.0.jar:2.13.0]
      	at org.apache.commons.io.IOUtils.toString(IOUtils.java:3177) ~[commons-io-2.13.0.jar:2.13.0]
      	at org.apache.commons.io.IOUtils.toString(IOUtils.java:3152) ~[commons-io-2.13.0.jar:2.13.0]
      	at org.apache.commons.io.FileUtils.readFileToString(FileUtils.java:2616) ~[commons-io-2.13.0.jar:2.13.0]
      	at org.apache.commons.io.FileUtils.readFileToString(FileUtils.java:2599) ~[commons-io-2.13.0.jar:2.13.0]
      

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              chkpnt Gregor Dschung
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: