遍历SD卡及删除、重命名文件

来源:互联网 发布:c语言课程设计报告 编辑:程序博客网 时间:2024/06/05 15:35

 遍历SD卡:

 

 

Java代码  收藏代码
  1. package com.hilary;  
  2.   
  3. import java.io.File;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10.   
  11. /** 
  12.  * @Author: hilary 
  13.  * @Date: 2011-6-25 
  14.  *  
  15.  * 遍历SD卡的文件 
  16.  **/  
  17. public class MySD extends Activity {  
  18.     private Button btn;  
  19.       
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         // TODO Auto-generated method stub  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main);  
  25.           
  26.         btn  = (Button) findViewById(R.id.btn1);  
  27.         btn.setOnClickListener(new OnClickListener() {  
  28.               
  29.             @Override  
  30.             public void onClick(View v) {  
  31.                 /* 手机的SD卡目录都是在sdcard目录下,所以要得到SD卡的所有文件,就要从/sdcard目录查起 */  
  32.                 getAllFiles(new File("/sdcard"));  
  33.             }  
  34.         });  
  35.     }  
  36.     /* 遍历接收一个文件路径,然后把文件子目录中的所有文件遍历并输出来  */  
  37.     private void getAllFiles(File root){  
  38.         File files[] = root.listFiles();  
  39.         if(files != null){  
  40.             for (File f : files){  
  41.                 if(f.isDirectory()){  
  42.                     getAllFiles(f);  
  43.                 }else{  
  44.                     System.out.println(f);  
  45.                 }  
  46.             }  
  47.         }  
  48.     }  
  49.       
  50. }  

 在遍历SD卡的时候需要得到操作SD卡的权限:

Java代码  收藏代码
  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  

 

在SD卡中创建文件夹及文件:

 

 

Java代码  收藏代码
  1. /* 创建SD对象  下面的两个对象是一个含意,只是名称不一样 */  
  2.         File file = new File("/sdcard/text.txt");  
  3.         File path = new File("/sdcard/ck");  
  4.         /* 判断file文件是否存在,如果不存在则创建file文件 */  
  5.         if(!file.exists()){  
  6.             try {  
  7.                 file.createNewFile();  
  8.                 System.out.println("text.txt 文件创建成功!");  
  9.             } catch (IOException e) {  
  10.                 e.printStackTrace();  
  11.             }  
  12.         }  
  13.         /* 判断path文件夹是否存在,如果不存在则创建path文件夹 */  
  14.         if(!path.exists()){  
  15.             path.mkdirs();  
  16.             System.out.println("ck 文件夹创建成功");  
  17.         }  

 

 

删除及重命名:

 

Java代码  收藏代码
  1. File file = new File("/sdcard/text.txt");  
  2.         File newFile = new File("/sdcard/text2.txt");  
  3.         File path = new File("/sdcard/ck");  
  4.         /* 删除文件及文件夹 */  
  5.         if(file.exists()){  
  6.             file.delete();  
  7.         }  
  8.         if(path.exists()){  
  9.             path.delete();  
  10.         }  
  11.         /* 给文件重命名 */  
  12.         if(file.exists()){  
  13.             file.renameTo(newFile);  
  14.         } 
0 0
原创粉丝点击