1、流文件存储数组
一、基本方法简介app
Android若是须要存储大量的数据,须要使用到文件存储ide
用来保存数据的方法为:openFileOutput(String name, int mode)ui
其中,name参数表示文件的明成,若是文件不存在,则直接建立,文件的存储位置为:/data/data/包名/files/文件目录,mode表示待存储文件的模式。this
mode模式:spa
MODE_PRIVATE:表示私有文件,该文件只能被建立他的文件所访问
MODE_APPEND:表示新的存储内容会添加在原有文件内容的后面。
MODE_WORLD_READABLE:表示该文件能被全部的文件读取,可是不能够写入。
MODE_WORLD_WEITABLE:表示该文件能被全部的文件写入,也能够读取
用来查询数据的方法为openFileInput(String name),其中name参数表示文件的名称code
注意:用来保存数据和查询数据的方法名与现实思惟理解是相反的对象
向文件中保存数据的流程:blog
待输入数据(文本等)—>转化为字节数组—>字节数组—>加入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、文件存储
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(); } }