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. Android 多线程----AsyncTask异步任务详解

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...

  2. Eclipse打开xml文件报校验错误解决办法

    XML文件在Eclipse中报校验错误: The content of element type "web-app" must match "(icon?,display ...

  3. js文本框提示和自动完成

    1.模仿大型网站自动提示,就是输入“苹果”,在水果类中搜索,html代码如下: <div id="searchTips" style="display:none;w ...

  4. 页的lock

    文件为什么要加锁? 页的操作为什么要加锁? http://linux.chinaunix.net/techdoc/system/2007/06/11/959844.shtml 上面一个页面有简单介绍什 ...

  5. 实战:ADFS3.0单点登录系列-总览

    本系列将以一个实际项目为背景,介绍如何使用ADFS3.0实现SSO.其中包括SharePoint,MVC,Exchange等应用程序的SSO集成. 整个系列将会由如下几个部分构成: 实战:ADFS3. ...

  6. WP老杨解迷:开发生态两极化和榜单乱象

    Windows Phone 自2013年的一片浪潮推动下,2014年终于开始引起了各大小CP们的注意,于是大量的产品开始乘风破浪一路涌进Windows Phone平台,立即改变了榜单的格局,如今,苦B ...

  7. wireshark排查打印机问题

    抓包工具排除故障 前言:上网慢,可能是内网堵了.装上wireshark,可抓到广播包,多播包,以及发给自己的包.如果想抓lan内其他人之间的通信包,那就要在sw上做端口镜像. 背景 调试打印机的人发现 ...

  8. Linux 网络编程八(epoll应用--大并发处理)

    //头文件 pub.h #ifndef _vsucess #define _vsucess #ifdef __cplusplus extern "C" { #endif //服务器 ...

  9. 解决SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问的方法

    1.开启Ad Hoc Distributed Queries组件,在sql查询编辑器中执行如下语句: reconfigure reconfigure 2.关闭Ad Hoc Distributed Qu ...

  10. cookie+memcached实现单点登陆

    10年的时候在iteye的第一篇文章记录了一下当时怎么实现我们系统的单点登陆.不过那个时候文章写的不好,思路也很浮躁,很难看懂,在csdn的第一篇技术博客打算重新温顾一下当时实现单点登陆的思路.先来看 ...