Android文件存储小结

来源:互联网 发布:麦迪在cba的数据 编辑:程序博客网 时间:2024/06/10 10:01

核心技术就是Context类的openFileInput()和openFileOutput()方法,之后就是利用java的各种流进行读写操作。但是文件存储并不适用于保存一些比较复杂的文本数据。
典型结构:

/*存储*/FileOutputStream out=null;PrintWriter pw=null;try{    out=openFileOutput("data",Context.MODE_PRIVATE);    //可以看出openFileOutput方法返回的是FileOutputStream类型    pw=new PrintWriter(out);    pw.write(inputText);    pw.flush();}catch(Exception e){    e.printStackTrace();}finally{    try{        if(writer!=null){            writer.close();        }    }catch(IOException e){        e.printStackTrace();    }}
/*读取*/FileInputStream in=null;BufferedReader br=null;StringBuilder sb=new StringBuilder();try{    in=openFileInput("data");    br=new BufferedReader(new InputSreamReader(in));    String temp="";    if((temp=br.readLine())!=null){        sb.append(temp);    }}catch(IOException e){    e.printStackTrace();}finally{    if(br!=null){        try{            br.close();        }catch(IOException e){            e.printStackTrace();        }    }}
0 0
原创粉丝点击