现在我正在开发一个阅读器。如果txt文件太大,读取时间会很长,没有反应。所以我想设置一个方法,就像它可以在洗澡时读取 txt 文件一样。
我写了下面的代码。它可以翻页。但它不能连续翻页。我应该怎么做?
Vector string;
int begin = 0;
public void readTxtByPage(String fileName) {
string.clear();
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(filePath + fileName);
br = new BufferedReader(fr);
br.skip(begin);
String content = "";
char ch;
int line = 0;
int w;
int len;
int start;
FontMetrics fm = paint.getFontMetrics();
fontHeight = (int) Math.ceil(fm.descent - fm.top) + 2;
pageLineNum = textHeight / fontHeight;
float[] widths = new float[1];
while ((content = br.readLine()) != null) {
len = content.length();
w = 0;
start = 0;
for (int i = 0; i < len; i++) {
ch = content.charAt(i);
paint.getTextWidths(String.valueOf(ch), widths);
w += Math.ceil(widths[0]);
if (w > textWidth) {
string.addElement(content.substring(start, i));
begin += (i - start);
start = i;
w = 0;
line++;
if (line >= pageLineNum) {
System.out.println("begin===>"+begin);
return;
}
}
}
string.addElement(content.substring(start));
begin += (len + 2 - start);
line++;
if (line >= pageLineNum) {
return;
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return;
}
提前致谢!