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"/>

最新文章

  1. JSP实现word文档的上传,在线预览,下载
  2. 数据结构 B-树和B+树的应用:数据搜索和数据库索引
  3. Linux 动态链接库
  4. 一次简单的MySQL数据库导入备份
  5. SQL SERVER 2012使用sequence
  6. Java安全编码之用户输入
  7. HDU题目分类
  8. threaded模式下,比prefork模式要省资源
  9. Linux ed
  10. MySQL的安装——源码方式(实验环境下测试用,真实环境请忽略此文)
  11. 392. Is Subsequence
  12. 谈谈托管代码、IL、CLR、ISAPI?
  13. Red Hat Enterprise Linux Server(RHEL) yum安装软件时This system is not registered with RHN. RHN support will be disabled. 的解决方法(转)
  14. python 循环使用 while 或 for 语句实现用户名密码输错三次退出
  15. Python 学习系列----第一章:基础知识
  16. PoolEntry 参数讲解
  17. asyncio并发编程
  18. oracle随机数(转)
  19. redis学习-有序集合(zset)常用命令
  20. Clever Little Box 电缆组件 USB A 插座 至 USB B 插头

热门文章

  1. 《PYTHON学习手册》(《Learing Python》--Mark Lutz)书摘
  2. 各种Java项目环境搭建-文档引用汇总记录
  3. python学习(三) 使用字符串
  4. C#多线程List的非线程安全性
  5. Linux - rpm 软件包管理
  6. c#中{set;get;}使用逻辑
  7. angular结合echarts创建图表
  8. Ubuntu下&amp;nbsp;Nfs服务器安装
  9. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 5 Octave Tutorial—5.6 向量化 Vectorization
  10. java基础知识(三)之数组