Files.size(Path) 可以抛出除 FileNotFoundException 以外的哪些异常?

Which exceptions other than FileNotFoundException can Files.size(Path) throw?

我有一台 RedHat 7 机器,想获取驻留在 Ext4 上的常规文件的大小。

Files.size() declares an IOException. Which other exceptions might be thrown, other than FileNotFoundException

使用 Java 7 和 nio 包。

此方法不能抛出 FileNotFoundException。

它可以抛出一个FileSystemException,其中之一:

  • NoSuchFileException,
  • 拒绝访问异常,
  • 其他。

它也可以抛出 IOException,这意味着 I/O 级错误(可能是磁盘问题)而不是文件系统级错误。

简而言之,最好的处理方法是:

try {
    Files.size(...);
} catch (FileSystemException e) {
    // deal with fs level error
} catch (IOException e) {
    // deal with I/O level error
}