Android开发中会用到文件存储,今天来学习下。

先改下布局界面:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/filename"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:textSize="15dp"
android:text="文件名称" /> <EditText
android:id="@+id/edit_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> <TextView
android:id="@+id/filecontent"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:textSize="15dp"
android:text="文件内容" /> <EditText
android:id="@+id/edit_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <Button
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存文件" /> <Button
android:id="@+id/btn_read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取文件" />
</LinearLayout> </LinearLayout>

再改下MainActivity文件:

 package com.example.filesave;

 import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream; import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; public class MainActivity extends Activity { private Context context = this;
private Button btnsave = null;
private Button btnread = null;
private EditText editname = null;
private EditText editcontent = 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);
editname = (EditText) findViewById(R.id.edit_name);
editcontent = (EditText) findViewById(R.id.edit_content); btnsave.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) { String filename = editname.getText().toString();
String filecontent = editcontent.getText().toString();
FileOutputStream out = null;
try {
out = context
.openFileOutput(filename, Context.MODE_PRIVATE);
out.write(filecontent.getBytes("UTF-8")); editname.setText("已经保存名称!");
editcontent.setText("已经保存内容!"); } catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}); btnread.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
String filename = editname.getText().toString(); // 获得读取的文件的名称
FileInputStream in = null;
ByteArrayOutputStream bout = null;
byte[] buf = new byte[1024];
bout = new ByteArrayOutputStream();
int length = 0;
try {
in = context.openFileInput(filename); // 获得输入流
while ((length = in.read(buf)) != -1) {
bout.write(buf, 0, length);
}
byte[] content = bout.toByteArray();
editcontent.setText(new String(content, "UTF-8")); // 设置文本框为读取的内容
} catch (Exception e) {
e.printStackTrace();
}
editcontent.invalidate(); // 刷新屏幕
try {
in.close();
bout.close();
} catch (Exception e) {
}
} }); } }

运行效果:

分别输入文件名称和文件内容为“aaaa"、”1234“,如图:

点击保存文件后,界面变为:

然后再修改文件名称为”aaaa",如图:

最后点击读取文件按钮,文件内容显示为”1234”,测试成功。如图:

提示:创建的存储文件保存在/data/data/<package name>/files文件夹下,那么我们怎样才能看到这文件呢?首先连接手机处于调试模式,进入adb shell命令行界面, 输入“su”命令回车,如图

然后再输入:cd  data/data 如图:

再输入ls 命令,找到自己创建工程包名文件就可以了,比如我的工程是“com.example.filesave”,那就cd  com.example.filesave即可,如图:

最后找到files就可看到文件了。

19.Android之文件存储方法学习的更多相关文章

  1. Android File文件存储功能

    1.介绍 2.使用方法 3.文件存储位置 4.java后台代码 package com.lucky.test47file; import android.support.v7.app.AppCompa ...

  2. Android使用文件存储数据

    Android上最基本的存储数据的方式即为使用文件存储数据,使用基本的Java的FileOutStream,BufferedWriter,FileInputStream和BufferedReader即 ...

  3. android 开发-文件存储之读写sdcard

    android提供对可移除的外部存储进行文件存储.在对外部sdcard进行调用的时候首先要调用Environment.getExternalStorageState()检查sdcard的可用状态.通过 ...

  4. android之文件存储和读取

    一.权限问题 手机中存储空间分为ROM和SDcard,ROM中放着操作系统以及我们安装的APP,而sdcard中一般放置着我们APP产生的数据.当然,Android也为每个APP在ROM中创建一个数据 ...

  5. Android的文件存储

    //文件的写入 String content1 = edt_file.getText().toString(); //用于文件的写操作 FileOutputStream fos=null; //缓冲输 ...

  6. Android - 读取文件存储的数据

    存取手机中的文件数据. 写入和读取的操作格式均为UTF-8. import java.io.File; import java.io.FileInputStream; import java.io.F ...

  7. Android资源文件命名规范学习手册

    [推荐] 资源文件需带模块前缀.[推荐] layout 文件的命名方式. Activity 的 layout 以 module_activity 开头 Fragment 的 layout 以 modu ...

  8. Android数据存储之Android 6.0运行时权限下文件存储的思考

    前言: 在我们做App开发的过程中基本上都会用到文件存储,所以文件存储对于我们来说是相当熟悉了,不过自从Android 6.0发布之后,基于运行时权限机制访问外置sdcard是需要动态申请权限,所以以 ...

  9. android 开发-数据存储之文件存储

    android的文件存储是通过android的文件系统对数据进行临时的保存操作,并不是持久化数据,例如网络上下载某些图片.音频.视频文件等.如缓存文件将会在清理应用缓存的时候被清除,或者是应用卸载的时 ...

随机推荐

  1. TestLink学习六:TestLink1.9.13工作使用小结

    Testlink是一款强大的用例追踪和管理工具.测试管理注重的实际上就是一个流程. 1.默认当测试用例同名时,就会有提示.(以前版本需要修改配置) 2.测试用例序号:(缺点) 1)删除一个测试用例之后 ...

  2. Android的Style的使用

    Style个人理解就是view的一些属性的集合,那么一系列view(例如TextVIew),只要是要该style那么就都有相同的内容,如 文字的大少,颜色等,方便修改 首先最基本的使用,多个textV ...

  3. 转 异常处理汇总 ~ 修正果带着你的Net飞奔吧!

    异常处理汇总 ~ 修正果带着你的Net飞奔吧!   异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 异常处理汇总-开发工具  h ...

  4. mysql数据库误删除后的数据恢复操作说明

    在日常运维工作中,对于mysql数据库的备份是至关重要的!数据库对于网站的重要性使得我们对mysql数据的管理不容有失!然后,是人总难免会犯错误,说不定哪天大脑短路了来个误操作把数据库给删除了,怎么办 ...

  5. OXM

    O/X Mapper 是什么? Spring 3.0 的一个新特性是 O/X Mapper.O/X 映射器这个概念并不新鲜,O 代表 Object,X 代表 XML.它的目的是在 Java 对象(几乎 ...

  6. 025医疗项目-模块二:药品目录的导入导出-HSSF导入类的封装

    上一篇文章提过,HSSF的用户模式会导致读取海量数据时很慢,所以我们采用的是事件驱动模式.这个模式类似于xml的sax解析.需要实现一个接口,HSSFListener接口. 原理:根据excel底层存 ...

  7. [Elixir008]Nested Module里的动态函数调用方式

    有时我们需要动态生成一些模块名,然后调用它里面的函数.但是我们常常碰到的却是明明有那个模块,结果还是raise模块未定义... 我们来看看到底怎么回事? 首先我们定义一个函数 iex(1)> d ...

  8. 基于EventAggregator的事件发布及订阅

    EventAggregator简介 EventAggregator是Prism中专门处理ViewModel与ViewModel之间事件传递的类对象,它提供了针对事件的发布方法和订阅方法,所以可以非常方 ...

  9. ace布置小作业: 制作一个简单的电话号码归属地查询软件:JSON解析和Volly发送get请求

    大概就这个样子 用到JSON解析和Volly发送Get请求两个知识点 关于Volly的用法请看我的这篇: http://www.cnblogs.com/AceIsSunshineRain/p/5177 ...

  10. Java系列: 我的第一个spring aop练习

    看<Spring in action>有一段时间了,陆续也都看懂了,但是看懂和自己动手写确实是两回事,今天花了几个小时陆续开始安装spring,开始使用DI,然后使用AOP,在写AOP例子 ...