我使用以下函数查找所有以 word 作为子字符串的行号。
public ArrayList<Integer> find(String word, File text) throws IOException {
LineNumberReader rdr = new LineNumberReader(new FileReader(text));
ArrayList<Integer> results = new ArrayList<Integer>();
try {
String line = rdr.readLine();
if (line.contains(word)) {
results.add(rdr.getLineNumber());
}
} finally {
rdr.close();
}
return results;
}
但是,当我将上面的函数调用为,我将lineNumberList的大小设为0(假设该文件至少包含一次 word 作为子字符串)
IProject project = (IProject)((IAdaptable)firstElement).getAdapter(IProject.class);
IFile manifest = project.getFile("AndroidManifest.xml");
File manifestfile = manifest.getRawLocation().makeAbsolute().toFile();
ArrayList<Integer> lineNumberList = new ArrayList<Integer>();
lineNumberList = find(PermissionInfo[0].trim(), manifestfile);
答案 0 :(得分:1)
如果文件中有多行,则可能需要for循环来包装getLine()。
答案 1 :(得分:1)
循环通过项目,即
public ArrayList<Integer> find(String word, File text) throws IOException {
LineNumberReader rdr = new LineNumberReader(new FileReader(text));
ArrayList<Integer> results = new ArrayList<Integer>();
try {
String line = rdr.readLine();
while(line != null){
if (line.contains(word)) {
results.add(rdr.getLineNumber());
}
line = rdr.readLine();
}
} finally {
rdr.close();
}
return results;
}