从内部存在中读文件
来源:互联网 发布:二氧化碳中毒 知乎 编辑:程序博客网 时间:2024/06/09 17:27
前面我们学习过了 往内部存储中写文件。
现在我们来读该文件,并且解析读取到的数据,显示到界面上。
/** * 从内部存储读取用户数据 */ public void readAccount(){ //要读取的文件 File f = new File("data/data/com.example.dev.readwritefile/info.txt"); try { FileInputStream fis = new FileInputStream(f); //把字节流转换成为字符串流 BufferedReader buffer = new BufferedReader(new InputStreamReader(fis)); //读取txt文件里的用户名和密码 String text = buffer.readLine(); String[] info = text.split(":"); //数据显示到界面上 EditText et_name = (EditText)findViewById(R.id.et_name); EditText et_pwd = (EditText)findViewById(R.id.et_pwd); et_name.setText(info[0]); et_pwd.setText(info[1]); }catch (Exception e){ e.printStackTrace(); } }
优化代码:
/** * 从内部存储读取用户数据 */ public void readAccount(){ //要读取的文件 File f = new File("data/data/com.example.dev.readwritefile/info.txt"); if (f.exists()){ //判断文件是否存在 try { FileInputStream fis = new FileInputStream(f); //把字节流转换成为字符串流 BufferedReader buffer = new BufferedReader(new InputStreamReader(fis)); //读取txt文件里的用户名和密码 String text = buffer.readLine(); String[] info = text.split(":"); //数据显示到界面上 EditText et_name = (EditText)findViewById(R.id.et_name); EditText et_pwd = (EditText)findViewById(R.id.et_pwd); et_name.setText(info[0]); et_pwd.setText(info[1]); }catch (Exception e){ e.printStackTrace(); } } }
0 0
- 从内部存在中读文件
- 从iframe中打开文件,并检测iframe存在否...
- 从手机内部存储器中获取相应文件
- 从路径中取目录/文件/创建目录/判断目录是否存在
- 文件中存在相同数据
- vb中检测文件是否存在
- sql中判断文件是否存在
- 向存在的文件中写入数据
- VC中判断文件是否存在
- Linux中判断文件是否存在
- 检测文件中是否存在bom
- QT中判断文件是否存在
- ubuntu中shell判断文件存在
- 检测文件中是否存在bom
- shell脚本中判断文件是否存在
- 【shell】判断文件中是否存在字符串
- windows查询文件中是否存在字符串
- 从Xml中取得数据存在ActiveReports中
- GridView九宫格
- 借势新一轮融资 优信二手车发展将大步向前
- Timus Online Judge:1100. Final Standings
- 龚祖春促进企业人文和谐时代之独特
- Spring 定时任务之 @Scheduled cron表达式
- 从内部存在中读文件
- 启动失败的日志提示通过more /var/lib/mysql/localhost.localdomain.err查看
- 【转】RPM GPG详解
- gdb 远程调试 方法
- Android架构思考(模块化、多进程)
- VS 2008 可扩展性开发(一):VS概览
- Golang 通关初级(3)
- 块存储,文件存储和对象存储
- 【C语言简单说】一:第一个C语言程序