获取文件资源

来源:互联网 发布:python 策略回测框架 编辑:程序博客网 时间:2024/05/19 13:20

当点击读取xml文件时显示

/**
 * 读取资源文件,利用resource*/
package edu.hrben.ResourceFileDemo;

import java.io.IOException;
import java.io.InputStream;

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

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;

public class ResourceFileDemo extends Activity {
    /** Called when the activity is first created. */

/**定义组件*/
 private Button fileBtn;
 private Button xmlBtn;
 private Button clearBtn;
 private TextView textDisply;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

//取得xml中的组件
        fileBtn = (Button)findViewById(R.id.txtFile);
        xmlBtn = (Button)findViewById(R.id.xmlFile);
        clearBtn = (Button)findViewById(R.id.clear);
        textDisply = (TextView)findViewById(R.id.disply);

//给按钮添加事件
        fileBtn.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Resources re = getResources();//获取资源文件对象
    InputStream is = null;
    
    try {
     is = re.openRawResource(R.raw.raw_txt);//以二进制流的形式打开指定原始文件
     byte[] data = new byte[is.available()];//获取读出的有用信息
     while(is.read(data)!=-1){
     }
     String str = new String(data,"GBK");//以gbk的形式存储到字符串str里,确保汉子不会出现乱码
     
     textDisply.setText(str);
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    finally{
     if(is!=null){
      try {
       is.close();//关闭文件流
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
     }
    }
    
   }
         
        });
        xmlBtn.setOnClickListener(new OnClickListener(){
         Resources re = getResources();//获取xml对象
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    XmlPullParser xp = re.getXml(R.xml.people);//获取xml文件解析器
    String nm = "";
    String ag = "";
    String ht = "";
    try {
     while(xp.next()!=XmlPullParser.END_DOCUMENT){//xp.next()获取高等级的解析事件
      //并通过对比确定事件类型
      String people = xp.getName();//获得元素名称
      String name = null;
      String age = null;
      String height = null;
      if(people!=null&&people.equals("person")){
       int count = xp.getAttributeCount();//获得属性数量
       for(int i=0;i<count;i++){
        String arrName = xp.getAttributeName(i);//获取属性名称
        String arrValue = xp.getAttributeValue(i);//通过分析属性名称获得属性值
        if(arrName!=null&&arrName.equals("name")){
         name = arrValue;
        }else if(arrName!=null&&arrName.equals("age")){
         age = arrValue;
        }else if(arrName!=null&&arrName.equals("height")){
         height = arrValue;
        }
       }
      }
      if(name!=null&&age!=null&&height!=null){
       nm = "姓名:"+name+" ,年龄:"+age+" ,身高:"+height+"/n";
       //ag = "姓名:"+name+" 年龄:"+age+" 身高:"+height;
       //ht = "姓名:"+name+" 年龄:"+age+" 身高:"+height;
       
      }
      textDisply.setText(nm);
     }
    } catch (XmlPullParserException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    
    //textDisply.setText(ag);
    //textDisply.setText(ht);
   }
   
  
        });
        clearBtn.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    textDisply.setText("");
   }
         
        });
    }
}