流文件存储

一、流文件存储

1、基本方法简介

Android如果需要存储大量的数据,需要使用到文件存储

用来保存数据的方法为:openFileOutput(String name, int mode)

其中,name参数表示文件的明成,如果文件不存在,则直接创建,文件的存储位置为:/data/data/包名/files/文件目录,mode表示待存储文件的模式。

mode模式:

MODE_PRIVATE:表示私有文件,该文件只能被创建他的文件所访问

MODE_APPEND:表示新的存储内容会添加在原有文件内容的后面。

MODE_WORLD_READABLE:表示该文件能被所有的文件读取,但是不可以写入。

MODE_WORLD_WEITABLE:表示该文件能被所有的文件写入,也可以读取

用来查询数据的方法为openFileInput(String name),其中name参数表示文件的名称

注意:用来保存数据和查询数据的方法名与现实思维理解是相反的

向文件中保存数据的流程:

待输入数据(文本等)—>转化为字节数组—>字节数组—>加入FileOutputStream—>FileOutputStream—>存入文件—>文件

从文件中查询数据的流程图:

文件—>转为FileInputStream—>FileInputStream—>FileInputStream取出字节数组—>字节数组—>转为数据—>数据

try{

    String string ="111";

    //将内容转化为自己数组

    byte[] buffer = string.getBytes();

    //创建文件输出流及文件demo.text

    FileOutputStream fos = openFileOutput("demo.txt",MODE_PRIVATE);

    //将自己数组通过文件输出流存入demo.txt

    fos.write(buffer);

    //关闭文件输出流

    fos.close();

}catch(Exception e){

    e.printStackTrace();

}

除了可以将文件保存在默认的位置外(/data/data/包名/files/),还可以保存在其他位置:

比如

(1)存储到/data/data/(本工程的包名)/demo.txt中:

File myfile = new File("/data/data/包名/demo.txt");

FileOutputStream fos = new FileOutputStream(myfile);

注意:只能存储到本工程的包名文件夹下面

(2)存储到sdcard中:

String path = Enviroment.getDownloadCacheDorectory().getPath();

File myfile = new File(pah+"demo.txt");

FileOutputStream fos = new FileOutputStream(myfile);

此外,还需要添加与SD卡相关的权限。

 

查询文本的核心代码:

try{

    //区的文件,并肩文件中的字节数组放入文件输入流中

    FileInputStream fis = openFileInput("demo.txt");

    //取得文件的大小,available()方法可以取得FileInputStream文件输出流的大小

    int length = fis.available();

    //设置缓冲字节数组,与文件大小相同

    byte[] buffer = new byte[length];

    //将文件输入流中的字节数组放入缓冲节数组
    fis.read(buffer);

    //我下两种方法是将缓冲字节数组转化为文本

    //String queryResult = new String(buffer,"UTF-8");

    String queryResult=EncodingUtils.getString(buffer,"UTF-8");

    fis.close();

}catch(Exception e){

    e.printStackTrace();

}

小知识:

//读取raw资源文件夹下面的内容

Resources myres = getResources();

InputStream is = myres.openRawResource(R.raw.demo1);

//读取Assets文件夹下的内容

Resources myres = getResources();

InputStream is = myres.getAssets().open(filename);

//读取sdcard文件夹下的内容

File file = new File("/sdcard/filename");

FileInputStream fis = new FileInputStream(file);

//读取特定未知的文件

File file = new File("/data/data/包名/filename");

FileInputStream fis = new FileInputStream(file);

 

一、文件存储

1.将数据存储到文件中

Context类提供了一个openFileOutput()方法,可用于将数据存储到指定的文件中,这个方法接收两个参数,第一个是参数名,在文件创建的时候就是使用的这个名称,第二个参数是文件的操作方式,一个是MODE_PRIVATE(是默认时的操作方式,表示当指定同样文件名的时候,所写入的内容将会覆盖原文件中的内容)和MODE_APPEND(表示如果该文件已存在,就往文件里追加内容,不存在就创建新文件)

openFileOutput()方法返回一个FileOutputStream对象,得到这个对象之后就可以使用JAVA流的方式将数据写入到文件中了

保存:

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    String inputText=edit.getText().toString();
    save(inputText);
}

 

private void save(String inputText) {
    // TODO Auto-generated method stub
    FileOutputStream out=null;
    BufferedWriter writer=null;
    try{
        out=openFileOutput("data", Context.MODE_PRIVATE);
        //用openFileOutput方法得到一个FileOutputStream对象
        writer=new BufferedWriter(new OutputStreamWriter(out));
        writer.write(inputText);
        //通过BufferedWriter将文本写入到文件夹中
    }catch(IOException e){
        e.printStackTrace();
    }finally{
        try{
            if(writer!=null){
                writer.close();
            }
        }catch(IOException e){
        e.printStackTrace();
        }
    }
}

 

提取

String inputText=load();
if(!TextUtils.isEmpty(inputText)){
    //判断是否为空,TextUtils.isEmpty()方法可以一次性进行两种空值的判断,当传入的字符串
    //等于null或者等于空字符串的时候都会返回true,从而不需要单独去判断这两种空值
    edit.setText(inputText);
    //就把读取的内容添加到EditText中
    edit.setSelection(inputText.length());
    //调用这个方法将输入的光标移动到文本的末尾以便于继续输入
    Toast.makeText(this, "Restoring succeeded",Toast.LENGTH_SHORT).show();

    private String load() {
        FileInputStream in=null;
        BufferedReader reader=null;
        StringBuilder content=new StringBuilder();
        try{
            in=openFileInput("data");
            reader=new BufferedReader(new InputStreamReader(in));
            String line="";
            while ((line=reader.readLine())!=null){
                content.append(line);
            }
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            if(reader!=null){
                try{
                    reader.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        }
        return content.toString();
    }
}

  

posted @ 2015-08-27 10:42  ChHM  阅读(646)  评论(0编辑  收藏  举报