//点击按钮1,使用MODE_PRIVATE模式,生成一个私有的文件
public void click1(View v){
try {
FileOutputStream fos = openFileOutput("private.txt", MODE_PRIVATE);
fos.write("private".getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//点击按钮2,使用MODE_PRIVATE模式,生成一个appent文件
public void click2(View v){
try {
FileOutputStream fos = openFileOutput("appent.txt", MODE_PRIVATE);
fos.write("appent".getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//点击按钮3,使用MODE_WORLD_READABLE模式,生成一个readable文件
public void click3(View v){
try {
FileOutputStream fos = openFileOutput("readable.txt", MODE_WORLD_READABLE);
fos.write("readable".getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//点击按钮4,使用MODE_WORLD_WRITEABLE模式,生成一个write文件
public void click4(View v){
try {
FileOutputStream fos = openFileOutput("writeable.txt", MODE_WORLD_WRITEABLE);
fos.write("writeable".getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
String sdPath = Environment.getExternalStorageDirectory().getPath() ;
File file = new File(sdPath + "writeable.txt");
try {
//File file = new File("/data/data/com.test.filereadwrite/files/readable.txt");
File file = new File("/data/data/com.test.filereadwrite/files/writeable.txt");
FileInputStream fis = new FileInputStream(file);
BufferedReader bffr = new BufferedReader(new InputStreamReader(fis));
String content = bffr.readLine();
Toast.makeText(MainActivity.this, content, 2000).show();
} catch (Exception e) {
e.printStackTrace();
}
String path = Context.getFilesDir().getPath();
File file = new File(path + "writeable.txt");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化我们要备份的数据
smsLists = new ArrayList<Sms>();
//插入测试数据
for (int i = 0; i < 10; i++) {
Sms sms = new Sms();
sms.setAddress("10086"+i);
sms.setBody("你好"+i);
sms.setDate("201"+i);
//把sms对象加入到集合中
smsLists.add(sms);
}
}
//点击按钮button1,通过StringBuffer的方式生成一个xml文件
public void click1(View v){
//1创建一个StringBuffer对象
StringBuffer sb = new StringBuffer();
//开始组拼xml文件头
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
//开始组拼xml根节点
sb.append("<smss>");
//开始组拼sms节点以及子节点
for (Sms sms : smsLists) {
sb.append("<sms>");
//组拼addressa节点
sb.append("<address>");
sb.append(sms.getAddress());
sb.append("</address>");
//组拼body节点
sb.append("<body>");
sb.append(sms.getBody());
sb.append("</body>");
//组拼date节点
sb.append("<date>");
sb.append(sms.getDate());
sb.append("</date>");
sb.append("</sms>");
}
//smss节点结束
sb.append("</smss>");
//把数据保存到SD卡中
try {
File file = new File(Environment.getExternalStorageDirectory().getPath(),"smsbackup.xml");
FileOutputStream fos = new FileOutputStream(file);
fos.write(sb.toString().getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//点击按钮button2,通过XmlSerializer的方式生成xml文件
public void click2(View v){
try {
//获取XmlSerializer类的实例 通过xml这个工具类去获取
XmlSerializer serializer = Xml.newSerializer();
//设置XmlSerializer序列化参数
File file = new File(Environment.getExternalStorageDirectory().getPath(),"smsbackup.xml");
FileOutputStream fos = new FileOutputStream(file);
serializer.setOutput(fos,"utf-8");
//开始写xml文档开头
serializer.startDocument("utf-8", true);
//写xml的根节点 namespace 命名空间
serializer.startTag(null, "smss");
//遍历sms节点以及子节点
for (Sms sms : smsLists) {
serializer.startTag(null, "sms");
//组拼addressa节点
serializer.startTag(null, "address");
serializer.text(sms.getAddress());
serializer.endTag(null, "address");
//组拼body节点
serializer.startTag(null, "body");
serializer.text(sms.getBody());
serializer.endTag(null, "body");
//组拼date节点
serializer.startTag(null, "date");
serializer.text(sms.getDate());
serializer.endTag(null, "date");
serializer.endTag(null, "sms");
}
serializer.endTag(null, "smss");
//結束xml結尾
serializer.endDocument();
//关闭流
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
//显示数据信息
TextView tv_sms = (TextView) findViewById(R.id.tv_weather);
//通过上下文 获取资产的管理者
InputStream in = getAssets().open("smss.xml");
//调用我们定义 解析xml的业务方法
List<Sms> smsLists = SmsParser.xmlParser(in);
StringBuffer sb = new StringBuffer();
for (Sms sms : smsLists) {
sb.append(sms.toString());
}
//把数据显示到TextView上
tv_sms.setText(sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有