在Android中根据文件位置的不同,可分为四种文件读取方式,具体如下;


//方法:从resource中的raw文件夹中获取文件并读取数据,注意:只能读取不能写入数据

 public String getFromRaw(int fileId) {    InputStream in = null;    String result = "";    ByteArrayOutputStream baos=null;    try {    // 获取Resources资源文件流    in = getResources().openRawResource(fileId);    // 文件大小    int length = in.available();    // 创建byte数组    byte[] buf = new byte[length];    // 创建内存流加快效率    baos = new ByteArrayOutputStream();    int count = 0;    while ((count = in.read(buf)) != -1) {    baos.write(buf, 0, count);    }        result=new String(baos.toByteArray(), "UTF-8");    //result = EncodingUtils.getString(buf, "UTF-8");    } catch (IOException e) {    Toast.makeText(this, "找不到文件", 2000).show();    } finally{    if(baos!=null)    try {    baos.close();    } catch (IOException e) {    e.printStackTrace();    }    if(in!=null)    try {    in.close();    } catch (IOException e) {    e.printStackTrace();    }    }    return result;    }


//方法:从asset中获取文件并读取数据,只能读取不能写入数据
 public String getFromAsset(String fileName){//fileName="read_asset.txt"        InputStream in = null;    String result = "";    ByteArrayOutputStream baos=null;                try{            in = getAssets().open(fileName);            int size = in.available();                        byte[] buffer = new byte[size];            baos = new ByteArrayOutputStream();                        while(in.read(buffer)!=-1){            baos.write(buffer);            }            // Convert the buffer into a string.         //   result = new String(baos.toByteArray(),"UTF-8");              result = EncodingUtils.getString(baos.toByteArray(), "UTF-8");            }catch(IOException e){            Toast.makeText(this, "找不到文件", 2000).show();            } finally{    if(baos!=null)    try {    baos.close();    } catch (IOException e) {    e.printStackTrace();    }    if(in!=null)    try {    in.close();    } catch (IOException e) {    e.printStackTrace();    }    }            return result;       }

//方法:向指定系统文件中写入指定的数据
public void writeFileData(String fileName, String message) {FileOutputStream fos = null;try {fos = openFileOutput(fileName, Context.MODE_PRIVATE);byte[] bytes = message.getBytes();fos.write(bytes);fos.flush();} catch (IOException e) {Toast.makeText(this, "文件写入错误", 2000).show();} finally {if (fos != null) {try {fos.close();} catch (IOException e) {e.printStackTrace();}}}}

//方法:打开指定系统文件读取其数据,
 public String readFileData(String fileName){    FileInputStream fis=null;        String result="";        try{        fis = openFileInput(fileName);        int size = fis.available();        byte [] buffer = new byte[size];        fis.read(buffer);        result=new String(buffer,"UTF-8");        }catch(IOException e){        Toast.makeText(this, "文件没有找到", 2000).show();        }finally{        if(fis!=null){        try {fis.close();} catch (IOException e) {e.printStackTrace();}        }        }        return result;//返回读到的数据字符串       }    


ok,下写到这里,待续

更多相关文章

  1. mybatisplus的坑 insert标签insert into select无参数问题的解决
  2. python起点网月票榜字体反爬案例
  3. NPM 和webpack 的基础使用
  4. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  5. 读取android手机流量信息
  6. android 使用html5作布局文件: webview跟javascript交互
  7. 《Android开发从零开始》——25.数据存储(4)
  8. Android(安卓)多媒体扫描过程(Android(安卓)Media Scanner Proces
  9. Android系统配置数据库注释(settings.db)

随机推荐

  1. Android从服务器上下载文件
  2. Android实践 -- 监听应用程序的安装、卸
  3. Android(安卓)ViewPager引导页
  4. /bin/bash: bison: command not found
  5. Android触摸事件(三)-触摸事件类使用实例
  6. android典型代码系列(二十一)------根据
  7. Android(安卓)解锁屏启动过程
  8. Android中的BroadCast简单使用
  9. nfs: server 192.168.0.3 not responding
  10. android添加hid设备驱动