android数据存储——文件存储
来源:互联网 发布:移动宽带端口扩容申请 编辑:程序博客网 时间:2024/06/08 16:06
android数据四大存储方式之一——文件存储
文件存储可以将一些数据直接以文件的形式保存在设备中:一些文本文件、PDF文件、音视频文件、图片等
一、数据存储路径:android系统中的/data/data/<package name>/files的.txt文件下
二、一些重要的操作方法:
重要操作方法方法含义openFileInput()打开应用程序文件以便读取openFileOutput()创建应用程序文件以便写入deleteFile()通过文件名称删除文件fileList()获得所有/data/data/<package name>/files
下的文件列表
getFileeDir()获得所有/data/data/<package name>/files子目录对象
getCacheDir()获得所有/data/data/<package name>/cache
子目录对象
getDir()根据名称创建或获取一个子目录三、数据存储过程
1)创建一个文件首先要建立一个输出流(先获得一个输出流对象)
方法:openFileOutput("myFile.txt",Context.MODE_PRIVATE);//myFile.txt是创建的文件名
2)向流中写入数据
方法:write(data.getBytes());
3)数据写完后需要关闭流
方法:fos.close();
四、读取文件
1) ①创建输入流,参数是文件名
方法:fis = openFileInput("myFile");
②上面方法得到的是字节流,需要将其装换为字符流,以便显示
方法:isr =new InputStreamReader(fis);
br = new BufferedReader(isr);
③得到字符流后将其包装为缓冲流,一段一段读取减少读写次数
2)读数据
方法:String s = null;
s = (br.readLine());
3)关闭输入流(每个流都要关闭)
fis.close();
isr.close();
br.close();
五、实例
1)描述:在第一给编辑框中输入数据,点击写入按钮数据就会写入到myFile.txt文件里,点击读取按钮myFile.txt文件里的内容就会第二个编辑框中显示。
2)实现代码
布局
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginLeft="20dp" android:layout_marginTop="15dp" android:ems="10" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_below="@+id/editText1" android:layout_marginTop="24dp" android:text="写入" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_below="@+id/button1" android:layout_marginTop="31dp" android:ems="10" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/editText2" android:layout_below="@+id/editText2" android:layout_marginTop="24dp" android:text="读取" /></RelativeLayout>主代码
package com.example.filesave;import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import android.os.Bundle;import android.app.Activity;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 implements OnClickListener{//OnClickListenerprivate Button button01;private Button button02;private EditText edittext01;private EditText edittext02;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);init();}public void init(){button01 = (Button)findViewById(R.id.button1); button02 = (Button)findViewById(R.id.button2); edittext01 = (EditText)findViewById(R.id.editText1); edittext02 = (EditText)findViewById(R.id.editText2); button01.setOnClickListener(this);button02.setOnClickListener(this);edittext01.setOnClickListener(this);edittext02.setOnClickListener(this);}public void onClick(View v){switch(v.getId()){case R.id.button1:write(edittext01.getText().toString());break;case R.id.button2:read();break;default: break;}}private void write(String content){try{//openFileOutput()的第二个参数为操作模式//Context.MODE_PRIVATE = 0 :为默认操作模式,代表该文件是私有数据//Context.MODE_APPEND = 32768 :模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件//Context.MODE_WORLD_READABLE = 1:表示当前文件可以被其他应用读取//Context.MODE_WORLD_WRITEABLE = 2:表示当前文件可以被其他应用写入//如果希望文件被其他应用读和写,可以传入://openFileOutput("itcast.txt", Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);FileOutputStream fos = openFileOutput("myFile.txt",MODE_APPEND);//实例化文件输出流//向流写内容fos.write(content.getBytes());//关闭文件流fos.close();Toast.makeText(this, "write", Toast.LENGTH_LONG).show();}catch (Exception e){e.printStackTrace();}}private void read(){try{//实例化文件输入流FileInputStream fis = openFileInput("myFile.txt");//实例化字节数组输出流ByteArrayOutputStream stream = new ByteArrayOutputStream();//定义缓存数组byte[] buffer = new byte[fis.available()];//定义数组长度int length=-1;while((length=fis.read(buffer))!=-1){stream.write(buffer,0,length);}//关闭流文件stream.close();fis.close();//往编辑框写内容edittext02.setText(stream.toString());Toast.makeText(this, "read", Toast.LENGTH_LONG).show();}catch (FileNotFoundException e){e.printStackTrace();}catch(IOException e){return;}}@Overridepublic 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;}}3)效果图
数据保存的路径
- Android数据存储——文件存储
- android数据存储——文件存储
- android数据存储—文件存储
- Android中数据存储——文件存储数据
- Android中数据存储——文件存储数据
- Android 数据存储操作(2)——文件存储
- Android学习14——数据存储之文件存储
- Android数据存储——2.文件存储_A_文本文件
- Android数据存储访问——文件存储
- Android数据存储访问——文件存储
- Android开发——数据存储之文件存储
- Android数据存储方式之——文件存储
- Android安卓——数据存储之文件存储
- Android 数据存储--文件存储
- android 数据存储-文件存储
- Android数据存储——文件ANDsharepreferences
- 数据存储——文件存储
- 数据存储——文件存储
- 剑指offer-面试题32.从1到n整数中1出现的次数
- 二项堆 Binomial Heap 与 二项树 Binomial Tree 性质探究(主要针对归并 merge 操作)
- kafka创建topic报错:KeeperException$NoNodeException: KeeperErrorCode = NoNode for /brokers/ids
- 剑指offer-面试题37.两个链表的第一个公共结点
- parcelable接口
- android数据存储——文件存储
- 几种最常见的网站盈利模式分析
- 唯品会——java开发实习生机试
- java导出Excel通用方法
- 常见jquery函数集合总结
- cocos2d-x 3.0菜单的使用
- WebSphere安装、系统部署、连接池
- Hdoj-1563-Find your present!
- Android数据库 SQLite数据库