FileManager

package com.test;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import android.content.Context;
import android.util.Log;

public class FileManager {
	static File defaultCacheDirectory;
	
	private FileManager(){
		
	}	
	private static FileManager mInstance;
	
	static File dest;
	
	public static FileManager getInstance(Context context){
		if(mInstance == null){
			mInstance = new FileManager();
			defaultDirectory = context.getDir("default", Context.MODE_PRIVATE);
		}
		
		return mInstance;
	}
	
	public void newFile(String url, String name){
		URL src = null;
		try {
			src = new URL(url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}  

		dest = new File(defaultCacheDirectory, name);  
		try {
			dest.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void delFolder(String folderPath) {
		try {
			delAllFile(folderPath); // 删除完里面所有内容
			String filePath = folderPath;
			filePath = filePath.toString();
			java.io.File myFilePath = new java.io.File(filePath);
			myFilePath.delete(); // 删除空文件夹
		} catch (Exception e) {
			System.out.println("删除文件夹操作出错");
			e.printStackTrace();
		}
	}
	
	public static void delAllFile(String path) {
		File file = new File(path);
		if (!file.exists()) {
			return;
		}
		if (!file.isDirectory()) {
			return;
		}
		String[] tempList = file.list();
		File temp = null;
		for (int i = 0; i < tempList.length; i++) {
			if (path.endsWith(File.separator)) {
				temp = new File(path + tempList[i]);
			} else {
				temp = new File(path + File.separator + tempList[i]);
			}
			if (temp.isFile()) {
				temp.delete();
			}
			if (temp.isDirectory()) {
				delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
				delFolder(path + "/" + tempList[i]);// 再删除空文件夹
			}
		}
	}
	
	public static void deleteAll(){
		 Log.w("file",defaultCacheDirectory.toString());
		delAllFile(defaultCacheDirectory.toString());
		/*if (!defaultCacheDirectory.exists()) {
            Log.w("file","delete");
         }
		if(defaultCacheDirectory.delete()){
			Log.w("file","delete 111111111");
		}
		else{
			Log.w("file","delete 222222222");
		}*/
	}

}

  

File f=new File("/data/data/com.test/app_default/163.html");

if(f.exists()){
Log.d("tag", "163.html");
}

posted @ 2012-07-27 11:09  cornellbox  阅读(493)  评论(0编辑  收藏  举报