Android 读取和保存文件(手机内置存储器)
1:activity_main.xml
<RelativeLayout 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">
<TextView
android:id="@+id/tv_filename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文件名称"/>
<EditText
android:id="@+id/et_filename"
android:layout_below="@id/tv_filename"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> <TextView
android:id="@+id/tv_content"
android:layout_below="@id/et_filename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文件内容"/>
<EditText
android:id="@+id/et_content"
android:layout_below="@id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> <Button
android:id="@+id/btn_save"
android:layout_below="@id/et_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存"/> <Button
android:id="@+id/btn_read"
android:layout_below="@id/btn_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取"/>
</RelativeLayout>
2:文件操作类:FileService.java
public class FileService {
private Context context=null; public FileService(Context context){
this.context=context;
} //save file
public void saveFile(String filename,String content) throws Exception{
FileOutputStream out=context.openFileOutput(filename, Context.MODE_PRIVATE);
out.write(content.getBytes());
out.close();
} //read file
public String readFile(String filename)throws Exception{
FileInputStream is=context.openFileInput(filename);
byte b[]=new byte[1024];
int len=0;
ByteArrayOutputStream baos=new ByteArrayOutputStream();
//先把数据写入内存
while((len=is.read(b))!=-1){
baos.write(b,0,len);
}
//从内存中读取数据
byte data[]=baos.toByteArray(); baos.close();
is.close(); return new String(data);
}
}
3:MainActivity.java
public class MainActivity extends Activity {
private FileService fileService=null;
private Button btnSave=null,btnRead=null;
private EditText etFilename=null;
private EditText etContent=null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); btnSave=(Button)findViewById(R.id.btn_save);
btnRead=(Button)findViewById(R.id.btn_read);
etFilename=(EditText)findViewById(R.id.et_filename);
etContent=(EditText)findViewById(R.id.et_content); fileService=new FileService(MainActivity.this); btnSave.setOnClickListener(new OnClickListener(){
public void onClick(View view){
String filename=etFilename.getText().toString();
String content=etContent.getText().toString();
try {
fileService.saveFile(filename, content);
Toast.makeText(MainActivity.this, "Save file success!", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "Save file error!", Toast.LENGTH_SHORT).show();
}
}
}); btnRead.setOnClickListener(new OnClickListener(){
public void onClick(View view){
String filename=etFilename.getText().toString();
try {
String text=fileService.readFile(filename);
etContent.setText(text);
} catch (Exception e) {
Toast.makeText(MainActivity.this, "Read file error!", Toast.LENGTH_SHORT).show();
}
}
});
} }
4:运行结果
Android 读取和保存文件(手机内置存储器)的更多相关文章
- JavaScript进阶(六)用JavaScript读取和保存文件
用JavaScript读取和保存文件 因为Google还不提供同步插件数据的功能,所以导入和导出插件配置就必须和文件打交道了.而出于安全原因,只有IE才提供访问文件的API:但随着HTML 5的到来, ...
- Android学习笔记——保存文件(Saving Files)
本人邮箱:JohnTsai.Work@gmail.com,欢迎交流讨论. 欢迎转载,转载请注明网址:http://www.cnblogs.com/J ...
- android 读取SD卡文件
SD卡作为手机的扩展存储设备,在手机中充当硬盘角色,可以让我们手机存放更多的数据以及多媒体等大体积文件.因此查看SD卡的内存就跟我们查看硬盘的剩余空间一样,是我们经常操作的一件事,那么在Android ...
- android 读取用户号码,手机串号,SIM卡序列号
简介: IMSI:international mobiles subscriber identity国际移动用户号码标识,这个一般大家是不知道,GSM必须写在卡内相关文件中:MSISDN:mobile ...
- [Android] Android读取Asset下文件的最简单的方法总结(用于MediaPlayer中)
方法一:getAssets().openFd //读取asset内容 private void openAssetMusic(String index) throws IOException { ...
- android 读取本地json文件 解决显示乱码显示
1.读取本地JSON ,但是显示汉字乱码 public static String readLocalJson(Context context, String fileName){ ...
- C# FileStream分块读取和保存文件
一 FileStream分块读取文件 public byte[] GetFileData(string fileName, long startPosition, long length) { byt ...
- Android学习笔记-保存数据的实现方法1
Android开发中,有时候我们需要对信息进行保存,那么今天就来介绍一下,保存文件到内存,以及SD卡的一些操作,及方法,供参考. 第一种,保存数据到内存中: //java开发中的保存数据的方式 pub ...
- 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)
1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name= ...
随机推荐
- win7虚拟机起不来,报错transport vmdb error -44 message the vmware authorization
运行: services.msc 选择:VMware Authorization Service,运行它
- 判断客户端浏览器是否安装了Flash插件
<script> /*检测浏览器是否安装了插件(在IE 中无效)*/ function hasPlugin(name){ name = name.toLowerCase(); for(va ...
- python 2017.1.9
python对缩进和空格要求非常严格,要求非常对齐 print时不同字符串之间会自动加上空格 while 和 if 等没有结束标记,全部通过对齐的方式表示的
- web前端面试试题总结---css篇
CSS 介绍一下标准的CSS的盒子模型?低版本IE的盒子模型有什么不同的? (1)有两种, IE 盒子模型.W3C 盒子模型: (2)盒模型: 内容(content).填充(padding).边界(m ...
- 最长回文子串(百度笔试题和hdu 3068)
版权所有.所有权利保留. 欢迎转载,转载时请注明出处: http://blog.csdn.net/xiaofei_it/article/details/17123559 求一个字符串的最长回文子串.注 ...
- "解密"微信开放高级接口 企业如何应对
今天(2013年10月29日)腾讯终于对外公开了微信公众平台最新的接口,一石激起千层浪,对于很多微信公众平台的运营人员来说,今天是令人兴奋的一天!微信在向申请服务号的企业开发了大量接口.用户不想输入文 ...
- <Win32_8>由浅入深——滚动条
滚动条在Win32程序中是非常常见的一个控件,它的功能和地位也就不言而喻了,在文本输出中算是一个难点…… 我将借用P先生的思路讲述两种不同风格滚动条,下面切入主题:(实例程序都是显示一张位图 当然, ...
- 【酷Q插件制作】教大家做一个简单的签到插件
酷Q插件已经有很多了,社区分享一大堆,不过还是自己写才有乐趣,哈哈.不得不吐槽一下,酷Q竟然不更新了,出了个酷Q pro,还收费!!诶.不过这也影响不了咱写插件的心情,今天教大家写一个酷Q签到插件,虽 ...
- 比较好的总结runtime
http://www.cocoachina.com/ios/20160128/15154.html
- 【转】iOS实时卡顿监控
转自http://www.tanhao.me/code/151113.html/ 在移动设备上开发软件,性能一直是我们最为关心的话题之一,我们作为程序员除了需要努力提高代码质量之外,及时发现和监控软件 ...