我最初使用RIM的本机xml解析器方法来解析一个150k的文本文件,大约有5000行xml,但它需要大约2分钟才能完成,所以我尝试了一种基于行的格式:在BlackBerry上解析文本文件需要永久
标题:书名
线1
线线3
我应该能够读取该文件在较短的时间比需要眨眼,但它仍然是缓慢的。
标识符书籍是书籍对象的向量,行存储在书籍对象中的字符串矢量中。
class classs = Class.forName("com.Gui.FileLoader");
InputStream is = classs.getResourceAsStream(fileName);
int totalFileSize = IOUtilities.streamToBytes(is).length;
int totalRead = 0;
//Thought that maybe a shared input stream would be faster, in this case it't not.
SharedInputStream sis = SharedInputStream.getSharedInputStream(classs.getResourceAsStream(fileName));
LineReader lr = new LineReader(sis);
String strLine = new String(lr.readLine());
totalRead += strLine.length();
Book book = null;
//Loop over the file until EOF is reached, catch EOF error move on with life after that.
while(1 == 1){
//If Line = Title: then we've got a new book add the old book to our books vector.
if (strLine.startsWith("Title:")){
if (book != null){
books.addElement(book);
}
book = new Book();
book.setTitle(strLine.substring(strLine.indexOf(':') + 1).trim());
strLine = new String(lr.readLine());
totalRead += strLine.length();
continue;
}
int totalComplete = (int) ( ((double) totalRead/(double) totalFileSize) * 100.00);
_observer.processStatusUpdate(totalComplete , book.getTitle());
book.addLine(strLine);
strLine = new String(lr.readLine(), "ascii");
totalRead += strLine.length();
}
为什么你不用传统的功能呢?例如没有任何LineReader。手动处理一个字符读取循环.. – 2010-10-22 05:42:45