Android之文件数据存储
一、文件保存数据介绍
Activity提供了openFileOutput()方法可以用于把数据输出到文件中,具体的实现过程与在J2SE环境中保存数据到文件中是一样的。文件可用来存放大量数据,如文本、图书、音频等。
File对象适合按照从开始到结束的顺序不跳过地读取或写入大量数据。例如,它适合于图片文件或通过网络交换的任何内容。
数据存储的默认位置:/data/data/<包名>/files/***.***。
所有的Android设备都有两个文件存储区域:“内部”和“外部”存储。这篇文章主要是将数据存储,所以在“内部”存储区域存储文件。
二、使用方法
1. 向文件写入内容
try {
FileOutputStream fos = mContext.openFileOutput(mFileName,Context.MODE_PRIVATE);
fos.write(info.getBytes());
fos.close();
}catch (Exception e){
e.printStackTrace();
}
openFileOutput()方法的第一个参数用于指定文件名称,不能包含路径分割符"/",如果文件不存在,Android会自动创建它,openFileOutput()方法的第二个参数用于指定操作模式。
操作模式有:
2. 读入文件内容
try {
FileInputStream fis = mContext.openFileInput(mFileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
String info = reader.readLine();
fis.close();
return info;
}catch (Exception e){
e.printStackTrace();
}
三、小案例
1.添加strings.xml文件
<string name="write_data">写入数据</string>
<string name="read_data">读取数据</string>
<string name="file">File</string>
2.修改activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
tools:context="com.zhangmiao.datastoragedemo.MainActivity"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/file" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/fab_margin"
android:layout_marginTop="@dimen/fab_margin"
android:orientation="horizontal"> <Button
android:id="@+id/file_write"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/write_data" /> <Button
android:id="@+id/file_read"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/read_data" /> </LinearLayout> <TextView
android:id="@+id/table_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
3.添加FileDBManager类
package com.zhangmiao.datastoragedemo; import android.content.Context;
import android.os.Environment;
import android.util.Log; import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream; /**
* Created by zhangmiao on 2016/12/20.
*/
public class FileDBManager { private File mFile; private Context mContext; private String mFileName = "myfile"; public FileDBManager(Context context){
mContext = context;
} public void write(String info){
try {
FileOutputStream fos = mContext.openFileOutput(mFileName,Context.MODE_PRIVATE);
fos.write(info.getBytes());
fos.close();
}catch (Exception e){
e.printStackTrace();
}
} public String read(){
try {
FileInputStream fis = mContext.openFileInput(mFileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
String info = reader.readLine();
fis.close();
return info;
}catch (Exception e){
e.printStackTrace();
}
return "";
}
}
4.修改MainActivity
package com.zhangmiao.datastoragedemo; import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener {private FileDBManager mFileManager;private TextView mTableInfo; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mFileManager = new FileDBManager(this); mTableInfo = (TextView) findViewById(R.id.table_info); fileWrite.setOnClickListener(this);
fileRead.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.file_write:
mFileManager.write("hello world!");
break;
case R.id.file_read:
mTableInfo.setText(mFileManager.read());
break;default:break;
}
}
}
代码下载地址https://github.com/ZhangMiao147/DataStorageDemo
Android之文件数据存储的更多相关文章
- Android中的数据存储(二):文件存储 2017-05-25 08:16 35人阅读 评论(0) 收藏
文件存储 这是本人(菜鸟)学习android数据存储时接触的有关文件存储的知识以及本人自己写的简单地demo,为初学者学习和使用文件存储提供一些帮助.. 如果有需要查看SharedPreference ...
- Android下的数据存储与訪问 --- 以文件的形式
Android下的数据存储与訪问 --- 以文件的形式 1.1 储存文件存放在手机内存中: // *** 储存数据到 /data/data/包名/files/jxn.txt文件里 String dat ...
- 67.Android中的数据存储总结
转载:http://mp.weixin.qq.com/s?__biz=MzIzMjE1Njg4Mw==&mid=2650117688&idx=1&sn=d6c73f9f04d0 ...
- Android Learning:数据存储方案归纳与总结
前言 最近在学习<第一行android代码>和<疯狂android讲义>,我的感触是Android应用的本质其实就是数据的处理,包括数据的接收,存储,处理以及显示,我想针对这几 ...
- Android中的数据存储
Android中的数据存储主要分为三种基本方法: 1.利用shared preferences存储一些轻量级的键值对数据. 2.传统文件系统. 3.利用SQLite的数据库管理系统. 对SharedP ...
- Android五种数据存储方式
android 五种数据存储 :SharePreferences.SQLite.Contert Provider.File.网络存储 Android系统提供了四种存储数据方式.分别为:SharePre ...
- Android——几种数据存储应用浅谈
(1)android中的数据存储主要有五种方式: 第一种.sharedPreferences存储数据, 适用范围:保存少量的数据,且这些数据的格式非常简单:字符串型.基本类型的值.比如应用程序的各种配 ...
- <Android基础> (六) 数据存储 Part 1 文件存储方式
第六章 数据存储 6.1 持久化技术 持久化技术指将内存中的瞬时数据保存到存储设备中,保证即使在手机或电脑关机的情况下,这些数据仍然不会丢失. 主要有三种方式用于简单地实现数据持久化功能:文件存储.S ...
- android开发 解析服务器端xml文件数据存储到android客户端SQLite数据库
以下面xml文件为例对其解析(假设此xml就在服务器端Server项目下的servlet包下的MenuServlet文件的输出流中): <?xml version="1.0" ...
随机推荐
- ASP.NET Core 中的那些认证中间件及一些重要知识点
前言 在读这篇文章之间,建议先看一下我的 ASP.NET Core 之 Identity 入门系列(一,二,三)奠定一下基础. 有关于 Authentication 的知识太广,所以本篇介绍几个在 A ...
- 虾扯蛋:Android View动画 Animation不完全解析
本文结合一些周知的概念和源码片段,对View动画的工作原理进行挖掘和分析.以下不是对源码一丝不苟的分析过程,只是以搞清楚Animation的执行过程.如何被周期性调用为目标粗略分析下相关方法的执行细节 ...
- 【探索】机器指令翻译成 JavaScript
前言 前些时候研究脚本混淆时,打算先学一些「程序流程」相关的概念.为了不因太枯燥而放弃,决定想一个有趣的案例,可以边探索边学. 于是想了一个话题:尝试将机器指令 1:1 翻译 成 JavaScript ...
- 菜鸟学Struts2——零配置(Convention )
又是周末,继续Struts2的学习,之前学习了,Struts的原理,Actions以及Results,今天对对Struts的Convention Plugin进行学习,如下图: Struts Conv ...
- 异步 HttpContext.Current 为空null 另一种解决方法
1.场景 在导入通讯录过程中,把导入的失败.成功的号码数进行统计,然后保存到session中,客户端通过轮询显示状态. 在实现过程中,使用的async调用方法,出现HttpContext.Curren ...
- asp.net mvc 验证码
效果图 验证码类 namespace QJW.VerifyCode { //用法: //public FileContentResult CreateValidate() //{ // Validat ...
- YII 2.x 模板文件的 beginBlock、beginContent、beginCache
echo '-----------beginBlock--------------------- <br />'; $this->beginBlock('block1', false ...
- maven依赖查询地址
http://search.maven.org/#search%7Cga%7C1%7C
- GOF23设计模式之单例模式
·核心作用: -保证一个类只有一个实例,并且提供一个访问该实例的全局访问点. ·常见应用场景: -Windows的Task Manager(任务管理器)就是很典型的单例模式 -Windows的Recy ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...