1

In Android I have a text file which can be LARGER or SMALLER then 33 lines. If larger then 33 lines read the LAST 33 lines. If LESS or equal to 33 lines then read ALL lines. The fastest way to do it thanks

OK this is what i got

MAXLINECOUNT=33;
File f = new File("file.......");                  
if (f.exists())
{
      String readString = new String();
     int i,l;
     i=0;
     l=0;
     LineNumberReader  reader = new LineNumberReader(new FileReader(new File("/mnt/sdcard/data.txt")));
     while ((readString = reader.readLine()) != null) {}
     l= reader.getLineNumber();
     if (l>=MAXLINECOUNT) l=l-(MAXLINECOUNT-3);// i realy need only 30
     FileInputStream fileIS = new FileInputStream(f);
     BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
     while((readString = buf.readLine())!= null){
     if (i>l)
         {

                process hear ...............
             }
     i++;

1 Answers1

0

I think fastest way to read file will be still using java.io.Reader over stream, reading line at the time and placing them into preallocated array of desired size ( in your case 33 ) advance placement index by:

(idx++ % 33)

This way array will hold at most 33 last lines.

Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35