繁体   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