filesystems - How to get a meaningful message for failed calls to Java File objects (mkdir, rename, delete) -
while using file.mkdir, , friends notice don't throw exceptions on failure! thankfully findbugs pointed out , code @ least checks return value still see no way meaningful information why call fails!
how find out why calls these file methods fail? there alternative or library handles this?
i've done searches here on , google , found surprising little info on topic.
[update] i've given vfs try , exception don't have anymore useful information. example trying move directory had been deleted resulted in could not rename file "d:\path\to\filea" "file:///d:/path/do/fileb".
no mention filea no longer existed.
[update] business requirements limit me jdk 1.6 solutions only, jdk 1.7 out
you call native methods, , proper error codes way. example, c function mkdir has error codes eexist , enospc. can use jna access these native functions easily. if supporting *nix , windows need create 2 versions of code.
for example of jna mkdir on linux can this,
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 denied parent directory in new directory added."); if (errno == eexist) throw new ioexception("a file named " + path + " exists."); if (errno == emlink) throw new ioexception( "the parent directory has many links (entries). well-designed file systems never report error, because permit more links disk possibly hold. however, must still take account of possibility of error, result network access file system on machine."); if (errno == enospc) throw new ioexception( "the file system doesn't have enough room create new directory."); if (errno == erofs) throw new ioexception( "the parent directory of directory being created on read-only file system , cannot modified."); if (errno == eacces) throw new ioexception( "the process not have search permission directory component of file name."); if (errno == enametoolong) throw new ioexception( "this error used when either total length of file name greater path_max, or when individual file name component has length greater 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; }
Comments
Post a Comment