簡體   English   中英

了解一些Java代碼-我需要一些解釋

[英]Understanding some Java code - I need a little explaination

誰能解釋一下此代碼及其組件的功能嗎? 我對使用流程或Android本機代碼不是很熟悉。 如果有人可以解釋此代碼的工作原理,那將是很好的:

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 Hamid

兩種方法都接受文件名作為參數,並運行cat實用程序將文件路徑傳遞給它。 然后,這兩種方法都將讀取外部進程的輸出:首先使用Scanner ,其次使用StreamUtils一次讀取所有內容,然后將內容解析為整數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM