这样就可以实现一些基本的文件操作,目前还没有写关于文件操作比较复杂的Demo,想深入了解的朋友可以看一下GitHub上小米的“ MIUI文件管理器社区开源版“。

今天就先这样,去学习了。

3.JSON数据的解析与封装

今天早早的下班了,写了一个简单的JSON Demo,这里总结一下。

首先是效果图:

android 存储数据根据存储时间进行排序_android

简单起见,解析部分并没有采用解析URL的方式,直接把JSON写到String里,逻辑比较简单,这里就不啰嗦了,上代码:

package com.example.jsondemo;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends Activity {
// private static final String BASE_URL =
// “http://zhoumushui.sinaapp.com/json/”;
private TextView tvMsg;
private TextView tvJson;
private EditText etName;
private EditText etAge;
private String strJson = “”;
private String staffInfo = “”;
private String strJsonRes = “”;
private String strMsg;
private Button btnAdd;
private Button btnJson;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvMsg = (TextView) findViewById(R.id.tvMsg);
tvJson = (TextView) findViewById(R.id.tvJson);
etName = (EditText) findViewById(R.id.etName);
etAge = (EditText) findViewById(R.id.etAge);
btnAdd = (Button) findViewById(R.id.btnAdd);
btnJson = (Button) findViewById(R.id.btnJson);
MsgToJson(); // 封装Json
JsonToMsg(); // 解析Json
}
class onClickListenerImp implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == btnAdd) {
if (strMsg != null && strJson.trim().length() != 0) {
strMsg = strMsg + “,{name:'” + etName.getText().toString()
• “',age:” + etAge.getText().toString() + “}”;
} else {
strMsg = “{staff:[{name:'” + etName.getText().toString()
• “',age:” + etAge.getText().toString() + “}”;
}
Toast.makeText(Main.this, “Add Succcess!”, Toast.LENGTH_SHORT)
.show();
etAge.setText(“”);
etName.setText(“”);
} else if (v == btnJson) {
strJsonRes = “”;
strJsonRes = strMsg + “]}”;
tvJson.setText(strJsonRes);
}
}
}
private void MsgToJson() {
btnAdd.setOnClickListener(new onClickListenerImp());
btnJson.setOnClickListener(new onClickListenerImp());
}
private void JsonToMsg() {
strJson = “{staff:[{name:‘Alex’,age:21},{name:‘Zhou’,age:22},{name:‘Anne’,age:23}],company:‘T-Chip’}”;
staffInfo = “原始数据:\n” + strJson + “\n\n解析之后:\n”;
try {
JSONObject mJsonObject = new JSONObject(strJson);
JSONArray mJsonArray = mJsonObject.getJSONArray(“staff”);
String company = mJsonObject.getString(“company”);
staffInfo = staffInfo + company + "共有 " + mJsonArray.length()
• " 个员工,信息如下:\n";
for (int staffCount = 0; staffCount < mJsonArray.length(); staffCount++) {
// 获取员工
JSONObject staff = mJsonArray.getJSONObject(staffCount);
int staffNo = staffCount + 1;
staffInfo = staffInfo + “序号:” + staffNo + " 姓名: "
• staff.getString(“name”) + " 年龄: "
• staff.getInt(“age”) + “\n”;
}
tvMsg.setText(staffInfo);
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}

布局有点拖沓,其实数据封装部分还可以利用一下解析部分的逻辑。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“fill_parent”
android:layout_height=“fill_parent”
android:orientation=“vertical” >
<TextView
android:id=“@+id/tvMsg”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content” />
<TextView
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:text=“==========================” />
<LinearLayout
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:orientation=“horizontal” >
<EditText
android:id=“@+id/etName”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:hint=“姓名” />
<EditText
android:id=“@+id/etAge”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:hint=“年龄” />
<Button
android:id=“@+id/btnAdd”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“增加” />
<Button
android:id=“@+id/btnJson”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“JSON” />
<TextView
android:id=“@+id/tvJson”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content” />
4.DOM,SAX解析XML
分别通过DOM和SAX解析XML实现存储和读取XML信息。
首先是DOM存储信息到XML文件:
// 判断是否存在SDCard
if(!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)){
return ;
}
// 路径:/sdcard/zhoumushui/android.xml
File file = new File(Environment
.getExternalStorageDirectory().toString()
• File.separator
• “zhoumushui” + File.separator + “android.xml”) ;
// 若目录不存在则创建
if (! file.getParentFile().exists()) {
file.getParentFile().mkdirs() ;
}
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Document doc = null;
doc = builder.newDocument();
// 根标签
Element roottag = doc.createElement(“roottag”) ;
// 次标签
Element infotag = doc.createElement(“infotag”) ;
Element name = doc.createElement(“name”) ;
Element url = doc.createElement(“url”) ;
name.appendChild(doc.createTextNode(MyDOMDemo.this.name.getText()
.toString()));
url.appendChild(doc.createTextNode(MyDOMDemo.this.url.getText()
.toString())) ;
// 次标签有两个子元素name和url
infotag.appendChild(name) ;
infotag.appendChild(url) ;
// 主标签有一个子元素infotag
roottag.appendChild(infotag) ;
doc.appendChild(roottag) ;
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = null;
try {
transformer = transformerFactory.newTransformer();
} catch (TransformerConfigurationException e1) {
e1.printStackTrace();
}
transformer.setOutputProperty(OutputKeys.ENCODING, “UTF-8”) ;
// 定义source和result,从source变形为result:
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(file);
try {
transformer.transform(source, result);
} catch (TransformerException e) {
e.printStackTrace();
}
在Activity中输入内容后,点击Button提交后,在/sdcard/zhoumushui/android.xml文件中显示以下信息:
 <?xml version="1.0" encoding="UTF-8"?> 
zhoumushui
blog.csdn.net/zhoumushui
成功实现了将数据存储到XML文件中,下面看一下怎么用DOM读取XML中的信息:
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
return;
}
File file = new File(Environment.getExternalStorageDirectory()
.toString()
• File.separator
• “zhoumushui”
• File.separator + “android.xml”);
if (!file.exists()) {
return;
}
// 建立DocumentBuilderFactory,以用于取得DocumentBuilder
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
// 通过DocumentBuilderFactory取得DocumentBuilder
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
// 定义Document接口对象,通过DocumentBuilder类进行DOM树的转换操作
Document doc = null;
try {
doc = builder.parse(file); // 读取指定路径的xml文件
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
NodeList nodeList = doc.getElementsByTagName(“infotag”);
// 输出NodeList中第一个子节点中文本节点的内容
for (int x = 0; x < nodeList.getLength(); x++) { // 循环输出节点内容
Element e = (Element) nodeList.item(x); // 取得每一个元素
MyDOMDemo.this.name.setText(e.getElementsByTagName(“name”)
.item(0).getFirstChild().getNodeValue()); // 设置文本
MyDOMDemo.this.email.setText(e.getElementsByTagName(“url”)
.item(0).getFirstChild().getNodeValue()); // 设置文本
}

这样就可以读取到刚刚存储到android.xml文件中的内容了。

下面看一下另外一种解析方式SAX获得xml中内容的处理方法:

Activity中的内容与DOM类似:

if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
return;
}
File file = new File(Environment.getExternalStorageDirectory()
.toString()
• File.separator
• “zhoumushui”
• File.separator + “android.xml”);
if (!file.exists()) {
return;
}
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = null;
MySAX mySax = new MySAX();
try {
parser = factory.newSAXParser();
} catch (Exception e) {
e.printStackTrace();
}
try {
parser.parse(file, mySax);
} catch (Exception e) {
e.printStackTrace();
}
List all = mySax.getAll();
name.setText(all.get(0).getName());
url.setText(all.get(0).getUrl());
MySAX类继承自org.xml.sax.helpers.DefaultHandler :
public class MySAX extends DefaultHandler {
private List all = null;
private String elementName = null;
private LinkMan linkMan = null;
@Override
public void startDocument() throws SAXException {
this.all = new ArrayList();
}
@Override


e.printStackTrace();
}
List all = mySax.getAll();
name.setText(all.get(0).getName());
url.setText(all.get(0).getUrl());
MySAX类继承自org.xml.sax.helpers.DefaultHandler :
public class MySAX extends DefaultHandler {
private List all = null;
private String elementName = null;
private LinkMan linkMan = null;
@Override
public void startDocument() throws SAXException {
this.all = new ArrayList();
}
@Override