热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Android学习笔记之数据的内部存储方式实习数据的读写

(1)目录结构(2)布局文件:


(1)目录结构

技术分享

(2) 布局文件:



    

    

(3)保存数据、读取数据的工具类:FileService.java

package com.example.data_storage_interal.file;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
/**
 * 使用内部存储的方式,可以使应用卸载的时候清除所有的相关信息
 * @author piaodangdehun
 *
 */
public class FileService {

	private Context context;

	public FileService(Context context) {
		this.cOntext= context;
	}

	/**
	 * 保存内容到文件中
	 * @param fileName 文件名
	 * @param mode 模式
	 * @param data 数据的缓冲区
	 * @return 返回真假值
	 */
	public boolean saveContentToFile(String fileName, int mode, byte[] data) {
		boolean flag = false;
		FileOutputStream outputStream = null;
		try {
			outputStream = context.openFileOutput(fileName, mode);
			try {
				outputStream.write(data, 0, data.length);
			} catch (IOException e) {
				e.printStackTrace();
			}
			flag = true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (Exception e2) {
				}
			}
		}
		return true;
	}

	/**
	 * 读取数据
	 * @param fileName
	 * @return
	 */
	public String readContentFromFile(String fileName) {
		String result = "";
		FileInputStream fileInputStream = null;
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		try {
			fileInputStream = context.openFileInput(fileName);
			int len = 0;
			byte[] data = new byte[1024];
			while ((len = fileInputStream.read(data)) != -1) {
				outputStream.write(data, 0, len);
			}
			return new String(outputStream.toByteArray());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
}

(4)MainActivity.java

package com.example.data_storage_interal;

import com.example.data_storage_interal.file.FileService;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Button button;
	private EditText editText;
	private FileService fileService;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button = (Button) this.findViewById(R.id.button1);
		editText = (EditText) this.findViewById(R.id.editText1);

		fileService = new FileService(this);
		/*
		 * 按钮点击的时候存放到文件中
		 */
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String valueString = editText.getText().toString().trim();
				boolean flag = fileService.saveContentToFile("bb.txt",
						Context.MODE_APPEND, valueString.getBytes());
				if (flag) {
					Toast.makeText(MainActivity.this, "保存成功!",
							Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

(5)测试类(要在资源文件中添加相应的信息、这里不说)

package com.example.data_storage_interal;

import com.example.data_storage_interal.file.FileService;

import android.app.Service;
import android.content.Context;
import android.test.AndroidTestCase;
import android.util.Log;

public class MyTest extends AndroidTestCase {
	private static final String TAG = "MyTest";

	/*
	 * 保存数据
	 */
	public void save() {
		FileService fileService = new FileService(getContext());
		boolean flag = fileService.saveContentToFile("aa.txt",
				Context.MODE_PRIVATE + Context.MODE_APPEND, "nihao".getBytes());
		Log.i(TAG, "--->>" + flag);
	}

	/*
	 * 保存数据
	 */
	public void read() {
		FileService fileService = new FileService(getContext());
		String msg = fileService.readContentFromFile("bb.txt");
		Log.i(TAG, "--->>" + msg);
	}
}

技术分享

保存成功后会在data-data-apk的安装目录下找到相应的文件:

技术分享


Android学习笔记之数据的内部存储方式实习数据的读写


推荐阅读
  • Linux     系统安装
    Linux系统安装linux系统安装准备工作电脑、u盘、光盘、网络、硬盘主要使用光盘、网络虚拟化软件vmwarevi ... [详细]
  • packagetest;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOE ... [详细]
  • vector:在vc6中,如果要镶嵌使用vector,如vector,后面的两个应该用,空格隔开,否则被编译器认为是移位符string::npos的值为 ... [详细]
  • Spark 贝叶斯分类算法
    一、贝叶斯定理数学基础我们都知道条件概率的数学公式形式为即B发生的条件下A发生的概率等于A和B同时发生的概率除以B发生的概率。根据此公式变换,得到贝叶斯公式:即贝叶斯定律是关于随机 ... [详细]
  • 安全3AAuthentication:认证Authorzation:授权Accouting|Audition:审计用户管理用户:UID:0,不一定是root,root的uid非0时 ... [详细]
  • 简单动态字符串redis里面很多地方都用到了字符串,我们知道redis是一个键值对存储的非关系型数据库,那么所有的key都是用字符串存储的,还有字符串类型,这些都是用字符串存储的 ... [详细]
  • 例子如Table表有性别字段,1代表男2代表女、3代表中性、还有没填就代表未说明selectid,decode(sex,'1','男', ... [详细]
  • 接口测试的方式有很多,比如可以用工具(jmeter,postman)之类,也可以自己写代码进行接口测试,工具的使用相对来说都比较简单,重点是要搞清楚项目接口的协议是什么,然后有针对 ... [详细]
  • C#的Type对象的简单应用
    通过Type对象可以获取类中所有的公有成员直接贴代码:classMyClass{privatestringname;privateintid;publicstringcity;pu ... [详细]
  • 作业迁移
    背景:数据库服务器更换,1、数据库迁移(BACKUPRESTORE);2、数据库登录名用户迁移(注意孤立用户);3、作业迁移数据库迁移,备份数据库、拷贝备份文件到新服务器,还原数据 ... [详细]
  • 定义:定义两个数论函数\(f\)、\(g\)的Dirichlet卷积为:\[\left(f*g\right)\left(n\right)\sum_{d|n}f\left(d\rig ... [详细]
  • rbac 4表 常规设计
    rbac4表常规设计设计模型:1、管理员表(users)Schema::create('users',function(Blueprint$table){$tabl ... [详细]
  • 读书这件事
    没事晒下自己的借书清单。60多本了,不知道自己一本子能读多少本?可惜的就是没写读书笔记,都是一些泛读。图书馆要 ... [详细]
  • phpstorm使用和配置技巧
    1.使用phpstorm的过程中,有时光标不小心变成了方块状,怎么修复回来呢?见下图,去掉“Useblockcare ... [详细]
  • 淘宝http:ip.taobao.cominstructions.php接口说明请求接口(GET):servicegetIpInfo.ph ... [详细]
author-avatar
zhaoyunnidaye_260
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有