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) { }
             }
         }

  • 相关阅读:
    非对称加密的理解
    常见META标签和针对SEO优化的设置
    shadow DOM
    浏览器渲染机制
    设计模式06---生产者消费者模式
    spring04-----Ioc容器实例化Bean
    设计模式05----装饰者模式
    Spring03-----Ioc的配置使用
    设计模式04----原型设计模式(prototype)
    设计模式03------单例模式
  • 原文地址:https://www.cnblogs.com/fx2008/p/3137981.html
Copyright © 2011-2022 走看看