android文件API使用

来源:互联网 发布:lol小兵数据 编辑:程序博客网 时间:2024/05/16 18:10
(一)获取总根
[java] view plain copy
  1. File[] fileList=File.listRoots();  
  2. //返回fileList.length为1  
  3. //fileList.getAbsolutePath()为"/"  
  4. //这就是系统的总根  

(二)打开总根目录

[java] view plain copy
  1. File file=new File("/");  
  2. File[] fileList=file.listFiles();  
  3. //获取的目录中除了"/sdcard"和"/system"还有"/data"、"/cache"、"/dev"等  
  4. //Android的根目录并不像Symbian系统那样分为C盘、D盘、E盘等  
  5. //Android是基于Linux的,只有目录,无所谓盘符  

(三)获取系统存储根目录

[java] view plain copy
  1. File file=Environment.getRootDirectory();//File file=new File("/system");  
  2. File[] fileList=file.listFiles();  
  3. //这里说的系统仅仅指"/system"  
  4. //不包括外部存储的手机存储的范围远远大于所谓的系统存储  

(四)获取SD卡存储根目录

[java] view plain copy
  1. File file=Environment.getExternalStorageDirectory();//File file=new File("/sdcard");  
  2. File[] fileList=file.listFiles();  
  3. //要获取SD卡首先要确认SD卡是否装载  
  4. boolean is=Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);  
  5. //如果true,则已装载  
  6. //如果false,则未装载  

(五)获取data根目录

[java] view plain copy
  1. File file=Environment.getDataDirectory();//File file=new File("/data");  
  2. File[] fileList=file.listFiles();  
  3. //由于data文件夹是android里一个非常重要的文件夹,所以一般权限是无法获取到文件的,即fileList.length返回为0  

(六)获取私有文件路径

[java] view plain copy
  1. Context context=this;//首先,在Activity里获取context  
  2. File file=context.getFilesDir();  
  3. String path=file.getAbsolutePath();  
  4. //此处返回的路劲为/data/data/包/files,其中的包就是我们建立的主Activity所在的包  
  5. //我们可以看到这个路径也是在data文件夹下  
  6. //程序本身是可以对自己的私有文件进行操作  
  7. //程序中很多私有的数据会写入到私有文件路径下,这也是android为什么对data数据做保护的原因之一  

(七)获取文件(夹)绝对路径、相对路劲、文件(夹)名、父目录

[java] view plain copy
  1. File file=……  
  2. String relativePath=file.getPath();//相对路径  
  3. String absolutePath=file.getAbsolutePath();//绝对路径  
  4. String fileName=file.getName();//文件(夹)名  
  5. String parentPath=file.getParent();//父目录  

(八)列出文件夹下的所有文件和文件夹

[java] view plain copy
  1. File file=……  
  2. File[] fileList=file.listFiles();  

(九)判断是文件还是文件夹

[java] view plain copy
  1. File file=……  
  2. boolean is=file.isDirectory();//true-是,false-否  

(十)判断文件(夹)是否存在

[java] view plain copy
  1. File file=……  
  2. boolean is=file.exists();//true-是,false-否  

(十一)新建文件(夹)

[java] view plain copy
  1. File file=……  
  2. oolean is=file.isDirectory();//判断是否为文件夹  
  3. /*方法1*/  
  4. if(is){  
  5.     String path=file.getAbsolutePath();  
  6.     String name="ABC";//你要新建的文件夹名或者文件名  
  7.     String pathx=path+name;  
  8.     File filex=new File(pathx);  
  9.     boolean is=filex.exists();//判断文件(夹)是否存在  
  10.     if(!is){  
  11.         filex.mkdir();//创建文件夹  
  12.         //filex.createNewFile();//创建文件  
  13.     }  
  14. /*方法2*/  
  15. if(is){  
  16.     String path=file.getAbsolutePath();  
  17.     String name="test.txt";//你要新建的文件夹名或者文件名  
  18.     File filex=new File(path,name);//方法1和方法2的区别在于此  
  19.     boolean is=filex.exists();//判断文件(夹)是否存在  
  20.     if(!is){  
  21.         filex.mkdir();//创建文件夹  
  22.         //filex.createNewFile();//创建文件  
  23. }  

(十二)重命名文件(夹)

[java] view plain copy
  1. File file=……  
  2. String parentPath=file.getParent();  
  3. String newName="name";//重命名后的文件或者文件夹名  
  4. File filex=new File(parentPath,newName);//File filex=new File(parentPaht+newName)  
  5. file.renameTo(filex);  

(十三)删除文件(夹)

[java] view plain copy
  1. File file=……  
  2. file.delete();//立即删除  
  3. //file.deleteOnExit();//程序退出后删除,只有正常退出才会删除

     

[java] view plain copy
  1. import java.io.File;  
  2. import java.util.*;  
  3.   
  4. import android.app.Activity;  
  5. import android.content.Context;  
  6. import android.os.*;  
  7. import android.view.*;  
  8. import android.widget.*;  
  9. import android.widget.AdapterView.OnItemClickListener;  
  10. import android.widget.ImageView.ScaleType;  
  11.   
  12. public class FileBrowser extends Activity {  
  13.   
  14.     private ListView mainListView=null;  
  15.     private List<Map<String,Object>> list=null;  
  16.       
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         this.setTitle("文件浏览器");  
  20.         mainListView=new ListView(this);  
  21.         setContentView(mainListView);  
  22.           
  23.         File file=Environment.getRootDirectory();  
  24.         String pathx=file.getAbsolutePath();  
  25.         this.setTitle(pathx);  
  26.         //android的总目录就是"/"  
  27.         list_init("/");  
  28.     }  
  29.       
  30.     void list_init(String path){  
  31.         File file=new File(path);  
  32.         File[] fileList=file.listFiles();  
  33.         list=new ArrayList<Map<String,Object>>();  
  34.         Map<String,Object> item;    
  35.         item=new HashMap<String,Object>();   
  36.         if(path.equals("/")){  
  37.             item.put("ico",R.drawable.home);   
  38.             item.put("name","总目录列表");    
  39.             item.put("path","/");    
  40.             list.add(item);  
  41.         }else{  
  42.             item.put("ico",R.drawable.back);   
  43.             item.put("name","返回上一级");    
  44.             item.put("path",file.getParent());    
  45.             list.add(item);  
  46.         }  
  47.         for(int i=0;i<fileList.length;i++){  
  48.             item=new HashMap<String,Object>();   
  49.             if(fileList[i].isDirectory()){  
  50.                 if(fileList[i].list().length<1){  
  51.                     item.put("ico",R.drawable.file1);  
  52.                 }else{  
  53.                     item.put("ico",R.drawable.file2);  
  54.                 }  
  55.             }else{  
  56.                 item.put("ico",R.drawable.content);   
  57.             }  
  58.             item.put("name",fileList[i].getName());    
  59.             item.put("path",fileList[i].getAbsolutePath());    
  60.             list.add(item);    
  61.         }  
  62.         MyAdapter ma=new MyAdapter(this,list);   
  63.         //mainListView=new ListView(this);  
  64.         mainListView.setAdapter(ma);   
  65.         mainListView.setOnItemClickListener(new OnItemClickListener(){  
  66.             public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {  
  67.                 if(arg2>0 && (Integer)(list.get(arg2).get("ico"))==R.drawable.content){  
  68.                     //非文件夹图标,点击无效  
  69.                 }else{  
  70.                     //打开下一级文件目录列表  
  71.                     list_init((String)(list.get(arg2).get("path")));  
  72.                 }  
  73.             }  
  74.         });  
  75.         this.setTitle(path);  
  76.     }  
  77.       
  78.     public class MyAdapter extends BaseAdapter{    
  79.             
  80.         Context context=null;    
  81.         List<Map<String,Object>> list=null;    
  82.             
  83.         MyAdapter(Context context,List<Map<String,Object>> list){    
  84.             this.context=context;    
  85.             this.list=list;  
  86.         }    
  87.         public int getCount() {return list.size();}    
  88.         public Object getItem(int position) {return position;}    
  89.         public long getItemId(int position) {return position;}    
  90.         
  91.         public View getView(int position, View convertView, ViewGroup parent) {    
  92.             LinearLayout returnView=new LinearLayout(context);    
  93.             returnView.setLayoutParams(new ListView.LayoutParams(-1,-2));//注意:ListView.LayoutParams     
  94.             //图标  
  95.             ImageView iv=new ImageView(context);    
  96.             LinearLayout.LayoutParams lp_iv=new LinearLayout.LayoutParams(-2,-2);      
  97.             lp_iv.rightMargin=10;  
  98.             iv.setLayoutParams(lp_iv);   
  99.             iv.setScaleType(ScaleType.CENTER_INSIDE);    
  100.             iv.setImageResource((Integer)((list.get(position)).get("ico")));    
  101.             returnView.addView(iv);  
  102.             //文件名   
  103.             TextView name=new TextView(context);    
  104.             LinearLayout.LayoutParams lp_tv=new LinearLayout.LayoutParams(-2,-2);   
  105.             name.setLayoutParams(lp_tv);    
  106.             name.setTextSize(name.getTextSize()+10);    
  107.             name.setText((String)(list.get(position).get("name")));  
  108.             returnView.addView(name);  
  109.             //  
  110.             return returnView;    
  111.         }    
  112.         
  113.     }   

/** * * @return 全部的内部空间大小 */public String getInterTotalSpace_G(){    File path = Environment.getDataDirectory();    StatFs statFs = new StatFs(path.getPath());    double t1 = statFs.getBlockSize();    double t2 = statFs.getBlockCount();    double t = t1 * t2 / 1000/ 1000/1024;    return String.format("%.2fG", t);}

/** * * @return 可用的内部空间大小 */public String getInterFreeSpace_G(){    File path = Environment.getDataDirectory();    StatFs statFs = new StatFs(path.getPath());    double t1 = statFs.getAvailableBlocks();    double t2 = statFs.getBlockSize();    double t = t1 * t2 / 1000 / 1000/1024;    return String.format("%.2fG", t);}


原文地址:http://blog.csdn.net/mrlixirong/article/details/6800585

                    http://blog.csdn.net/mrlixirong/article/details/6800073

0 0
原创粉丝点击