谁能准确解释一下这段代码及其组件的作用吗?我对使用进程或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/