Android 学习笔记 文本文件的读写操作
2024-10-18 08:14:45
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <Button
android:id="@+id/btn01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="保存" />
<Button
android:id="@+id/btn02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="读取" /> <TextView
android:id="@+id/msg"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private static final String FILENAME="Test01.txt";
private Button btn01=null;
private Button btn02=null;
private TextView msg=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
this.btn01=(Button)super.findViewById(R.id.btn01);
this.btn02=(Button)super.findViewById(R.id.btn02);
this.btn01.setOnClickListener(new OnClickListenerImpl());
this.btn02.setOnClickListener(new OnClickListenerImpl());
this.msg=(TextView)super.findViewById(R.id.msg);
}
private class OnClickListenerImpl implements OnClickListener{ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn01:
FileOutputStream output=null;
try {
output=MainActivity.this.openFileOutput(FILENAME, Activity.MODE_PRIVATE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
PrintStream out=new PrintStream(output);
out.println(sdf.format(new Date()));
out.println("文本内容aa");
out.close();
Toast.makeText(MainActivity.this, "文件已经保存", Toast.LENGTH_SHORT).show();
break;
case R.id.btn02:
FileInputStream input=null;
try {
input=MainActivity.this.openFileInput(FILENAME);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Scanner scanner=new Scanner(input);
MainActivity.this.msg.setText("");
MainActivity.this.msg.append("-------Start--------\n");
while (scanner.hasNext()) {
MainActivity.this.msg.append(scanner.next()+"\n");
}
MainActivity.this.msg.append("-------End--------\n");
Toast.makeText(MainActivity.this, "文件已经读取", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
如果需要操作SD卡上的文件,需要在AndroidManifest.xml中添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
最新文章
- JSP实现word文档的上传,在线预览,下载
- 数据结构 B-树和B+树的应用:数据搜索和数据库索引
- Linux 动态链接库
- 一次简单的MySQL数据库导入备份
- SQL SERVER 2012使用sequence
- Java安全编码之用户输入
- HDU题目分类
- threaded模式下,比prefork模式要省资源
- Linux ed
- MySQL的安装——源码方式(实验环境下测试用,真实环境请忽略此文)
- 392. Is Subsequence
- 谈谈托管代码、IL、CLR、ISAPI?
- Red Hat Enterprise Linux Server(RHEL) yum安装软件时This system is not registered with RHN. RHN support will be disabled. 的解决方法(转)
- python 循环使用 while 或 for 语句实现用户名密码输错三次退出
- Python 学习系列----第一章:基础知识
- PoolEntry 参数讲解
- asyncio并发编程
- oracle随机数(转)
- redis学习-有序集合(zset)常用命令
- Clever Little Box 电缆组件 USB A 插座 至 USB B 插头
热门文章
- 《PYTHON学习手册》(《Learing Python》--Mark Lutz)书摘
- 各种Java项目环境搭建-文档引用汇总记录
- python学习(三) 使用字符串
- C#多线程List的非线程安全性
- Linux - rpm 软件包管理
- c#中{set;get;}使用逻辑
- angular结合echarts创建图表
- Ubuntu下&;nbsp;Nfs服务器安装
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 5 Octave Tutorial—5.6 向量化 Vectorization
- java基础知识(三)之数组