zoukankan      html  css  js  c++  java
  • android 读取raw文件

    在Android平台下,除了对应用程序的私有文件夹中的文件进行操作外,还可以从资源文件和 Assets 中获得输入流读取数据,这些文件分别放在应用程序的res/raw 目录和 assets 目录下,这些文件在编译的时候和其他文件一起被打包。

        需要注意的是,来自Resources和Assets 中的文件只可以读取而不能进行写的操作,下面就通过一个例子来说明如何从 Resources 和 Assets中的文件中读取信息。首先分别在res/raw 和 assets 目录下新建两个文本文件 "test1.txt"   和 "test2.txt" 用以读取,结构如下图。



       为了避免字符串转码带来的麻烦,可以将两个文本文件的编码格式设置为UTF-8。设置编码格式的方法有很多种,比较简单的一种是用 Windows 的记事本打开文本文件,在另存为对话框中编码格式选择"UTF-8" ,如下图。



    看一下运行后的效果。

    package xiaohang.zhimeng;

    import java.io.InputStream;
    import org.apache.http.util.EncodingUtils;
    import android.app.Activity;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.widget.TextView;

    public class Activity02 extends Activity{
        
        public static final String ENCODING = "UTF-8";
        TextView tv1;
        TextView tv2;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            tv1 = (TextView)findViewById(R.id.tv1);
            tv1.setTextColor(Color.RED);
            tv1.setTextSize(15.0f);
            tv2 = (TextView)findViewById(R.id.tv2);
            tv2.setTextColor(Color.RED);
            tv2.setTextSize(15.0f);
            tv1.setText(getFromRaw());
            tv2.setText(getFromAssets("test2.txt"));
        }
        
        //从resources中的raw 文件夹中获取文件并读取数据
        public String getFromRaw(){
            String result = "";
                try {
                    InputStream in = getResources().openRawResource(R.raw.test1);
                    //获取文件的字节数
                    int lenght = in.available();
                    //创建byte数组
                    byte[]  buffer = new byte[lenght];
                    //将文件中的数据读到byte数组中
                    in.read(buffer);
                    result = EncodingUtils.getString(buffer, ENCODING);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return result;
        }
        
        //从assets 文件夹中获取文件并读取数据
        public String getFromAssets(String fileName){
            String result = "";
                try {
                    InputStream in = getResources().getAssets().open(fileName);
                    //获取文件的字节数
                    int lenght = in.available();
                    //创建byte数组
                    byte[]  buffer = new byte[lenght];
                    //将文件中的数据读到byte数组中
                    in.read(buffer);
                    result = EncodingUtils.getString(buffer, ENCODING);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return result;
        }
    }

  • 相关阅读:
    Vagrant安装virtualbox
    博客园页面定制CSS
    Node.js安装详细步骤教程(Windows版)
    已完成题目
    20.10.07总结
    纯IPv6主机上搭建网站
    Azure Database for MySQL Connection Security -(3) Private Endpoint
    Azure Database for MySQL Connection Security -(2) VNET rules and service endpoint
    Azure Database for MySQL Connection Security -(1) Public network access and Firewall rules
    python教程:删除列表中某个元素的3种方法
  • 原文地址:https://www.cnblogs.com/seawh411/p/3715644.html
Copyright © 2011-2022 走看看