如何获得对失败的Java File对象调用的有意义的消息(mkdir,重命名,删除)


问题内容

在使用File.mkdir和朋友时,我注意到他们不会在失败时抛出异常!幸运的是,FindBugs指出了这一点,现在我的代码至少检查了返回值,但是我仍然看不到任何有关调用失败
原因 的有意义的信息!

我如何找出为什么对这些File方法的调用失败的原因?是否有一个很好的替代方案或库来处理此问题?

我在SO和Google上进行了一些搜索,发现关于该主题的信息很少。

[更新]我已经尝试了VFS,但它的异常不再有用的信息。 例如,尝试移动最近删除的目录导致Could not rename file "D:\path\to\fileA" to "file:///D:/path/do/fileB".没有提及fileA不再存在。

[更新]业务需求将我限制为仅JDK 1.6解决方案,因此JDK 1.7已发布


问题答案:

您可以调用本机方法,并以这种方式获取正确的错误代码。例如,c函数
mkdir具有错误代码,例如EEXIST和ENOSPC。您可以使用JNA轻松访问这些本机函数。如果支持*
nix和Windows,则需要创建此代码的两个版本。

对于Linux上的jna mkdir的示例,您可以执行此操作,

import java.io.IOException;

import com.sun.jna.LastErrorException;
import com.sun.jna.Native;

public class FileUtils {

  private static final int EACCES = 13;
  private static final int EEXIST = 17;
  private static final int EMLINK = 31;
  private static final int EROFS = 30;
  private static final int ENOSPC = 28;
  private static final int ENAMETOOLONG = 63;

  static void mkdir(String path) throws IOException {

    try {
      NativeLinkFileUtils.mkdir(path);

    } catch (LastErrorException e) {
      int errno = e.getErrorCode();
      if (errno == EACCES)
        throw new IOException(
            "Write permission is denied for the parent directory in which the new directory is to be added.");
      if (errno == EEXIST)
        throw new IOException("A file named " + path + " already exists.");
      if (errno == EMLINK)
        throw new IOException(
            "The parent directory has too many links (entries).  Well-designed file systems never report this error, because they permit more links than your disk could possibly hold. However, you must still take account of the possibility of this error, as it could result from network access to a file system on another machine.");
      if (errno == ENOSPC)
        throw new IOException(
            "The file system doesn't have enough room to create the new directory.");
      if (errno == EROFS)
        throw new IOException(
            "The parent directory of the directory being created is on a read-only file system and cannot be modified.");
      if (errno == EACCES)
        throw new IOException(
            "The process does not have search permission for a directory component of the file name.");
      if (errno == ENAMETOOLONG)
        throw new IOException(
            "This error is used when either the total length of a file name is greater than PATH_MAX, or when an individual file name component has a length greater than NAME_MAX. See section 31.6 Limits on File System Capacity.");
      else
        throw new IOException("unknown error:" + errno);
    }




  }
}

class NativeLinkFileUtils {
  static {
    try {
      Native.register("c");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  static native int mkdir(String dir) throws LastErrorException;

}