简体   繁体   中英

How to read text file from server and display only e.g. 100 last lines of it in textview

The piece of code below is from my android app. The purpose is to read text file from server and display 100 last lines of it in textview. Obviously, the code below works correctly only if number of lines in text file is 2000.
What I can think of is that I first need to go through all lines to count the number of them and then go through again to display 100 last lines in textview. I have tried nested BufferedReaders but haven't succeeded.Any ideas?

 protected Void doInBackground(String...params){
        URL url;
        int lines = 0;
        try {
            //create url object to point to the file location on internet
            url = new URL(params[0]);
            //make a request to server
            HttpURLConnection con=(HttpURLConnection)url.openConnection();
            //get InputStream instance
            InputStream is=con.getInputStream();
            //create BufferedReader object

            BufferedReader br=new BufferedReader(new InputStreamReader(is));

            String line;

            //read content of the file line by line
            while((line=br.readLine())!=null){
                if(++lines > 1900)
                    text+=line + "\n";
            }

            br.close();

        }catch (Exception e) {
            e.printStackTrace();
            //close dialog if error occurs
            if(pd!=null) pd.dismiss();
        }
        return null;
    }

    protected void onPostExecute(Void result){
        //close dialog
        if(pd!=null)
            pd.dismiss();
        TextView txtview = (TextView) findViewById(R.id.text_view);
        txtview.setMovementMethod(ScrollingMovementMethod.getInstance());
        //display read text in TextView
        txtview.setText(text);
    }
}

}

One solution would be to add everything to an ArrayList and then make your text out of the last hundred records. To improve the functionality you can start removing the lines from the top once the count reaches beyond hundred.

Here is the code snippet:

/* Iterate File */
List<String> lst = new ArrayList<>();
String line = null;
while((line = br.readLine()) != null) {
    if(lst.size() == 100) {
        lst.remove(0);
    }
    lst.add(line);
}
br.close(); 

/* Make Text */
StringBuilder sb = new StringBuilder();
for(String s : lst) {
    sb.append(s).append("\n");
}
text = sb.toString();

/* Clear ArrayList */
lst.clear();

As suggested in the accepted response to Read last n lines of a HUGE file you can estimate where to start reading from and start adding lines to a Guava cache which will start evicting older lines once it has 100.

Or you could use Apache ReversedLinesFileReader as suggested in another response to the same question

Or you could execute a shell command ('tail -n100') as described here . If what you really want is 'tail -f' consider using Apache Commons Tailer or Java 7+ file change notifications API

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM