java - 理解一些 Java 代码 - 我需要一些解释

标签 java process

谁能准确解释一下这段代码及其组件的作用吗?我对使用进程或Android native 代码不是很熟悉。如果有人能解释这段代码是如何工作的,那就太好了:

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。

问候, 阿奇夫·哈米德

最佳答案

这两种方法都接受文件名作为参数,并运行将文件路径传递给它的 cat 实用程序。然后,这两种方法都会读取外部进程的输出:第一个使用 Scanner,第二个使用 StreamUtils 一次性读取所有内容,然后将内容解析为整数。

关于java - 理解一些 Java 代码 - 我需要一些解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9657497/

相关文章:

c# - 如果进程以编程方式运行(来自 C#),则“没有这样的文件或目录”

process - 代码质量: how to measure developers performance?

c# - 如何识别 ffmpeg 管道已结束?

Java用户界面框架

java - 在 JApplet 上使用 KeyAdapter 和 Thread 重置/重新启动我的迷你游戏

c# - 如何使用 C# 监视进程的 IO 事件?

python - 如何在Python中并行运行多个进程

java - 如何从 input.txt 文件创建字符串数组

java - 如何在 Java 中将多个(任意数量的)Set<String> 合并为一个?

java - 如何通过xml签名文件提取 "original"内容