首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Java中解析/proc/mount

在Java中解析/proc/mount
EN

Stack Overflow用户
提问于 2012-11-27 06:23:45
回答 2查看 996关注 0票数 0

我正在尝试解析在Linux环境中运行cat /proc/mounts的结果。

当挂载包含特殊字符(如空格)时,将对其进行转义。一个这样的例子是

/home/user/media\040/image.dd /media/dd ...,其中\040实际上是一个空格。

我正在尝试用java解析它,但我不想写一个手动解析器,因为我必须相信这已经完成了,而且比我要做的更健壮。有没有专门负责解码的类?我找到了一两个八进制解码器,但它们不能处理文本和八进制混合的情况。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-05-17 21:19:44

我最终使用了Apache Commons Lang。

http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/StringEscapeUtils.html#unescapeJava(java.lang.String)

这似乎能正确地解决所有问题。

票数 1
EN

Stack Overflow用户

发布于 2012-11-27 10:45:19

您可以在C中使用getmntent解析/proc/mount。

您可以根据规则在Java中实现它:

空格(\040)、制表符(\011)、换行符(\012)和反斜杠(\134)

或者none表示交换分区。

男人得到了内容:

内容结构定义如下:

代码语言:javascript
复制
       struct mntent {
           char *mnt_fsname;   /* name of mounted file system */
           char *mnt_dir;      /* file system path prefix */
           char *mnt_type;     /* mount type (see mntent.h) */
           char *mnt_opts;     /* mount options (see mntent.h) */
           int   mnt_freq;     /* dump frequency in days */
           int   mnt_passno;   /* pass number on parallel fsck */
       };

   Since  fields  in  the mtab and fstab files are separated by whitespace, octal escapes are
   used to represent the four characters space (\040), tab (\011), newline (\012)  and  back-
   slash  (\134) in those files when they occur in one of the four strings in a mntent struc-
   ture.  The routines addmntent() and getmntent() will convert from string representation to
   escaped representation and back.

另一种方式:直接解析mount的结果。将它们按" on“、"type”等拆分可能会更简单。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13574188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档