Android文件读写操作方法总结

  

<强> Android文件读写操作方法总结

  

在Android中的文件放在不同位置,它们的读取方式也有一些不同。
  

  

本文对android中对资源文件的读,取数据区文件的读取,SD卡文件的读取及RandomAccessFile的方式和方法进行了整理。供参考。

  

<强>一、资源文件的读取:
  

  

,,,,,1)从资源的原始中读取文件数据:
  

        字符串res=" ";   尝试{//得到资源中原始的数据流   InputStream的=getresource () .openRawResource (R.raw.test);//得到数据的大小   int长度=in.available ();      缓冲区byte[]=新的字节(长度);//读取数据   in.read(缓冲);//依用法的编码类型选择合适的编码,如果不调整会乱码   res=EncodingUtils。getString(缓冲区,“繁体”);//关闭   in.close ();      }捕捉(异常e) {   e.printStackTrace ();   }   之前      

, 2)从资源的资产中读取文件数据
  

        字符串文件名=坝梅ā?//文件名字   字符串res=" ";   尝试{//得到资源中的资产数据流   .getAssets InputStream的=getresource () () .open(文件名);      int长度=in.available ();   缓冲区byte[]=新的字节(长度);      in.read(缓冲);   in.close ();   res=EncodingUtils。getString(缓冲区,“utf - 8”);      }捕捉(异常e) {      e.printStackTrace ();      }   之前      

<强>二,读写/数据/数据/& lt;应用程序名祝辞目录上的文件:
  

     //写数据   公共空间writeFile(文件名的字符串,字符串writestr)抛出IOException {   尝试{      FileOutputStream输出信号=openFileOutput(文件名,MODE_PRIVATE);      byte[]字节=writestr.getBytes ();      fout.write(字节);      fout.close ();   }      捕获(异常e) {   e.printStackTrace ();   }   }//读数据   readFile公共字符串(字符串文件名)抛出IOException {   字符串res=" ";   尝试{   FileInputStream鳍=注意openFileInput(文件名);   int长度=fin.available ();   缓冲区byte[]=新的字节(长度);   fin.read(缓冲);   res=EncodingUtils。getString(缓冲区,“utf - 8”);   fin.close ();   }   捕获(异常e) {   e.printStackTrace ();   }   返回res;      }   之前      

<强>三,读写SD卡中的文件。也就是/mnt/sdcard/目录下面的文件:
  

     //写数据到SD中的文件   公共空间writeFileSdcardFile(文件名字符串,字符串write_str)抛出IOException {   尝试{      FileOutputStream输出信号=new FileOutputStream(文件名);   byte[]字节=write_str.getBytes ();      fout.write(字节);   fout.close ();   }      捕获(异常e) {   e.printStackTrace ();   }   }//读SD中的文件   readFileSdcardFile公共字符串(字符串文件名)抛出IOException {   字符串res=" ";   尝试{   FileInputStream鳍=new FileInputStream(文件名);      int长度=fin.available ();      缓冲区byte[]=新的字节(长度);   fin.read(缓冲);      res=EncodingUtils。getString(缓冲区,“utf - 8”);      fin.close ();   }      捕获(异常e) {   e.printStackTrace ();   }   返回res;   }   之前      

<强>四、使用文件类进行文件的读写:
  

     //读文件   readSDFile公共字符串(字符串文件名)抛出IOException {      文件文件=新文件(文件名);      FileInputStream fis=new FileInputStream(文件);      int长度=fis.available ();      缓冲区byte[]=新的字节(长度);   fis.read(缓冲);      res=EncodingUtils。getString(缓冲区,“utf - 8”);      fis.close ();   返回res;   }//写文件   公共空间writeSDFile(文件名字符串,字符串write_str)抛出IOException {      文件文件=新文件(文件名);      FileOutputStream安全系数=new FileOutputStream(文件);      byte[]字节=write_str.getBytes ();      fos.write(字节);      fos.close ();   }   之前      

<强>五,另外,文件类还有下面一些常用的操作:
  

        字符串名称=File.getName ();//获得文件或文件夹的名称:   字符串parentPath=File.getParent ();//获得文件或文件夹的父目录   字符串路径=File.getAbsoultePath();//绝对路经   字符串路径=File.getPath();//相对路经   File.createNewFile();//建立文件   File.mkDir ();//建立文件夹   File.isDirectory ();//判断是文件或文件夹   文件[]文件=File.listFiles ();//列出文件夹下的所有文件和文件夹名   File.renameTo(桌子);//修改文件夹和文件名   File.delete ();//删除文件夹或文件   

Android文件读写操作方法总结