android的数据储存(1)(SharedPreference、File)

2023-10-21 00:59

本文主要是介绍android的数据储存(1)(SharedPreference、File),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、有些时候程序有少量的数据需要存储时,而且格式简单,是普通的字符串,标量等,对于这种数据android提供了SharedPreference进行保存。

 

二、SharedPreference保存的数据是简单的key--value对,SharedPreference接口主要负责,读取应用程序的SharedPreference数据,而SharedPreference是没有写的能力,只有读的能力。

由于SharedPreference是接口,是无法直接创建实例,只有通过Context提供的getSharedPreferences(String name,int mode)

name:为数据保存的文件名

mode:可以有三个,但是其中有两个是不安全的,android已经不再推荐了。所以mode用:MODE_PRIVATE

1、boolean  contains(String key):判断SharedPreference是否包含特定的key

2、abstract Map<String,?> getAll():获取SharedPreference中的全部key--value对3、

3、boolean getXxx(String key, xx defValue):获取SharedPreference指定的key--value,如果存在value则返回,如果不存在就返回defValue默认值。其中xx可以是boolean,flaot,int,long,String等。

 

三、要实现数据的写入,要使用Editor对象,此对象是通过SharedPreference调用edit()方法得到、Editor对象提供如下的方法对数据的写入。

1、SharedPreference.Editor  clear():清空SharedPreference里面的所有数据

2、SharedPreference.Editor  putXxx(String key,xxx  value):向SharedPreference里面存入指定key对的数据,xxx可以是boolean,flaot,int,long,String等。

3、SharedPreference.Editor  remove(String key):删除SharedPreference里面指定的key对数据项

4、boolean  commit():当Editor编辑完成后,调用该方法提交修改

 

实例:对SharedPreference数据进行操作

1、xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/write"android:text="write"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/read"android:text="read"/>
</LinearLayout>

2、activity

public class SharedPreferencesActivity extends AppCompatActivity {SharedPreferences sharedPreferences;SharedPreferences.Editor editor;static int count;public void onCreate(Bundle saveInstanceState) {super.onCreate(saveInstanceState);setContentView(R.layout.sharedpreferences_activity);sharedPreferences=getSharedPreferences("crazyit",MODE_PRIVATE);  //sharedPreferences为接口,无法直接创建,调用context中的方法,实例化sharedPreferences,进行对数据的读editor=sharedPreferences.edit();//调用方法获得Editor对象,对数据进行写Button write=findViewById(R.id.write);Button read=findViewById(R.id.read);count=sharedPreferences.getInt("count",0);write.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日"+"hh:mm:ss");editor.putString("time",sdf.format(new Date()));  //写入日期editor.putInt("random",(int)(Math.random()*100));  //写入一个整数editor.putInt("count",++count);        //写入counteditor.commit();                //提交修改}});read.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {    //进行读String time=sharedPreferences.getString("time",null);  int random=sharedPreferences.getInt("random",0);int count1=sharedPreferences.getInt("count",0);Toast.makeText(SharedPreferencesActivity.this,time+" "+random+" "+count1,Toast.LENGTH_SHORT).show();}});}}

 

 

四、File存储

一、Context提供了如下的两种方式打开程序数据文件的io流

1、FileInputStream openFileInput(String name):打开应用程序数据文件下的name文件对应输入流,对于读数据

2、FileOutputStream openFileOutput(String name,int mode):打开应用程序的数据文件下name文件对应的输入流,对应写数据,

其中mode对应三个模式,但是其中两个已经不推荐使用,所以mode:MODE_PRIVATE

 

二、Context还提供了如下的几种方式访问应用程序的数据文件

1、getDir(String name,int mdoe):在应用程序的数据文件夹下获取或创建name对应的子目录

2、File getFilesDir():获取应用程序的数据文件的绝对路径

3、String[]  fileList():返回应用程序文件夹下的全部文件

4、deleteFile(String):删除应用程序的数据文件指定的文件

 

向程序数据文件夹内输入数据:

    //以追加的方式打开文件夹的输入流FileOutputStream fos=openFileOutput(FILE_NAME,MODE_PRIVATE);//将FileOutputStream封装成printStreamPrintStream ps=new PrintStream(fos);//输出内容到文件ps.print(content);ps.close();

 

向应用程序数据文件读数据

   //打开name程序数据文件流输入流FileInputStream fil=openFileInput(FILE_NAME);//用于接收每次读出的数据byte[] buff=new byte[1024];int hasRead=0; //读取出来的全部数据StringBuilder builder=new StringBuilder(); while((hasRead=fil.read(buff))>0){  //判断数据是否读完builder.append(new String(buff,0,hasRead));}//关闭文件流fil.close();

 

 

实例:程序写如数据到程序文件夹内,在读取文件夹里面的内容

public class FileActivity  extends AppCompatActivity{private String FILE_NAME="/crazyit.bin";EditText editText1;EditText editText2;protected void onCreate(Bundle saveInstanceSate) {super.onCreate(saveInstanceSate);LinearLayout linearLayout=new LinearLayout(this);linearLayout.setOrientation(LinearLayout.VERTICAL);Button read=new Button(this);Button write=new Button(this);read.setText("read");write.setText("write");editText1=new EditText(this);editText2=new EditText(this);linearLayout.addView(write);linearLayout.addView(editText1);linearLayout.addView(read);linearLayout.addView(editText2);setContentView(linearLayout);read.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {editText2.setText(read());}});write.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {write(editText1.getText().toString());editText1.setText("");}});}private String read(){//向应用程序的数据文件夹readtry{FileInputStream fil=openFileInput(FILE_NAME);byte[] buff=new byte[1024];int hasRead=0;StringBuilder builder=new StringBuilder();while((hasRead=fil.read(buff))>0){builder.append(new String(buff,0,hasRead));}//关闭文件流fil.close();return builder.toString();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return null;}private void write(String content){//向应用程序的数据文件夹writetry{FileOutputStream fos=openFileOutput(FILE_NAME,MODE_PRIVATE);//将FileOutputStream封装成printStreamPrintStream ps=new PrintStream(fos);ps.print(content);ps.close();} catch (FileNotFoundException e) {e.printStackTrace();}}}

 

 

 

 

 

 

五、读取SD卡上的文件

一、为了更好地存取应用程序地大文件,应用程序需要读写SD卡上的文件,SD卡大大地扩充了手机地存储功能。分为几个步骤。

1、调用Environment的getExteranlStorageState():方法判断手机是否插入SD卡,并且应用程序具有读写的SD卡的权限。

//如果手机已插入sd卡,且应用程序具有对鞋SD卡的能力,下面返回false

Environment.getExternalStorageDirectory().equals(Environment.MEDIA_MOUNTED)

2、调用Environment的getExternalStorageDirectory()方法来获取外部存储器,也就是SD卡的目录

3、使用FileInputStream,FileOutputStream   ,FileRead或者FileWrite(),方法来读写SD卡

4、读写SD卡是一个危险权限,需要在androidManifest.xml文件中声明权限

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>    //sd卡中删除文件权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>       //sd卡中读写文件权限

 

SD卡读:

    //如果手机插入SD卡,而且应用程序具有访问sd卡的权限if(!Environment.getExternalStorageDirectory().equals(Environment.MEDIA_MOUNTED)){//获取sd卡对应的存储目录File sdCardDir=Environment.getExternalStorageDirectory();//获取指定文件的输入流FileInputStream fis=new FileInputStream(sdCardDir.getCanonicalPath()+FILE_NAME);//将指定的输入流包装成BBufferedReaderBufferedReader bufferedReader=new BufferedReader(new InputStreamReader(fis));StringBuilder builder=new StringBuilder("");String line=null;while ((line=bufferedReader.readLine())!=null){builder.append(line);}bufferedReader.close();return builder.toString();}

SD卡写:

if(!Environment.getExternalStorageDirectory().equals(Environment.MEDIA_MOUNTED)){//获取sd卡对应的存储目录Toast.makeText(FileActivity.this,"有权限",Toast.LENGTH_SHORT).show();File sdCardDir=Environment.getExternalStorageDirectory();File targetFile=new File(sdCardDir.getCanonicalPath()+FILE_NAME);Toast.makeText(FileActivity.this,sdCardDir.getCanonicalPath()+FILE_NAME,Toast.LENGTH_SHORT).show();//将指定文件创建RandomAccessFile对象//这里使用RandomAccessFile向SD卡中指定追加内容,如果使用FileOutputStream向指定文件写数据会将原有的文件内容清空,而不是追加的方式RandomAccessFile raf=new RandomAccessFile(targetFile,"rw");//将文件的指针移动到最后面raf.seek(targetFile.length());//输出文件内容raf.write(content.getBytes());Toast.makeText(FileActivity.this,"完成",Toast.LENGTH_SHORT).show();raf.close();
}

 

实例:读写sd卡中的数据

public class FileActivity  extends AppCompatActivity{private String FILE_NAME="/crazyit.bin";EditText editText1;EditText editText2;protected void onCreate(Bundle saveInstanceSate) {super.onCreate(saveInstanceSate);LinearLayout linearLayout=new LinearLayout(this);linearLayout.setOrientation(LinearLayout.VERTICAL);Button read=new Button(this);Button write=new Button(this);read.setText("read");write.setText("write");editText1=new EditText(this);editText2=new EditText(this);linearLayout.addView(write);linearLayout.addView(editText1);linearLayout.addView(read);linearLayout.addView(editText2);setContentView(linearLayout);read.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {editText2.setText(read());}});write.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {write(editText1.getText().toString());editText1.setText("");}});}private String read(){//SD卡读try{//如果手机插入SD卡,而且应用程序具有访问sd卡的权限if(!Environment.getExternalStorageDirectory().equals(Environment.MEDIA_MOUNTED)){//获取sd卡对应的存储目录File sdCardDir=Environment.getExternalStorageDirectory();//获取指定文件的输入流FileInputStream fis=new FileInputStream(sdCardDir.getCanonicalPath()+FILE_NAME);//将指定的输入流包装成BBufferedReaderBufferedReader bufferedReader=new BufferedReader(new InputStreamReader(fis));StringBuilder builder=new StringBuilder("");String line=null;while ((line=bufferedReader.readLine())!=null){builder.append(line);}bufferedReader.close();return builder.toString();}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return null;}private void write(String content){//SD卡写try{//如果手机插入SD卡,而且应用程序具有访问sd卡的权限if(!Environment.getExternalStorageDirectory().equals(Environment.MEDIA_MOUNTED)){//获取sd卡对应的存储目录Toast.makeText(FileActivity.this,"有权限",Toast.LENGTH_SHORT).show();File sdCardDir=Environment.getExternalStorageDirectory();File targetFile=new File(sdCardDir.getCanonicalPath()+FILE_NAME);Toast.makeText(FileActivity.this,sdCardDir.getCanonicalPath()+FILE_NAME,Toast.LENGTH_SHORT).show();//将指定文件创建RandomAccessFile对象//这里使用RandomAccessFile向SD卡中指定追加内容,如果使用FileOutputStream向指定文件写数据会将原有的文件内容清空,而不是追加的方式RandomAccessFile raf=new RandomAccessFile(targetFile,"rw");//将文件的指针移动到最后面raf.seek(targetFile.length());//输出文件内容raf.write(content.getBytes());Toast.makeText(FileActivity.this,"完成",Toast.LENGTH_SHORT).show();raf.close();}} catch (IOException e) {e.printStackTrace();}}
}

 

 

实例:sd卡文件夹浏览器

(如果开发者不想用Environment.getExternalStorageDirectory().equals(Environment.MEDIA_MOUNTED),也可以使用/mnt/sdcard/路径代表SD卡路径

1、xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/path"android:textSize="20sp"android:layout_gravity="center_horizontal"android:layout_alignParentTop="true"/><ListViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/list"android:divider="#000"android:dividerHeight="1px"android:layout_below="@+id/path"/><Buttonandroid:layout_width="50dp"android:layout_height="50dp"android:background="@drawable/pzb"android:layout_centerHorizontal="true"android:layout_alignParentBottom="true"android:id="@+id/parent"/>
</RelativeLayout>

 

2、activity

public class FileListActivity extends AppCompatActivity {ListView listView;TextView textView;//当前父目录File currentParent;File[] currentFiles;public void onCreate(Bundle saveInstanceState) {super.onCreate(saveInstanceState);setContentView(R.layout.listfile_activity);listView=findViewById(R.id.list);textView=findViewById(R.id.path);//获取系统的SD卡的目录File root=new File("/mnt/sdcard/");//如果SD卡存在if(root.exists()){currentParent=root;currentFiles=root.listFiles();//使用当前目录下面的全部文件,文件夹来填充listViewinflateListView(currentFiles);}//为listview的列表的单击事件绑定监听器listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {//如果用户点击文件不是,直接返回,不做任何处理if(currentFiles[i].isFile())return ;//获取用户点击的文件夹下面的文件夹File []tmp=currentFiles[i].listFiles();if(tmp==null||tmp.length==0){Toast.makeText(FileListActivity.this,"当前的路径不可访问,该路径下面没有文件",Toast.LENGTH_SHORT).show();}else {//获取用户单击的列表项对应的文件夹,设当前的文件夹为父文件夹currentParent=currentFiles[i];//保存当前点的父文件夹的全部内容currentFiles=tmp;//再次更新ListViewinflateListView(currentFiles);}}});//获取上一级目录按钮Button parent=findViewById(R.id.parent);parent.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {try {if(!currentParent.getCanonicalPath().equals("/mnt/sdcard")){//获取上一级目录currentParent=currentParent.getParentFile();//列出当前目录下面的所有文件currentFiles=currentParent.listFiles();//再次更新listViewinflateListView(currentFiles);}} catch (IOException e) {e.printStackTrace();}}});}private void inflateListView(File[] files){//创建一个list集合,list集合的元素是MapList<Map<String,Object>> list=new ArrayList<Map<String,Object>>();for(int i=0;i<files.length;i++){Map<String,Object> listItem=new HashMap<String,Object>();//如果当前的file是文件,使用folder图标,否则使用file图标if(files[i].isDirectory()){listItem.put("icon",R.drawable.p7txxw);}else{listItem.put("icon",R.drawable.tp);}listItem.put("fileName",files[i].getName());//添加list项list.add(listItem);}SimpleAdapter adapter=new SimpleAdapter(this,list,R.layout.filelist_item,new String[]{"icon","fileName"},new int[]{R.id.file_image,R.id.file_name});listView.setAdapter(adapter);try{textView.setText("当前目录:"+currentParent.getCanonicalPath());} catch (IOException e) {e.printStackTrace();}}
}

 

 

 

 

 

 

 

 

 

 

 

这篇关于android的数据储存(1)(SharedPreference、File)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Android 实现一个隐私弹窗功能

《Android实现一个隐私弹窗功能》:本文主要介绍Android实现一个隐私弹窗功能,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 效果图如下:1. 设置同意、退出、点击用户协议、点击隐私协议的函数参数2. 《用户协议》、《隐私政策》设置成可点击的,且颜色要区分出来res/l

Android实现一键录屏功能(附源码)

《Android实现一键录屏功能(附源码)》在Android5.0及以上版本,系统提供了MediaProjectionAPI,允许应用在用户授权下录制屏幕内容并输出到视频文件,所以本文将基于此实现一个... 目录一、项目介绍二、相关技术与原理三、系统权限与用户授权四、项目架构与流程五、环境配置与依赖六、完整

Android 12解决push framework.jar无法开机的方法小结

《Android12解决pushframework.jar无法开机的方法小结》:本文主要介绍在Android12中解决pushframework.jar无法开机的方法,包括编译指令、框架层和s... 目录1. android 编译指令1.1 framework层的编译指令1.2 替换framework.ja

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义