现在的位置: 首页 > 综合 > 正文

android的文件操作

2013年02月15日 ⁄ 综合 ⁄ 共 2523字 ⁄ 字号 评论关闭

http://blog.csdn.net/fenghome/article/details/5668598

android的文件操作要有权限:

[xhtml] view
plain
copy

  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>  

 

SD卡下的文件操作:

1、判断SD卡是否插入

[java] view
plain
copy

  1. Environment.getExternalStorageState().equals(  
  2.         android.os.Environment.MEDIA_MOUNTED);  

2、获得sd卡根目录:

[java] view
plain
copy

  1. File skRoot = Environment.getExternalStorageDirectory();  

私有目录下的文件操作:

1、获得私有根目录:

[java] view
plain
copy

  1. File fileRoot = Context.getFilesDir()+"//";  

还未整理

 

文件夹或文件夹操作:

1、确定或获得文件夹和文件路径

a、获得文件或文件夹的绝对路径和相对路径。区别

[java] view
plain
copy

  1. String path = File.getPath();//相对  
  2. String path = File.getAbsoultePath();//绝对  

b 、获得文件或文件夹的父目录

[java] view
plain
copy

  1. String parentPath = File.getParent();  

c、获得文件或文件夹的名称:

[java] view
plain
copy

  1. String Name = File.getName();  

2、建立文件或文件夹

[java] view
plain
copy

  1. File.mkDir(); //建立文件夹  
  2. File.createNewFile();//建立文件  

3、判断是文件或文件夹

[java] view
plain
copy

  1. File.isDirectory()  

4、列出文件夹下的所有文件和文件夹名

[java] view
plain
copy

  1. File[] files = File.listFiles();  

5、修改文件夹和文件名

[java] view
plain
copy

  1. File.renameTo(dest);  

6、删除文件夹或文件

[java] view
plain
copy

  1. File.delete();  

 

 

[java] view
plain
copy

  1. package otheri.common;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8. import java.io.OutputStream;  
  9.   
  10. import otheri.io.Input;  
  11. import otheri.io.Output;  
  12. import android.content.Context;  
  13. import android.os.Environment;  
  14.   
  15. public class FileHelper {  
  16.     private Context context;  
  17.     private String SDPATH;  
  18.     private String FILESPATH;  
  19.   
  20.     public FileHelper(Context context) {  
  21.         this.context = context;  
  22.         SDPATH = Environment.getExternalStorageDirectory().getPath() + "//";  
  23.         FILESPATH = this.context.getFilesDir().getPath() + "//";  
  24.     }  
  25.   
  26.     /** 
  27.      * 在SD卡上创建文件 
  28.      *  
  29.      * @throws IOException 
  30.      */  
  31.     public File creatSDFile(String fileName) throws IOException {  
  32.         File file = new File(SDPATH + fileName);  
  33.         file.createNewFile();  
  34.         return file;  
  35.     }  
  36.   
  37.     /** 
  38.      * 删除SD卡上的文件 
  39.      *  
  40.      * @param fileName 
  41.      */  
  42.     public boolean delSDFile(String fileName) {  
  43.         File file = new File(SDPATH + fileName);  
  44.         if (file == null || !file.exists() || file.isDirectory())  
  45.             return false;  
  46.         file.delete();  
  47.         return true;  
  48.     }  
  49.   
  50.     /** 
  51.      * 在SD卡上创建目录 
  52.      *  
  53.      * @param dirName 
  54.      */  
  55.     public File creatSDDir(String dirName) {  
  56.         File dir = new File(SDPATH + dirName);  
  57.         dir.mkdir();  

抱歉!评论已关闭.