zoukankan      html  css  js  c++  java
  • res/raw下的资源文件读写

    读取原始格式文件,首先需要调用getResource()函数获得资源对象,然后通过调用资源对象的openRawResource()函数,以二进制流的形式打开指定的原始格式文件。在读取文件结束后,调用close()函数关闭文件流 关于读取原始格式文件的核心代码如下

    Resources resources = this.getResources();
    InputStream inputStream = null;
    try {
     inputStream = resources.openRawResource(R.raw.raw_file);   
     byte[] reader = new byte[inputStream.available()];
     while (inputStream.read(reader) != -1) { 
     }

    自己整理的:


     Resources resources = this.getResources();//在住程序初始化的时候得到资源文件        
     InputStream inputStream = null;  //得到输入流 
         try {
             inputStream = resources.openRawResource(R.raw.raw_file);//以二进制流的形式打开指定的原始格式文件   
             byte[] reader = new byte[inputStream.available()]; //把得到的流数据存放在reader里面
             while (inputStream.read(reader) != -1) { //一直读到最后
             }
             displayView.setText(new String(reader,"utf-8")); //显示在EditText里面
         } catch (IOException e) {
             Log.e("ResourceFileDemo", e.getMessage(), e);
         } finally {    
             if (inputStream != null) {
                 try {
                         inputStream.close(); //关闭输入流
                         }
                             catch (IOException e) { }
             }
         }

  • 相关阅读:
    Eclipse RCP与Spring OSGi:技术详解与最佳实践
    AutoCAD 2016机械设计从入门到精通(第2版)
    中文版CorelDRAW X7平面设计
    神奇的中文版Photoshop CC 2017入门书
    Hadoop实战(第2版)
    1048.判断三角形类型
    1046.求最大值
    1047.素数判定
    1056.最大公约数
    1057.众数
  • 原文地址:https://www.cnblogs.com/fx2008/p/3137981.html
Copyright © 2011-2022 走看看