admin管理员组

文章数量:1561870

/** 
* @Title: DeleteFile.java
* @Description: 文件、文件夹操作类
* @author MZH
* @version 1.0 一般自己的工具辅助类,我个人习惯加版本号,得考虑到后期维护,方便标记
*/
public class FileUtils{


/**
* 一般情况下,小马希望大家养成好习惯,构造方法,一般留个空的,方便扩展
*/
public FileUtils(){

}


/**
* 删除文件
* @param file
* @return
*/
public boolean deleteFile(File file) {
boolean result = false;
if (file != null) {
try {
File file2 = file;
file2.delete();
result = true;
} catch (Exception e) {
e.printStackTrace();
result = false;
}
}
return result;
}

/**
* 删除文件夹
* @param folder
* @return
* 因为涉及到一个文件夹下有文件也有文件夹的情况,所以写个函数,以后直拿来用
*/
public boolean deleteFolder(File folder) {
boolean result = false;
try {
//取得文件下所有信息:包括文件及文件夹名称
String childs[] = folder.list();
//小马的错误:是字符串数组,没习惯判断length大小,希望朋友们不要忘了判断长度,养成好的习惯
if (childs == null || childs.length <= 0) {
if (folder.delete()) {
result = true;
}
} else {
for (int i = 0; i < childs.length; i++) {
String childName = childs[i];
String childPath = folder.getPath() + File.separator
+ childName;
File filePath = new File(childPath);
//小马的错误:只判断是否存在,未判断是文件还是文件夹,这样会漏掉一大
//部分文件的操作,所以isFile一定要加
if (filePath.exists() && filePath.isFile()) {
if (filePath.delete()) {
result = true;
} else {
result = false;
break;
}
//如果是文件夹的操作,此及写递归删除,调用自己
} else if (filePath.exists() && filePath.isDirectory()) {
if (deleteFolder(filePath)) {
result = true;
} else {
result = false;
break;
}
}
}
folder.delete();
}
} catch (Exception e) {
e.printStackTrace();
result = false;
}
return result;
}

/**
* 实现清空所有Temp文件
*
* @return 是否删除成功
*/
private boolean deleteTempFiles() {
//ApplicationData myApp = new ApplicationData();
//这一步是小马犯的错误,因为继承Application的类是可以这样new对象,
//但不建议直接new,多分内存,而且还是全局的,浪费

//此处直接写getApplication会报错,因为需要上下文件,
//所以这时小马说的扩展构造方法就有用了,可以在new本类对象时会了Context对象哦,吼吼
//ApplicationData myApp = (ApplicationData)getApplication();

FileUtils df = new FileUtils();
File file = null;
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
//因为类似临时文件,一般都在最开始创建,最后删除,
//所以小马把建议类似操作放在Application里面操作,取值操作都方便
file = new File(TEMP); //TEMP为临时文件存放的文件夹路径
}
return df.deleteFolder(file);
}


/**
* 保存时将临时文件夹下所有文件移至目标文件夹下
* @param oldLocation 源文件夹
* @param newLocation 目标文件夹
* @throws IOException 异常
* @throws NoSpaceIOException 磁盘空间不足 小马自定义的异常类,可以跳过
*/
public void moveFile(File oldLocation, File newLocation)
throws IOException, NoSpaceIOException {
boolean isMkdirs = false;
File file = new File(newLocation.getPath()+File.separator+"此处拼接文件名");
if(!file.exists()){
isMkdirs = file.mkdirs();
if(isMkdirs){
String childs[] = oldLocation.list();
if(oldLocation != null && oldLocation.exists()){
for(int i=0;i<childs.length;i++){
String childName = childs[i];
String childPath = oldLocation.getPath()+File.separator+childName;
File filePath = new File(childPath);
if(filePath.exists() && filePath.isFile()){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(filePath);
//下面这句中的false是用来判断是否是append追加状态,
//不懂的朋友们可以直接访问:http://developer.android/reference/java/io/File.html
//来查看相关方法参数的具体涵义,吼吼
fos = new FileOutputStream(new File(file.getPath()+File.separator+childName),false);

byte[] buff = new byte[8192];
int readIn ;
//此处注意下:read方法中的参数如果不写的话,会读不到文件内容,
//报异常,小马犯过的低级错误,希望你不要犯啦
while((readIn = fis.read(buff,0,buff.length))!= -1){
fos.write(buff,0,readIn);
}
fos.flush();
} catch (Exception e) {
e.printStackTrace();
/*if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
// 如果有sdcard,磁盘空间是否满了
Log.d(TAG, "DMUtils>>>sdcard is ok!");
// No space left on device
if (ex.getMessage() != null
&& ex.getMessage().indexOf(
"No space left on device") != -1) {
// 磁盘空间满
throw new NoSpaceIOException(ex.getMessage());
}
} else {
// 文件保存失败应该检查sd卡
Log.d(TAG, "DMUtils>>>sdcard is error!state: "
+ android.os.Environment.getExternalStorageState());
}
throw new IOException(ex.getMessage()
+ " IOException when transferring "
+ oldLocation.getPath() + " to "
+ newLocation.getPath());*/
}finally{
//大伙应该很熟悉了吧?小马一朋友犯过的错误,未关闭流,
//导致生成的文件本来是KB的却成了MB的文件,所以一定注意打开什么就关闭什么
try {
if(fis!=null){
fos.close();
}
if(fos!=null){
fos.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
/*
* 因为此处小马用的是一个文件夹下的文件操作,并不包含子文件夹,
* 所以:如果有朋友要用子文件夹操作的话可激活此处递归处理哦
*/
/*else if(filePath.exists() && filePath.isDirectory()){
try {
moveFile(filePath,new File("此处路径从全局文件中取就OK了"));
} catch (Exception e) {
e.printStackTrace();
}
}*/
}
}
}
}
}

}

本文标签: 批量操作file