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 读取和保存文件(手机内置存储器)的更多相关文章

  1. JavaScript进阶(六)用JavaScript读取和保存文件

    用JavaScript读取和保存文件 因为Google还不提供同步插件数据的功能,所以导入和导出插件配置就必须和文件打交道了.而出于安全原因,只有IE才提供访问文件的API:但随着HTML 5的到来, ...

  2. Android学习笔记——保存文件(Saving Files)

              本人邮箱:JohnTsai.Work@gmail.com,欢迎交流讨论.                 欢迎转载,转载请注明网址:http://www.cnblogs.com/J ...

  3. android 读取SD卡文件

    SD卡作为手机的扩展存储设备,在手机中充当硬盘角色,可以让我们手机存放更多的数据以及多媒体等大体积文件.因此查看SD卡的内存就跟我们查看硬盘的剩余空间一样,是我们经常操作的一件事,那么在Android ...

  4. android 读取用户号码,手机串号,SIM卡序列号

    简介: IMSI:international mobiles subscriber identity国际移动用户号码标识,这个一般大家是不知道,GSM必须写在卡内相关文件中:MSISDN:mobile ...

  5. [Android] Android读取Asset下文件的最简单的方法总结(用于MediaPlayer中)

    方法一:getAssets().openFd //读取asset内容    private void openAssetMusic(String index) throws IOException { ...

  6. android 读取本地json文件 解决显示乱码显示

    1.读取本地JSON ,但是显示汉字乱码 public static String readLocalJson(Context context,  String fileName){         ...

  7. C# FileStream分块读取和保存文件

    一 FileStream分块读取文件 public byte[] GetFileData(string fileName, long startPosition, long length) { byt ...

  8. Android学习笔记-保存数据的实现方法1

    Android开发中,有时候我们需要对信息进行保存,那么今天就来介绍一下,保存文件到内存,以及SD卡的一些操作,及方法,供参考. 第一种,保存数据到内存中: //java开发中的保存数据的方式 pub ...

  9. 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)

    1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name= ...

随机推荐

  1. A - ACM Computer Factory - poj 3436(最大流)

    题意:有一个ACM工厂会生产一些电脑,在这个工厂里面有一些生产线,分别生产不同的零件,不过他们生产的电脑可能是一体机,所以只能一些零件加工后别的生产线才可以继续加工,比如产品A在生产线1号加工后继续前 ...

  2. SCGHR 系统设计

    1.表,弄清表的意义,表是系统的核心.

  3. Oracle 数据库基本操作——实用手册、表操作、事务操作、序列

    目录: 0. 参考链接与参考手册1. oracle 实用(常用操作)指令2. 数据库基本操作语法 a) 表操作 1)创建表 2)更新表 3)删除表 4)查询 b) 事务操作 c) 序列操作 1)创建序 ...

  4. (转)Maven实战(六)依赖

    我们项目中用到的jar包可以通过依赖的方式引入,构建项目的时候从Maven仓库下载即可. 1. 依赖配置    依赖可以声明如下: <project> ... <dependenci ...

  5. Ellipse常用快捷键

    Ctrl+m:视窗大小变化 Ctrl+F6:在打开的文件件进行切换 Ctrl+F7:在资源窗口间切换Ctrl+F8:在各种模式下进行切换 Ctrl+e:选择某个打开的文件Shift+home:整行选取 ...

  6. Android各种颜色dawable.xml中定义

    < drawable name="white">#FFFFFF< /drawable>< !--白 --> < drawable name ...

  7. quick-cocos2dx学习笔记

    20140603 下载quick,拉开拉链,跑setup.bat(注意,setup事就是在系统环境变量里加入QUICK_COCOS2DX_ROOT,假设不运行这个的话,启动player时会报找不到fr ...

  8. Injecting and Binding Objects to Spring MVC Controllers--转

    I have written two previous posts on how to inject custom, non Spring specific objects into the requ ...

  9. api接口

    目录(?)[-] 接口特点汇总 PHP Token令牌 先说第一个tokenapi_token 服务端接口校验PHP实现流程如下 再说第二个tokenuser_token 接口用例如下 接口特点汇总: ...

  10. css实现鼠标移上去变大,旋转,转别人的额

    <!doctype html><html><head> <meta charset="utf-8"> <title>CS ...