如何为Java File对象的失败调用获取有意义的消息(mkdir,rename,delete)

在使用File.mkdir和朋友时,我注意到他们不会在失败时抛出exception! 值得庆幸的是,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; } 

您可以使用以下内容创建实用程序类:

 public int mkdir(File dirToCreate) throws IOException { if (dirToCreate.exists()) throw new IOException("Folder already exists"); if (!dirToCreate.getParent().canWrite()) throw new IOException("No write access to create the folder"); return dirToCreate.mkdir(); } public int rename(File from, File to) throws IOException, FileNotFoundException { if (from.equals(to)) throw new IllegalArgumentException("Files are equal"); if (!from.exists()) throw new FileNotFoundException(from.getAbsolutePath() + " is not found"); if (!to.getParent().exists()) throw new IllegalAccessException("Parent of the destination doesn't exist"); if (!to.getParent().canWrite()) throw new IllegalAccessException("No write access to move the file/folder"); return from.renameTo(to); } 

当然这还不完整,但你可以解决这个问题。

使用JDK7的新文件API 。 它有更好的操作系统集成,并提供更详细的反馈。 例如,请参阅移动/重命名的文档 。

您可以执行该命令并捕获包含有意义消息的err输出。

这是一些最小的可执行代码(使用apache commons-exec ),演示了这可以如何工作:

 import org.apache.commons.exec.*; public static String getErrorMessage(String command) { CommandLine cmdLine = CommandLine.parse(command); DefaultExecutor executor = new DefaultExecutor(); ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream(); executor.setStreamHandler(new PumpStreamHandler(out, err)); try { executor.execute(cmdLine); } catch (Exception e) { return err.toString().trim(); } return null; // no error occurred } 

这是对此代码的测试,显示了各种文件操作错误情况:

 public static void main(String[] args) throws Exception { System.out.println(getErrorMessage("cp fake.file x")); System.out.println(getErrorMessage("cp /tmp /tmp")); System.out.println(getErrorMessage("mkdir /Volumes")); System.out.println(getErrorMessage("mv /tmp /")); System.out.println(getErrorMessage("mv fake.file /tmp")); } 

输出(在mac osx上运行):

 cp: fake.file: No such file or directory cp: /tmp is a directory (not copied). mkdir: /Volumes: File exists mv: /tmp and /tmp are identical mv: rename fake.file to /tmp/fake.file: No such file or directory 

您可以将上述方法包装在抛出IOException的方法中,该方法获取消息后,可以使用正则表达式匹配解析关键参数和映射消息,或者contains特定的IOExceptions并抛出它们,例如:

 if (message.endsWith("No such file or directory")) throw new FileNotFoundException(); // Use IOExceptions if you can if (message.endsWith("are identical")) throw new IdenticalFileException(); // Create your own Exceptions that extend IOException 

如果你想抽象它以用于多种OS风格,你必须为每个平台实现代码(对于任何给定的文件操作/结果,windows和* nix使用不同的shell命令/错误消息)。

如果获得此答案的赏金,我将发布完整整洁的工作代码版本,包括用于文件操作的时尚enum

您可以使用jakarta VFS。 FileObject.createFolder()抛出保存错误代码的FileSystemException 。 这不是实现@Martijn Courteaux提供的逻辑