扣丁学堂札记第15天文件管理与XML、JSON解析

扣丁学堂笔记第15天文件管理与XML、JSON解析

1.文件管理

私有文件读写与raw文件

写入与读取私有文件代码

MainActivity

package com.example.privatefiletest;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    public void writePrivateFileClick(View v){
    	try {
			OutputStream out = openFileOutput("abc.txt", Context.MODE_PRIVATE);
			String info = "分享私有文件";
			byte[] bytes = info.getBytes();
			out.write(bytes, 0, bytes.length);//写入文本内容
			out.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    public void readPrivateFileClick(View v){
    	try {
			InputStream in = openFileInput("abc.txt");
			byte[] bytes = new byte[1024];
			StringBuffer sb = new StringBuffer();
			int len = -1;
			while((len=in.read(bytes))!=-1){
				sb.append(new String(bytes, 0, len));
			}
			in.close();
			Toast.makeText(this, sb, Toast.LENGTH_LONG).show();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    }
}

activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.privatefiletest.MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="写入私有文件"
        android:onClick="writePrivateFileClick" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignRight="@+id/button1"
        android:layout_below="@+id/button1"
        android:text="读取私有文件"
        android:onClick="readPrivateFileClick" />

</RelativeLayout>
读取raw(原生)文件代码
(注:真机测试读取到的raw文件Toast显示乱码)
public void readRawFileClick(View v) {
		try {
			InputStream in = getResources().openRawResource(R.raw.raw);
			byte[] bytes = new byte[1024];
			StringBuffer sb = new StringBuffer();
			int len = -1;
			while ((len = in.read(bytes)) != -1) {
				sb.append(new String(bytes, 0, len));
			}
			in.close();
			Toast.makeText(this, sb, Toast.LENGTH_LONG).show();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}



私有缓存文件操作

public void writePrivateCacheFileClick(View v){
		try {
			File temp = File.createTempFile("temp", null, getCacheDir());
			FileOutputStream out = new FileOutputStream(temp);
			PrintStream ps = new PrintStream(out);
			ps.print("写入私有缓存文件");
			ps.close();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}


SDCard基本操作


SDCacrd私有文件操作


XML解析_PULL

MainActivity

package com.example.pullparsertest;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.os.Bundle;
import android.util.Xml;
import android.view.View;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	public void pullParserClick(View v) {
		ArrayList<Person> list = parser();
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i));
		}
	}

	private ArrayList<Person> parser() {
		ArrayList<Person> list = new ArrayList<Person>();
		Person p = null;
		// 创建pull解析器
		XmlPullParser pull = Xml.newPullParser();
		InputStream in = getResources().openRawResource(R.raw.person);
		try {
			// 设置要解析的文件夹
			pull.setInput(in, "UTF-8");
			// 获取当前的事件类型
			int eventType = pull.getEventType();


			while (eventType != XmlPullParser.END_DOCUMENT) {

				switch (eventType) {
				case XmlPullParser.START_TAG:
					String tag = pull.getName();
					if ("person".equals(tag)) {
						p = new Person();
						p.personid = Integer.parseInt(pull.getAttributeValue(
								null, "id"));
					}else if("name".equals(tag)){
						p.name = pull.nextText();
					}else if("age".equals(tag)){
						p.age = Integer.parseInt(pull.nextText());
					}else if("sex".equals(tag)){
						p.sex = pull.nextText();
					}else if("phone".equals(tag)){
						p.tel = pull.nextText();
					}
					break;
				case XmlPullParser.END_TAG:
					if ("person".equals(pull.getName())){
						list.add(p);
					}
				}
				// 获取下一事件类型
				eventType = pull.next();
			}

		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			return list;
	}
}

person.xml

<persons>
<person id="1">
	<name>Tom</name>
	<age>18</age>
	<sex>男</sex>
	<phone>12345678900</phone>
</person>
<person id="2">
	<name>Tom2</name>
	<age>18</age>
	<sex>男</sex>
	<phone>12345678901</phone>
</person>
</persons>

Person.java

package com.example.pullparsertest;

public class Person {

	int personid;
	String name;
	int age;
	String sex;
	String tel;
	@Override
	public String toString() {
		return "Person [personid=" + personid + ", name=" + name + ", age="
				+ age + ", sex=" + sex + ", tel=" + tel + "]";
	}
}


JSON解析

生成JSON数据



相关内容推荐