数据存储之四种方式之一

来源:互联网 发布:js 正则匹配标点符号 编辑:程序博客网 时间:2024/05/01 22:28

对于数据存储操作而言,安卓提供了五种方式:1、SharedPreferences方式  2、文件存储方式  3、SQLite 数据库存储   4、Content Provider方式   5、网络存储

1、SharedPreferences提供了一些基础的保存功能,所有的信息都是按照“key=value”的形式进行保存的,保存的类型都是基本类型,例如:字符串、整型、布尔型等。只需在程序中创建文件名,然后存储数据,可在fileExploer中查找存储的文件

SharedPreferences share=super.getSharedPreferences(FILENAME, MODE_PRIVATE);  //取得实例
SharedPreferences.Editor edit=share.edit();
edit.putString("book", "red and black");
edit.putInt("age", 25);
edit.putBoolean("yes or no", true);
edit.commit();

读取文件并显示出来,只需要在textView中设置显示读取的数据

 this.authorinfo.setText("作者:"+share.getString("author", "没有作者"));
    this.ageinfo.setText("年龄:"+share.getInt("age", 0));

2、文件存储方式

IO流输出/输入文件的流程

。使用File类定义一个要操作的文件
。使用字节流或字符流的子类为父类进行实例化,因为四个IO流的操作类都是抽象类
。完成输入/输出的功能
。关闭流

如果现在要输出内容使用PrintStream很方便,但是要输入内容,就一定要使用Scanner完成


private static final String FILENAME="vanchu.txt";
FileOutputStream output=null;                //输出文件
try {
output=super.openFileOutput(FILENAME, Activity.MODE_PRIVATE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
PrintStream print=new PrintStream(output);
print.println("姓名:jackie");
print.println("性别:男");
print.println("年龄:25");
print.println("地址:shenzh");
print.close();

this.show=(TextView) super.findViewById(R.id.show);  //读取文件
FileInputStream input=null;
try {
input=super.openFileInput(FILENAME);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Scanner scan=new Scanner(input);
while(scan.hasNext()){
this.show.append(scan.next()+"\n");
}
scan.close();

如果要向指定的sdcard下存储数据,就需要指定文件的路径,而且还需要操作sdcard的权限。

1、  输出文件,并保存到Sdcard上

Sdcard卡的路径不存在则可以自动生成,操作储存卡需要保存文件的权限和创建文件夹的权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

创建文件夹,输出保存文件的代码如下

File file = new File(FILENAME);// 定义要操作的文件

           if(!file.getParentFile().exists()) {

                    file.getParentFile().mkdirs();// 创建文件夹路径

           }

           PrintStream out = null;

           try {

                    out = newPrintStream(new FileOutputStream(file));

                    out.println("不够成熟 ,寂寞寂寞就好,夏天的风,独家记忆,爱死了昨天,菊花台  爱夏  冷雨夜 白色风车  我是一只小小鸟 ");

           } catch (Exception e) {

                    e.printStackTrace();

           } finally { // 一定要执行关闭

                    if (out != null){

                             out.close();

                    }

           }

 

判断sdcard是否存在

if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){

                    System.out.println("***"+Environment.getExternalStorageDirectory());

           }

 

Environment这个类可以提供sdcard的路径,设置了路径, File.separator路径分隔符

Filefile=newFile(Environment.getExternalStorageDirectory()+File.separator+DIR+File.separator+FILENAME);

 

输入文件与输出文件的程序没多大区别,用Scanner就可以完成FileInputStream’

将res下的资源文件输出在textView中显示,代码如下

this.msg=(TextView) super.findViewById(R.id.msg);
        Resources res=super.getResources();
        InputStream input=res.openRawResource(R.raw.vanchu1);
        Scanner scan=new Scanner(input);
        StringBuffer buf=new StringBuffer();
        while(scan.hasNext()){
        buf.append(scan.next()).append("\n");
        }
        scan.close();
        try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
        this.msg.setText(buf);



0 0
原创粉丝点击