栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

您可以调用本机方法,并以这种方式获取正确的错误代码。例如,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;}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/483562.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号