数据存储--------详解持久化技术

2023-10-19 22:32

本文主要是介绍数据存储--------详解持久化技术,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文件存储

       方法存储是Android最基本的一种数据存储方式下面就给大家承上代码:

       

package com.example.liangshaoteng.fileoutputstream;import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;public class MainActivity extends AppCompatActivity {private BufferedWriter bufferedWriter;private EditText editText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);editText = (EditText) findViewById(R.id.et_text);}private void load() {try {//从代码的openFileInput我们就能看出是:打开文件从里面输入到什么地方FileInputStream data = openFileInput("dataSave");StringBuffer stringBuffer = new StringBuffer();//缓冲区读取BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(data));String line = "";//一行一行的读取while ((line = bufferedReader.readLine()) != null) {stringBuffer.append(line);}//通过StringBuffer追加到一起Toast出来Toast.makeText(getApplicationContext(), stringBuffer.toString(), Toast.LENGTH_LONG).show();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}private void save(String inputText) {if (inputText.isEmpty()) return;try {//从代码的openFileOutput我们就能看出是:打开文件从外面放东西//第一个参数是:文件的名称 第二个参数是:存储的模式//存储模式 ://Context.MODE_PRIVATE:别的应用不能访问得到SharedPreferences对象//Context.MODE_WORLD_READABLE:别的应用可以访问,并且是可以读取SharedPreferences中的数据,但不能写入数据//Context.MODE_WORLD_WRITEABLE:别的应用可以访问,可以在SharedPreferences中写入修改数据FileOutputStream dataSave = openFileOutput("dataSave", Context.MODE_PRIVATE);//在放东西的期间是需要一个缓冲BufferedWriterbufferedWriter = new BufferedWriter(new OutputStreamWriter(dataSave));//然后通过缓冲写入数据bufferedWriter.write(inputText);} catch (IOException e) {e.printStackTrace();} finally {Toast.makeText(getApplicationContext(), "保存成功!", Toast.LENGTH_LONG).show();if (bufferedWriter != null) {try {//不要忘记用完要关闭bufferedWriter.close();} catch (IOException e) {e.printStackTrace();}}}}public void commit(View view) {String trim = editText.getText().toString().trim();if (trim.isEmpty()) return;save(trim);}public void input(View view) {load();}
}



下面就是ShardPreference这一个非常简单所以我就不再详细的讲解了后续呢我将给出工具类大家可以下载直接拿来用

 大家先看一下工具类ShardPreference

 

package com.waywings.fm.utils;import android.content.Context;
import android.content.SharedPreferences;public class ShareUtil {public static String CONFIG = "config";public static String FCONFIG = "fmconfig";public static String INTFIG = "intconfig";private static SharedPreferences sharedPreferences;public static void saveFloatData(Context context, String key, Float value) {if (sharedPreferences == null) {sharedPreferences = context.getSharedPreferences(FCONFIG, Context.MODE_PRIVATE);}sharedPreferences.edit().putFloat(key, value).commit();}public static Float getFloatData(Context context, String key, Float defValue) {if (sharedPreferences == null) {sharedPreferences = context.getSharedPreferences(FCONFIG, Context.MODE_PRIVATE);}return sharedPreferences.getFloat(key, defValue);}public static void saveStringData(Context context, String key, String value) {if (sharedPreferences == null) {sharedPreferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);}sharedPreferences.edit().putString(key, value).commit();}public static String getStringData(Context context, String key, String defValue) {if (sharedPreferences == null) {sharedPreferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);}return sharedPreferences.getString(key, defValue);}public static int getIntData(Context context, String key, int defValue) {if (sharedPreferences == null) {sharedPreferences = context.getSharedPreferences(INTFIG, Context.MODE_PRIVATE);}return sharedPreferences.getInt(key, defValue);}public static void savaIntData(Context context, String key, int value) {if (sharedPreferences == null) {sharedPreferences = context.getSharedPreferences(INTFIG, Context.MODE_PRIVATE);}sharedPreferences.edit().putInt(key, value).commit();}
}

不足之处还望大家指出 !

Sqlite存储 : 请大家移步到我的另一篇文章 :点击打开链接

后续给大家提供的工具类的下载 :   工具类下载

这篇关于数据存储--------详解持久化技术的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/242791

相关文章

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

PHP轻松处理千万行数据的方法详解

《PHP轻松处理千万行数据的方法详解》说到处理大数据集,PHP通常不是第一个想到的语言,但如果你曾经需要处理数百万行数据而不让服务器崩溃或内存耗尽,你就会知道PHP用对了工具有多强大,下面小编就... 目录问题的本质php 中的数据流处理:为什么必不可少生成器:内存高效的迭代方式流量控制:避免系统过载一次性

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

MySQL的JDBC编程详解

《MySQL的JDBC编程详解》:本文主要介绍MySQL的JDBC编程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、前置知识1. 引入依赖2. 认识 url二、JDBC 操作流程1. JDBC 的写操作2. JDBC 的读操作总结前言本文介绍了mysq

Redis 的 SUBSCRIBE命令详解

《Redis的SUBSCRIBE命令详解》Redis的SUBSCRIBE命令用于订阅一个或多个频道,以便接收发送到这些频道的消息,本文给大家介绍Redis的SUBSCRIBE命令,感兴趣的朋友跟随... 目录基本语法工作原理示例消息格式相关命令python 示例Redis 的 SUBSCRIBE 命令用于订

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e

SpringBoot日志级别与日志分组详解

《SpringBoot日志级别与日志分组详解》文章介绍了日志级别(ALL至OFF)及其作用,说明SpringBoot默认日志级别为INFO,可通过application.properties调整全局或... 目录日志级别1、级别内容2、调整日志级别调整默认日志级别调整指定类的日志级别项目开发过程中,利用日志

Java中的抽象类与abstract 关键字使用详解

《Java中的抽象类与abstract关键字使用详解》:本文主要介绍Java中的抽象类与abstract关键字使用详解,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、抽象类的概念二、使用 abstract2.1 修饰类 => 抽象类2.2 修饰方法 => 抽象方法,没有

MySQL8 密码强度评估与配置详解

《MySQL8密码强度评估与配置详解》MySQL8默认启用密码强度插件,实施MEDIUM策略(长度8、含数字/字母/特殊字符),支持动态调整与配置文件设置,推荐使用STRONG策略并定期更新密码以提... 目录一、mysql 8 密码强度评估机制1.核心插件:validate_password2.密码策略级