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

  • 相关阅读:
    Guns项目整体结构
    基于事件的NIO多线程服务器
    Reactor模式和NIO
    ConcurrentHashMap之实现细节
    C 语言的前世今生
    Netty系列之Netty高性能之道
    java synchronized详解
    生产者/消费者模式
    当spring 容器初始化完成后执行某个方法
    Linux系统管理员需要知道的16个服务器监控命令
  • 原文地址:https://www.cnblogs.com/fx2008/p/3137981.html
Copyright © 2011-2022 走看看