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" ...
随机推荐
- 菜鸟Python学习笔记第一天:关于一些函数库的使用
2017年1月3日 星期二 大一学习一门新的计算机语言真的很难,有时候连函数拼写出错查错都能查半天,没办法,谁让我英语太渣. 关于计算机语言的学习我想还是从C语言学习开始为好,Python有很多语言的 ...
- 如何选择PHP框架?
PHP是世界上最受欢迎的编程语言之—.最近发布的PHP7令这种服务器的编程语言比以前变得更好,更稳定了. PHP被广泛应用于重大的项目.例如Facebook就是使用PHP来维护和创建它们的内部系统的. ...
- angular2系列教程(十一)路由嵌套、路由生命周期、matrix URL notation
今天我们要讲的是ng2的路由的第二部分,包括路由嵌套.路由生命周期等知识点. 例子 例子仍然是上节课的例子:
- JavaScript基础知识总结(一)
当我们接触一种新语言时,首先要先了解它,对它有一定的理论认识. 那么,什么是JavaScript呢? JavaScript是一种脚本语言,由web浏览器进行解释和执行.它包括ECMAScript.DO ...
- 一行代码实现java list去重
1.不带类型写法: 1 List listWithoutDup = new ArrayList(new HashSet(listWithDup)); 2.带类型写法(以String类型为例):1)Ja ...
- 装饰者模式 Decoration
1.什么是装饰者模式 动态给对象增加功能,从一个对象的外部来给对象添加功能,相当于改变了对象的外观,比用继承的方式更加的灵活.当使用装饰后,从外部系统的角度看,就不再是原来的那个对象了,而是使用一系列 ...
- 解决 Could not find com.android.tools.build:gradle 问题
今天拉同事最新的代码,编译时老是报如下错误: Error:Could not find com.android.tools.build:gradle:2.2.0.Searched in the fol ...
- java中的移位运算符:<<,>>,>>>总结
java中有三种移位运算符 << : 左移运算符,num << 1,相当于num乘以2 >> : 右移运算符,num >& ...
- 一条Sql语句分组排序并且限制显示的数据条数
如果我想得到这样一个结果集:分组排序,并且每组限定记录集的数量,用一条SQL语句能办到吗? 比如说,我想找出学生期末考试中,每科的前3名,并按成绩排序,只用一条SQL语句,该怎么写? 表[TScore ...
- mysql开启慢查询日志及查询--windows
MySQL慢查询配置 1. 慢查询有什么用? 它能记录下所有执行超过long_query_time时间的SQL语句, 帮你找到执行慢的SQL, 方便我们对这些SQL进行优化. 2. 如何开启慢查询? ...