You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Gregor Dschung (Jira)" <ji...@apache.org> on 2023/09/27 07:52:00 UTC

[jira] [Created] (IO-810) Wrong JavaDoc regarding exception in FileUtils

Gregor Dschung created IO-810:
---------------------------------

             Summary: Wrong JavaDoc regarding exception in FileUtils
                 Key: IO-810
                 URL: https://issues.apache.org/jira/browse/IO-810
             Project: Commons IO
          Issue Type: Bug
          Components: Utilities
    Affects Versions: 2.13.0, 2.12.0
            Reporter: Gregor Dschung


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|https://github.com/apache/commons-io/commit/dcb09db922e1edbd6dc6cb1531b827451d58aa83#diff-0bd8160a11e12ea1f96476a00e78d0a95b965d07c7fe126e1af498c08511dd70] 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.

{code:title=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]
{code}

{code:title=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]
{code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)