【发布时间】:2012-12-23 11:58:19
【问题描述】:
我在下面有这段代码,它读取下一行直到未编号文件的末尾(文件中的行没有数字),它工作得非常好。现在,我想读取前一行(向后读取)。如果可能的话,随机播放(阅读随机行)。任何想法。
这是一个例子:
InputStream in;
BufferedReader reader;
String qline;
try {
in = this.getAssets().open("quotations.txt");
reader = new BufferedReader(new InputStreamReader(in));
qline = reader.readLine();
quote.setText(qline);
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
在我的 onClick 方法中,我有一个下一步按钮
//code
else if (v.getId() == R.id.next) {
try{
if (( qline = reader.readLine()) != null) {
// myData = myData + qline;
quote.setText(qline);
}
} catch (java.io.FileNotFoundException e) {
// do something if the myfilename.txt does not exits
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
else if (v.getId() == R.id.back) {
// code for the back option
}
【问题讨论】:
-
嗯,你可以使用
Math.Random来输出任意范围的随机数。一旦你有了一个数字,根据给定的随机数移动文件指针。这是您可以访问的链接。 docs.oracle.com/javase/1.4.2/docs/api/java/io/…
标签: android pointers random file-handling