我在assets文件夹(assets/question.txt(中的txt文件中提取了10行,从下面的代码中,我可以从assets文件夹中的.txt文件开始逐行获取。但我想从4行的txt文件开始。请帮忙。
BufferedReader reader_ques;
try {
reader_ques = new BufferedReader(new InputStreamReader(getAssets().open("question.txt")));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
line_q = reader_ques.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (line_q != null)
{
question_tv.setText(line_q);
}
最简单的方法是读取它,忽略它们。例如:
try {
for(int i=0 ;i < 4; i++) {
reader_ques.readLine();
}
while ((line_q = reader_ques.readLine()) != null) {
// do something with line_q
}
} catch (IOException e) {
e.printStackTrace();
}
或者你可以有一个字符串数组列表:
ArrayList<String> fileContent = new ArrayList<String>();
try {
while ((line_q = reader_ques.readLine()) != null) {
fileContent.add(line_q);
}
}
在第二种情况下,文件的第一行位于ArrayList
的索引0,依此类推
如果你想要第五个字符串,你只需要做
String myContent = fileContent.get(5)
String line = FileUtils.readLines(file).get(lineNumber);
试试这个。。
如果你有bufferReader,那么试试下面的
LineIterator lineIterator = IOUtils.lineIterator(reader_ques);
for (int lineNumber = 0; lineIterator .hasNext(); lineNumber++) {
String line = (String) lineIterator .next();
if (lineNumber == expectedLineNumber) {
return line;
}
}
要使用上面的代码,您需要这个库-http://commons.apache.org/proper/commons-io/download_io.cgi
或者这个-->http://www.motorlogy.com/apache//commons/io/binaries/commons-io-2.4-bin.zip