安卓学习之文件存储(笔记)

通过getSharedPreferences(“文件名”,权限)获取SharedPreferences;

SharedPreferences sp = getSharedPreferences(“info”,MODE_PRIVATE);
- 保存值:(通过sp获取edit,通过edit中的方法进行保存值)
Editor editor= sp.edit();
editor.putString(“uName”,uName); //uName在这里是从后台获取的用户名数据
editor.putString(“uPass”,uPass); //upass是从后台获取的密码数据
editor.commit();
- 取值:
String u =sp.getString(“uName”,”“);
String p =sp.getString(“uPass”,”“);

序列化

以短信为例:

public class CMS {
    private String  form;
    private String contentx;
    private String time;
    public String getForm() {
        return form;
    }
    public void setForm(String form) {
        this.form = form;
    }
    public String getContentx() {
        return contentx;
    }
    public void setContentx(String contentx) {
        this.contentx = contentx;
    }
    public String getTime() {
        return time;
    }
    public void setTime(String time) {
        this.time = time;
    }
    @Override
    public String toString() {
        return "CMS{" +
                "form='" + form + '\'' +
                ", contentx='" + contentx + '\'' +
                ", time='" + time + '\'' +
                '}';
    }
}

使用普通方法保存数据到xml:

    public void saveCMS(view v){
       StringBuffer  sb= new StringBuffer();
        sb.append("");
        sb.append("");
        for (int i=0;i
            CMS  cms=cmsList.get(i);
            sb.append("");
            sb.append("
"); sb.append(cms.getForm()); sb.append(""
); sb.append(""); sb.append(cms.getContent()); sb.append(""); sb.append("); sb.append(cms.getTime()); sb.append(""); sb.append(""); } sb.append(""); String xml = sb.toString(); try{ FileOutputStream fos =openFileOutput("cms.xml",MODE_PRIVATE); fos.write(xml.getBytes()); fos.close(); }catch (Exception e){ e.printStackTrace(); } }

使用序列化进行保存数据

 //xml序列化器
    public  void saveCMSby(view v){
      XmlSerializer serializer= Xml.newSerializer();
        try{
            serializer.setOutput(openFileOutput("cms.xml",MODE_PRIVATE),"utf-8");
            serializer.startDocument("utf-8",true);
            serializer.startTag(null,"scmlist");
            for(CMS m:cmsList){
                serializer.startTag(null,"cms");
                serializer.startTag(null,"form");
                serializer.text(m.getForm());
                serializer.endTag(null,"form");
                serializer.startTag(null,"content");
                serializer.text(m.getContent());
                serializer.endTag(null,"content");
                serializer.startTag(null,"time");
                serializer.text(m.getTime());
                serializer.endTag(null,"time");
                serializer.endTag(null,"cms");
            }
            serializer.endTag(null,"scmlist");
            serializer.endDocument();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

解析xml文件

获取解析器:

public  void  parXml(View  view){
        XmlPullParser pullParser=Xml.newPullParser();
        List cmsList=null;
        CMS cms=null;
        try {
            pullParser.setInput(openFileInput("cms.xml"),"utf-8");
            int  eventType=pullParser.getEventType();
            while (eventType!=XmlPullParser.END_DOCUMENT){
                switch (eventType){
                    case XmlPullParser.START_TAG:
                        if("cmslist".equals(pullParser.getName())) {
                                cmsList=new ArrayList<>();
                        }else if("cms".equals(pullParser.getName())){
                            cms=new CMS();
                        }else if("form".equals(pullParser.getName())){
                            cms.setForm(pullParser.nextText());

                        }else if("content".equals(pullParser.getName())){
                            cms.setContent(pullParser.nextText());
                        }else if("time".equals(pullParser.getName())){
                            cms.setTime(pullParser.nextText());
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if("cms".equals(pullParser.getName())){
                            cmsList.add(cms);
                        }
                        break;
                }
               eventType= pullParser.next();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

你可能感兴趣的:(android)