任何人都可以详细解释这段代码及其组件的作用吗?我对使用进程或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
答案 0 :(得分:1)
两种方法都接受文件名作为参数,并运行cat
实用程序将文件路径传递给它。然后两个方法都读取外部进程的输出:首先使用Scanner
,第二个StreamUtils
一次读取所有内容,然后将内容解析为整数。