无标题文档
人多不足以依赖,要生存只有靠自己。
      深窥自己的心,而后发觉一切的奇迹在你自己。
          凡事皆有终结,因此,耐心是赢得成功的一种手段。

android 文件读取(assets、raw)

      

  

    需要注意的是,来自Resources和Assets 中的文件只可以读取而不能进行写的操作。

    assets文件夹里面的文件都是保持原始的文件格式,需要用AssetManager以字节流的形式读取文件。
      1. 先在Activity里面调用getAssets() 来获取AssetManager引用。
      2. 再用AssetManager的open(String fileName, int accessMode) 方法则指定读取的文件以及访问模式就能得到输入流InputStream。 
      3. 然后就是用已经open file 的inputStream读取文件,读取完成后记得inputStream.close() 。
      4.调用AssetManager.close() 关闭AssetManager。
 
 
 
从assets 文件夹中获取文件并读取数据
//从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;
}
}

 

 

public String getFromRaw(){ 
            try { 
                InputStreamReader inputReader = new InputStreamReader( getResources().openRawResource(R.raw.test1));
                BufferedReader bufReader = new BufferedReader(inputReader);
                String line="";
                String Result="";
                while((line = bufReader.readLine()) != null)
                    Result += line;
                return Result;
            } catch (Exception e) { 
                e.printStackTrace(); 
            }             
    } 

 

转  http://www.cnblogs.com/greatverve/archive/2012/03/08/android-assets.html

 

posted @ 2013-11-28 16:23  酷玩时刻  阅读(3689)  评论(0编辑  收藏  举报
友情链接:快递查询 快递查询