android TextView显示容量很大的.txt文件

son123qaz123 2012-01-11 10:46:45
写了个阅读txt文件的小程序显示数据时候数据太多导致UI线程阻塞,界面卡住,有没有方法分段的读取数据并且在TextView显示啊求高手解决啊!
代码
public class TextActivity extends Activity {

private TextView text_title;
private DigitalClock clock;
private TextView myView;// 文本
private ImageView menu_button;// 菜单按钮
private ImageView close_button;// 取消按钮
private String textPath;// 获取文本文件的路径
private ProgressDialog progressDialog;
private StringBuffer stringBuffer;// 存放加载的数据
private static final int finsh_id = 1;
private Handler handler;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setTitle("Mini文件阅读器");
setContentView(R.layout.text_rw);
text_title = (TextView) findViewById(R.id.text_title);
clock = (DigitalClock) findViewById(R.id.clock);
myView = (TextView) findViewById(R.id.myView);

menu_button = (ImageView) findViewById(R.id.menu_button);
menu_button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(TextActivity.this,
MenuActivity.class);
startActivity(intent);
}
});
close_button = (ImageView) findViewById(R.id.close_button);
close_button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
TextActivity.this.finish();
}
});
textPath = getIntent().getExtras().getString("textPath");

String text_name = textPath.substring(textPath.lastIndexOf("/") + 1,
textPath.lastIndexOf("."));// 获得文件名称,不包含扩展名称
text_title.setText(text_name);

HandlerThread handlerThread = new HandlerThread("handlerThread");// 创建异步对象
handlerThread.start();
Handler threadHandler = new Handler(handlerThread.getLooper()) {

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
convertCodeAndGetText(textPath);// 加载数据并且转码
handler.sendEmptyMessage(finsh_id);
}

};
Message message = threadHandler.obtainMessage();
message.sendToTarget();// 启动异步对象

handler = new Handler() {

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == finsh_id) {
progressDialog.dismiss();
myView.setText(stringBuffer.toString());//显示数据
}

}

};

progressDialog = ProgressDialog.show(this, "请稍后...", "加载数据中...", false);
}

public void convertCodeAndGetText(String path) {// 转码

stringBuffer = new StringBuffer();

File file = new File(path);

BufferedReader reader;
try {
FileInputStream fis = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(fis);
in.mark(4);
byte[] first3bytes = new byte[3];
in.read(first3bytes);// 找到文档的前三个字节并自动判断文档类型。
in.reset();
if (first3bytes[0] == (byte) 0xEF && first3bytes[1] == (byte) 0xBB
&& first3bytes[2] == (byte) 0xBF) {// utf-8
reader = new BufferedReader(new InputStreamReader(in, "utf-8"));

} else if (first3bytes[0] == (byte) 0xFF
&& first3bytes[1] == (byte) 0xFE) {
reader = new BufferedReader(
new InputStreamReader(in, "unicode"));
} else if (first3bytes[0] == (byte) 0xFE
&& first3bytes[1] == (byte) 0xFF) {
reader = new BufferedReader(new InputStreamReader(in,
"utf-16be"));
} else if (first3bytes[0] == (byte) 0xFF
&& first3bytes[1] == (byte) 0xFF) {
reader = new BufferedReader(new InputStreamReader(in,
"utf-16le"));
} else {
reader = new BufferedReader(new InputStreamReader(in, "GBK"));
}

String str;

while ((str = reader.readLine()) != null) {
stringBuffer.append(str + "\n");
}
reader.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
protected void onDestroy() {
super.onDestroy();
}

}
...全文
811 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
木糖醇的爸爸 2012-01-28
  • 打赏
  • 举报
回复
阅读器的textView是不用全部把文本读进去的 一般情况下是预读当前页的后几页 然后翻一页读一页。。一下子读完完全就是很容易卡住死机的而且耗内存 具体算法自己实现。。。
bcf102 2012-01-21
  • 打赏
  • 举报
回复
RandomAccessFile
用这个读就行了,具体用法自己查
串联谐振双有源桥(SR-DAB)DC-DC变换器闭环仿真研究内容概要:本文围绕串联谐振双有源桥(SR-DAB)DC-DC变换器的闭环仿真展开研究,重点探讨其在电力电子系统中的动态响应特性与控制策略。通过对SR-DAB变换器建立数学模型,并结合Matlab/Simulink进行闭环仿真,分析系统在不同工作条件下的电压、电流波形及功率传输效率,验证控制算法的有效性与稳定性。研究涵盖系统建模、控制器设计(如PI控制)、软开关实现条件、抗干扰能力以及动态调节性能,旨在提升变换器在新能源、储能、数据中心供电等场景中的可靠性和能效水平。; 适合人群:具备电力电子、自动控制理论基础,从事新能源、微电网、电力系统仿真等相关领域的科研人员与工程技术人员。; 使用场景及目标:①用于高校及科研机构开展DC-DC变换器控制策略的教学与实验;②为工业界在高效率隔离型功率变换系统的设计与优化提供仿真依据和技术支撑;③服务于数据中心、电动汽车、可再生能源并网等场景下的电源系统研发。; 阅读建议:建议读者结合Matlab代码实践仿真流程,重点关注系统建模与控制器参数整定部分,通过调整负载、输入电压等条件观察系统响应,深入理解闭环控制对变换器性能的影响机制。
内容概要:本文介绍了FastReport VCL/FMX/LCL 2026.2.4版本的更新内容,重点涵盖核心引擎、图形处理、用户界面及导出功能的优化与修复。主要更新包括修复了Developer Express兼容性问题、TfrxSVGGraphic在TImage中的显示问题、PDF/A导出合规性、EMF图像旋转异常,以及ReportTree拖拽功能等问题。同时,部分模块新增了HTMLTags对XLSX导出的支持、内置右键菜单和新图标,并改进了FastCube组件的元素调整体验。此外,还修复了CPP脚本中Result参数名的Bug以及自定义过滤器中OR操作失效的问题。; 适合人群:使用Delphi或Lazarus进行开发,且涉及报表设计与数据展示功能的中高级开发者;尤其适用于需要集成FastReport控件到企业级应用中的技术人员。; 使用场景及目标:①提升报表系统在不同平台(VCL/FMX/LCL)下的稳定性和兼容性;②满足PDF/A标准的文档归档需求;③优化用户交互体验,如拖拽操作与元素缩放;④增强脚本支持与数据集字段识别能力,确保开发效率与功能完整性。; 阅读建议:建议结合官方更新链接和实际项目中使用的FastReport模块对照查看,重点关注自身所用组件(如VCL、FMX或Lazarus版)的具体修复项,及时升级以规避已知问题并利用新特性优化报表功能。

80,490

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧