如何从服务器读取文本文件并仅显示例如文本视图中的最后 100 行
How to read text file from server and display only e.g. 100 last lines of it in textview
下面的代码来自我的 android 应用程序。目的是从服务器读取文本文件并在文本视图中显示最后 100 行。显然,只有当文本文件中的行数为 2000 时,下面的代码才能正常工作。
我能想到的是,我首先需要遍历所有行来计算它们的数量,然后再次遍历以在 textview 中显示最后 100 行。我试过嵌套的 BufferedReaders 但没有 succeeded.Any 想法?
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);
}
}
}
一种解决方案是将所有内容添加到 ArrayList
,然后从最后一百条记录中提取文本。要改进功能,您可以在计数超过一百后开始从顶部删除行。
这是代码片段:
/* 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();
如已接受的对 Read last n lines of a HUGE file 的回复中所建议,您可以估计从何处开始读取并开始向 Guava 缓存添加行,一旦达到 100 行就会开始驱逐旧行。
或者您可以按照对同一问题的另一个回复中的建议使用 Apache ReversedLinesFileReader
或者您可以执行 shell 命令 ('tail -n100') 作为 described here. If what you really want is 'tail -f' consider using Apache Commons Tailer 或 Java 7+ 文件更改通知 API
HTH
下面的代码来自我的 android 应用程序。目的是从服务器读取文本文件并在文本视图中显示最后 100 行。显然,只有当文本文件中的行数为 2000 时,下面的代码才能正常工作。
我能想到的是,我首先需要遍历所有行来计算它们的数量,然后再次遍历以在 textview 中显示最后 100 行。我试过嵌套的 BufferedReaders 但没有 succeeded.Any 想法?
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);
}
}
}
一种解决方案是将所有内容添加到 ArrayList
,然后从最后一百条记录中提取文本。要改进功能,您可以在计数超过一百后开始从顶部删除行。
这是代码片段:
/* 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();
如已接受的对 Read last n lines of a HUGE file 的回复中所建议,您可以估计从何处开始读取并开始向 Guava 缓存添加行,一旦达到 100 行就会开始驱逐旧行。
或者您可以按照对同一问题的另一个回复中的建议使用 Apache ReversedLinesFileReader
或者您可以执行 shell 命令 ('tail -n100') 作为 described here. If what you really want is 'tail -f' consider using Apache Commons Tailer 或 Java 7+ 文件更改通知 API
HTH