文件
通过文件流进行读写操作:FileInputStream , FileOutputStream
对ROM中的files目录操作:String filePath = context.getFilesDir();
对ROM中的cache目录操作:String filePath = context.getCacheDir();
对SD卡目录操作:String filePath = Environment.getExternalStorageDirectory()
FileInputStream , FileOutputStream 和
context.openFileOutput(“private.txt”,Context.MODE_PRIVATE); context.openFileInput(“private.txt”); 区别:
FileInputStream , FileOutputStream可以指定任意路径下的文件进行读写;
而openFileOutput , openFileInput只能是ROM中当前应用名下files目录进行读写;
SharedPreferences
此方法是对ROM目录下的shared_prefs目录下的sp.xml文件进行读写操作
写入数据:
SharedPreferences sp = context.getSharedPreferences("sp",Context.MODE_PRIVATE);
Editor editor = sp.edit();
editor.putBoolean("gender", true);
editor.putString("username", username);
editor.putInt("age", 24);
editor.putFloat("salary", 13888.88f);
editor.commit();
取出数据:
SharedPreferences sp = this.getSharedPreferences("sp",
Context.MODE_PRIVATE);
String username = sp.getString("username", "");
int age = sp.getInt("age", 0);
boolean gender = sp.getBoolean("gender", false);
float salary = sp.getFloat("salary", 0.0f);
xml(通过序列化的方式)
序列化
XmlSerializer serializer = Xml.newSerializer();
OutputStream os;
try {
os = openFileOutput("provinces.xml", MODE_PRIVATE);
serializer.setOutput(os, "utf-8");
serializer.startDocument("utf-8", true);
serializer.startTag(null, "provinces");
serializer.startTag(null, "province");
serializer.attribute(null, "pname", "浙江省");
serializer.startTag(null, "city");
serializer.text("杭州市");
serializer.endTag(null, "city");
serializer.startTag(null, "city");
serializer.text("宁波市");
serializer.endTag(null, "city");
serializer.startTag(null, "city");
serializer.text("温州市");
serializer.endTag(null, "city");
serializer.startTag(null, "city");
serializer.text("台州市");
serializer.endTag(null, "city");
serializer.startTag(null, "city");
serializer.text("绍兴市");
serializer.endTag(null, "city");
serializer.endTag(null, "province");
serializer.startTag(null, "province");
serializer.attribute(null, "pname", "湖北省");
serializer.startTag(null, "city");
serializer.text("武汉市");
serializer.endTag(null, "city");
serializer.startTag(null, "city");
serializer.text("荆州市");
serializer.endTag(null, "city");
serializer.endTag(null, "province");
serializer.endTag(null, "provinces");
serializer.endDocument();
os.close();
Toast.makeText(this, "序列化成功", 0).show();
} catch (Exception e) {
e.printStackTrace();
}
pull解析xml
XmlPullParser parser = Xml.newPullParser();
InputStream is = MainActivity.class.getClassLoader()
.getResourceAsStream("provinces.xml");
try {
parser.setInput(is, "utf-8");
int type = parser.getEventType();
List<Province> provinces = null;
List<City> cities = null;
Province p = null;
City c = null;
while (type != XmlPullParser.END_DOCUMENT) {
String tagname = parser.getName();
switch (type) {
case XmlPullParser.START_TAG:
if ("provinces".equals(tagname)) {
provinces = new ArrayList<Province>();
} else if ("province".equals(tagname)) {
p = new Province();
p.setPname(parser.getAttributeValue(0));
cities = new ArrayList<City>();
p.setCities(cities);
provinces.add(p);
} else if ("city".equals(tagname)) {
c = new City();
c.setCname(parser.nextText());
cities.add(c);
}
break;
default:
break;
}
type = parser.next();
}
tv_info.setText(provinces.toString());
} catch (Exception e) {
e.printStackTrace();
}
数据库
存储到数据库,不展开了。
服务器
可以把有些数据存储到服务器上,比如跟用户绑定的一些收藏,评论等数据