1
我有下面的代码读取下一行直到无编号文件的末尾(文件中的行没有数字),它工作得很好。现在,我想读取以前的行(向后读)。如果还有可能,洗牌(读随机线)。任何想法。阅读文件中的上一行
下面是一个例子:
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'来输出任意数量的期望范围。一旦你有一个数字,移动fil e指针基于给定的随机数。 这是您可以关注的链接。 http://docs.oracle.com/javase/1.4.2/docs/api/java/io/RandomAccessFile.html –