Android递归方式删除某文件夹下的所有文件
2016-01-18 15:47
567 查看
1.由于需要删除文件,因此需要如下权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
2.核心代码
[java] view
plaincopy
package com.example.deleteyoumi;
import java.io.File;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
/*
*@author: ZhengHaibo
*web: http://blog.csdn.net/nuptboyzhb
*mail: zhb931706659@126.com
*2013-4-28 Nanjing,njupt,China
*/
public class DeleteYouMiMainActivity extends Activity implements
OnClickListener {
private Button deleteButton;
private final static String youmiPathString = "/mnt/sdcard/Android/data/.youmicache/";//文件夹的路径
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delete_you_mi_main);
deleteButton = (Button) findViewById(R.id.delete_youmi);
deleteButton.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.delete_you_mi_main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.delete_youmi:
File file = new File(youmiPathString);
DeleteFile(file);
mHandler.sendEmptyMessage(1);
break;
default:
break;
}
}
Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
Toast.makeText(getApplicationContext(), "文件或文件夹不存在", Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(getApplicationContext(), "删除成功!", Toast.LENGTH_LONG).show();
break;
default:
break;
}
};
};
/**
* 递归删除文件和文件夹
*
* @param file
* 要删除的根目录
*/
public void DeleteFile(File file) {
if (file.exists() == false) {
mHandler.sendEmptyMessage(0);
return;
} else {
if (file.isFile()) {
file.delete();
return;
}
if (file.isDirectory()) {
File[] childFile = file.listFiles();
if (childFile == null || childFile.length == 0) {
file.delete();
return;
}
for (File f : childFile) {
DeleteFile(f);
}
file.delete();
}
}
}
}
3.拓展
本博客是以删除为例。当然,对于遍历某文件夹下的所有文件均可用这个方法。如搜索.mp3文件等。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
2.核心代码
[java] view
plaincopy
package com.example.deleteyoumi;
import java.io.File;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
/*
*@author: ZhengHaibo
*web: http://blog.csdn.net/nuptboyzhb
*mail: zhb931706659@126.com
*2013-4-28 Nanjing,njupt,China
*/
public class DeleteYouMiMainActivity extends Activity implements
OnClickListener {
private Button deleteButton;
private final static String youmiPathString = "/mnt/sdcard/Android/data/.youmicache/";//文件夹的路径
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delete_you_mi_main);
deleteButton = (Button) findViewById(R.id.delete_youmi);
deleteButton.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.delete_you_mi_main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.delete_youmi:
File file = new File(youmiPathString);
DeleteFile(file);
mHandler.sendEmptyMessage(1);
break;
default:
break;
}
}
Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
Toast.makeText(getApplicationContext(), "文件或文件夹不存在", Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(getApplicationContext(), "删除成功!", Toast.LENGTH_LONG).show();
break;
default:
break;
}
};
};
/**
* 递归删除文件和文件夹
*
* @param file
* 要删除的根目录
*/
public void DeleteFile(File file) {
if (file.exists() == false) {
mHandler.sendEmptyMessage(0);
return;
} else {
if (file.isFile()) {
file.delete();
return;
}
if (file.isDirectory()) {
File[] childFile = file.listFiles();
if (childFile == null || childFile.length == 0) {
file.delete();
return;
}
for (File f : childFile) {
DeleteFile(f);
}
file.delete();
}
}
}
}
3.拓展
本博客是以删除为例。当然,对于遍历某文件夹下的所有文件均可用这个方法。如搜索.mp3文件等。
相关文章推荐
- Android编程之selector下设置背景属性值的方法
- android:Invalid file name: must contain only [a-z0-9_.]错误
- 开源框架】Android之史上最全最简单最有用的第三方开源库收集整理,有助于快速开发
- Android仿易信界面SlidingMenu左右滑动
- Android SDK开发包国内下载地址
- Android 开发绕不过的坑:你的 Bitmap 究竟占多大内存?
- Android系统之路(初识MTK) ------ 默认第一次开机屏幕亮度/日期格式/picture makeer model
- Android布局中的常用属性小结
- Android 自定义View画‘√’
- Android Fragment avoid non-default constructors in fragments use a default constructor plus fragment
- AndroidJNI 通过C/C++调用JAVA
- Android编程开发之EditText中inputType属性小结
- android smooth-line-chart 贝塞尔曲线入门
- android 在activity onCreate时获取控件高宽
- Android实际开发问题05------杀不死的服务
- 在Android6.0上给外置SD卡增加一个路径
- Android获取设备隐私 忽略6.0权限管理
- Android Menu菜单使用
- android开源系列:CircleImageView自定义圆形控件的使用
- android各种提示Dialog 弹出框