views:

114

answers:

2

I need to implement a paging widget that would be able to read an arbitrarily large text file. widget will be used by different apps with a wide range of hardware (mobile with low ram on low end) so need to be fairly memory stingy and efficient. the amount to be paged is also going to be arbitrarily different for each user. is there any free sample code that has implemented this somewhere? i'm looking for a java snippet really if possible.

A: 

You basically need to remember the amount of lines you already have read and then skip the same amount of lines in the while ((line = bufferedReader.readLine()) != null) statement.

BalusC
I guess the reading through the file is pretty obvious but my bigger concern is the management of the current/next page and the user experience as they scroll through the app.
For quicker response, consider an embedded database. E.g. JavaDB, Derby or HSQLDB.
BalusC
This loop might take a while for arbitrarily large files ...
meriton
Is there a paging or pagination pattern that would encompass the reading of the data and managing the concept of previous and next page? It seems like both the RandomAcessFile() and the BufferedReader() solution maybe fairly simple to code. I guess the challenge I am facing is the management of the "pages" and ensuring decent performance.Thanks for the responses so far.
RandomAccessFile is in theory faster, but it's also more work because you need to encode the characters yourself and find the linebreaks yourself. Maybe a combination of both should be doable.
BalusC
+2  A: 

java.io.RandomAccessFile should be helpful here, as it allows you start reading anywhere in the file. This enables you to only keep the current page in memory.

meriton