簡體   English   中英

Android從末尾讀取文本文件

[英]Android read text file from end

在Android中,我有一個文本文件,該文件可以更大或更小,然后33行。 如果大於33行,則讀取LAST 33行。 如果少於或等於33行,則讀取ALL行。 最快的方法,謝謝

好,這就是我得到的

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++;

我認為最快的讀取文件的方式仍將是使用java.io.Reader進行流式傳輸,同時讀取行並將它們放入預先分配的所需大小的數組中(在您的情況下為33),可以通過以下方式提前放置索引:

(idx++ % 33)

這樣,數組最多可以容納33行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM