java - 如何从最后一行到第一行读取文本文件

标签 java android

我需要它反向运行,以便各行以与此相反的顺序显示。有没有一种快速的方法来翻转 TextView 或类似的东西?感谢您的帮助。

try {
// Create a URL for the desired page
URL url = new URL("text.txt");

// Read all the text returned by the server
BufferedReader MyBr = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
line = MyBr.readLine();
TextView textview1 = (TextView) findViewById(R.id.textView1);

StringBuilder total = new StringBuilder();

while ((line = MyBr.readLine()) != null) {
total.append(line + "\r\n");}

textview1.setText(total);
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/wonderfull.ttf");
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setTypeface(tf);
MyBr.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}

最佳答案

由于您要更改代码中的 UI,因此必须在 UI 线程上运行。当然,您知道在 UI 线程上执行这样的 I/O 是不允许的,并且很容易创建 ANR。所以我们假设这是伪代码。最好的选择是在阅读流时逆转事情。你可以insert at the front of the StringBuilder例如。

关于java - 如何从最后一行到第一行读取文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7617443/

相关文章:

java - Android HandlerThread 在没有 UiHandler 的情况下更新 UI

android - 如何在 android 惰性列表应用程序中使用字节图像 [] 数组而不是字符串字符串 [] 数组

java - 改造 - android.os.NetworkOnMainThreadException

java - DataSnapshot.getValue() 未正确检索对象

android - 如何部分删除 JSON 在 android 上使用 intent putExtra

java - 确定 java 应用程序是否在 Eclipse 中处于 Debug模式

java - 无法启动tomcat 7 : Caused by: java. lang.NoClassDefFoundError

java - 为什么求解方程时 URL 结果循环会出现意外情况

java - 如何使用java获取网络上的所有IP地址?

java - 如何在 Eclipse 中拖动按钮并自动生成 onclick 方法?如何在 Eclipse 中查找变量/方法引用?