2012-03-11 83 views
1

任何人都可以請解釋到底什麼是這個代碼及其組件正在做什麼?我不熟悉使用進程或Android本機代碼。這將是巨大的,如果有人可以解釋這個代碼是如何工作的:瞭解一些Java代碼 - 我需要一點解釋

private static MatchResult matchSystemFile(final String pSystemFile, final String pPattern, final int pHorizon) throws SystemUtilsException { 
    InputStream in = null; 
    try { 
     final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start(); 

     in = process.getInputStream(); 
     final Scanner scanner = new Scanner(in); 

     final boolean matchFound = scanner.findWithinHorizon(pPattern, pHorizon) != null; 
     if(matchFound) { 
      return scanner.match(); 
     } else { 
      throw new SystemUtilsException(); 
     } 
    } catch (final IOException e) { 
     throw new SystemUtilsException(e); 
    } finally { 
     StreamUtils.close(in); 
    } 
} 

private static int readSystemFileAsInt(final String pSystemFile) throws SystemUtilsException { 
    InputStream in = null; 
    try { 
     final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start(); 

     in = process.getInputStream(); 
     final String content = StreamUtils.readFully(in); 
     return Integer.parseInt(content); 
    } catch (final IOException e) { 
     throw new SystemUtilsException(e); 
    } catch (final NumberFormatException e) { 
     throw new SystemUtilsException(e); 
    } finally { 
     StreamUtils.close(in); 
    } 
} 

我需要了解這部分,如何處理正在採取兩個字符串,我無法理解這個代碼是如何工作的兩個文件(對我來說看起來像/ system/bin/cat和pSystemFile字符串是文件的路徑)並提取所需的信息。

final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start(); 

    in = process.getInputStream(); 
    final Scanner scanner = new Scanner(in); 

    final boolean matchFound = scanner.findWithinHorizon(pPattern, pHorizon) != null; 
    if(matchFound) { 
     return scanner.match(); 
    } 

此代碼取自AndEngines Utils。

問候, Aqif哈米德

+8

你已經發布了很多代碼,並沒有說明你不明白哪一部分。請更具體一點 - 有人刻意地解釋每一行,沒有意義,只能發現只有一個方面你不明白。 – 2012-03-11 18:11:54

+0

您還可以使用調試器對其進行檢查。 – 2012-03-11 18:17:19

+0

已編輯!對不起,期待您的意見。 – 2012-03-11 18:23:08

回答

1

兩種方法都接受文件名作爲參數和運行cat工具傳遞文件路徑。然後這兩種方法都讀取外部進程的輸出:首先使用Scanner,第二個StreamUtils一次讀取所有內容,然後將內容解析爲整數。

+0

謝謝你的評論AlexR,它是有幫助的。 此文件是'/ system/bin/cat'還是cat實用程序命令的字符串路徑?請讓我知道,如果我需要學習一些相關的材料來理解這些命令來運行像'貓'這樣的實用程序 – 2012-03-11 18:26:59

+0

'貓'是位於'/ system/bin /' – AlexR 2012-03-12 07:40:27

+0

下的實用程序感謝您的幫助.. :) – 2012-03-12 15:21:46