android文件管理

来源:互联网 发布:冰河木马软件下载 编辑:程序博客网 时间:2024/06/05 15:42

Android文件操作个人技术总结(如有误,请轻喷)

Android文件操作是建立在Java IO流的基础上,以下是Java IO流结构草图(网上基本都搜得到这个图):

通过上图,我们能清楚的认识到Java IO流的结构分布,其中各个流的详细用法这里不做过多的解释,我们直接切入正题,通过下列代码讲解安卓文件操作:

一、Android常用存放文件路径及其调用方法

1、从resource中读取文件数据

打开resource下的raw文件夹中的文件(注:raw文件夹一般需要自己建立):

[html] view plain copy
  1. @Override  
  2.     protected void onCreate(Bundle savedInstanceState) {  
  3.         super.onCreate(savedInstanceState);  
  4.         setContentView(R.layout.activity_main);  
  5.         /**  
  6.          * 上下文.getResources().openRawResource(R.raw.aa);  
  7.          * 注:在Activity中上下文可省略*/  
  8.         InputStream inputStream=this.getResources().openRawResource(R.raw.aa);  
  9.     }  

打开resource下的assets文件夹中的文件:

[html] view plain copy
  1. @Override  
  2.     protected void onCreate(Bundle savedInstanceState) {  
  3.         super.onCreate(savedInstanceState);  
  4.         setContentView(R.layout.activity_main);  
  5.         /**上下文.getResources().getAssets().open("cc.text");  
  6.          * 注:在Activity中上下文可省略  
  7.          * 打开资源目录下的assets文件目录  
  8.          * 注:当前目录只能读不能写  
  9.          * */  
  10.         try {  
  11.             InputStream inputStream=this.getResources().getAssets().open("cc.text");  
  12.         } catch (IOException e) {  
  13.             // TODO Auto-generated catch block  
  14.             e.printStackTrace();  
  15.         }  
  16.     }  

2、读写应用程序名目录(/data/data/应用程序名)上的文件

[html] view plain copy
  1. //文件写入  
  2.     public void writeFile(){  
  3.         String info="文件写入";  
  4.         try {  
  5.             //声明文件输出流写入到应用目录上的文件,其中第二个参数是文件操作时的权限,注:在普通类中需上下文调出  
  6.             FileOutputStream fileOutputStream=openFileOutput("ceshi.txt", MODE_PRIVATE);  
  7.             byte[] bytes=info.getBytes();//String类型转成byte类型  
  8.             fileOutputStream.write(bytes);//写入文件  
  9.             fileOutputStream.close();//关闭文件输出流  
  10.         } catch (Exception e) {  
  11.             // TODO Auto-generated catch block  
  12.             e.printStackTrace();  
  13.         }  
  14.     }  

[html] view plain copy
  1. //文件读取  
  2.     public void readFile(){  
  3.         try {  
  4.             FileInputStream fileInputStream=openFileInput("ceshi.txt");//声明文件输入流读取应用目录上的文件  
  5.             int lenth=fileInputStream.available();//获取文件长度  
  6.             byte[] bytes=new byte[lenth];//存放读取出的文件内容  
  7.             fileInputStream.read(bytes);//读取文件  
  8.             String result=EncodingUtils.getString(bytes, "UTF-8");//byte类型转成String类型并转码  
  9.             fileInputStream.close();//关闭文件输入流  
  10.         } catch (Exception e) {  
  11.             // TODO Auto-generated catch block  
  12.             e.printStackTrace();  
  13.         }  
  14.     }  

3、读写SD卡中的文件

只需修改上述代码文件输入输出流声明部分的代码

[html] view plain copy
  1. /**  
  2.  * 文件写入  
  3. * Environment.getExternalStorageDirectory()是android4.0以后的的SD卡路径  
  4.  * */  
  5. File file=new File(Environment.getExternalStorageDirectory()+"/ceshi.txt");  
  6. FileOutputStream fileOutputStream=new FileOutputStream(file);  
[html] view plain copy
  1. /**  
  2.  * 文件读取  
  3. * Environment.getExternalStorageDirectory()是android4.0以后的的SD卡路径  
  4.  * */  
  5. File file=new File(Environment.getExternalStorageDirectory()+"/ceshi.txt");  
  6. FileInputStream fileInputStream=new FileInputStream(file);  
4、读写其他路径的文件

只需修改文件输入输出实例化的文件路径

[html] view plain copy
  1. //文件写入  
  2. File file=new File("/ceshi.txt");  
  3. FileOutputStream fileOutputStream=new FileOutputStream(file);  
[html] view plain copy
  1. //文件读取  
  2. File file=new File("/ceshi.txt");  
  3. FileInputStream fileInputStream=new FileInputStream(file);  
5、其他方法的文件操作

[html] view plain copy
  1. public String readFile(){  
  2.     String string="";  
  3.     try {  
  4.         File file=new File(Environment.getExternalStorageDirectory()+"/ceshi.txt");  
  5.         FileInputStream fileInputStream=new FileInputStream(file);  
  6.         InputStreamReader inputStreamReader=new InputStreamReader(fileInputStream);  
  7.         BufferedReader bufferedReader=new BufferedReader(inputStreamReader);  
  8.         String line="";  
  9.         while((line=bufferedReader.readLine())!=null){  
  10.             string+=line+"\n";  
  11.         }  
  12.         bufferedReader.close();  
  13.         inputStreamReader.close();  
  14.         fileInputStream.close();  
  15.     } catch (Exception e) {  
  16.         e.printStackTrace();  
  17.     }  
  18.     return string;  
  19. }  
[html] view plain copy
  1. public String readFile(){  
  2.     String string="";  
  3.     try {  
  4.         File file=new File(Environment.getExternalStorageDirectory()+"/ceshi.txt");  
  5.         FileInputStream fileInputStream=new FileInputStream(file);  
  6.         int length=fileInputStream.available();  
  7.         ByteArrayOutputStream outputStream=new ByteArrayOutputStream();//建立缓存流存放文件内容  
  8.         byte[] bytes=new byte[length];  
  9.         int len=0;  
  10.         if(fileInputStream!=null){  
  11.             while((len=fileInputStream.read(bytes))!=-1){  
  12.                 outputStream.write(bytes, 0, len);  
  13.             }  
  14.         }  
  15.         //string=EncodingUtils.getString(outputStream.toByteArray(), "UTF-8");//转码  
  16.         string=outputStream.toString();  
  17.         outputStream.close();  
  18.         fileInputStream.close();  
  19.     } catch (Exception e) {  
  20.         e.printStackTrace();  
  21.     }  
  22.     return string;  
  23. }  
0 0