2012-11-26 23 views
0

我想解析在Linux環境下運行cat /proc/mounts的結果。解析Java中的/ proc/mounts

當坐騎包含特殊字符(如空格)時,它們會被轉義。一個例子是

/home/user/media\040/image.dd /media/dd ...其中\ 040實際上是一個空格。

我想解析這個在java中,但不想寫一個手動解析器,因爲我必須相信這已經完成,並做得比我更強大。有沒有需要解碼的類?我找到一個或兩個八進制解碼器,但它們不適用於文本和八進制混合。

回答

1

您可以分析的/ proc/C中

可以使用getmntent坐騎

可以實現它在Java accoring排除:

空間(\ 040),標籤(\ 011),換行符(\ 012 )和反斜槓(\ 134)

或者none表示切換分區。

人getmntent:

的mntent結構在如下定義:

 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. 

的另一種方法:解析的 「安裝」 直接結果。通過「打開」,「類型」等來拆分它們可能更簡單。

+0

問題是你可以逃脫字符,(合法的,但真正愚蠢的名字)。所以「某些東西」可能是有效的。另外,關鍵詞的分裂對於裝載這個名字可能是相當危險的。我正在尋找一種始終有效的解決方案。實際上,我已經編寫了一個使用getmntent並輸出爲已知格式的C程序,但我希望避免使用Jar發佈本地工具。 – Ryan