public class FileUtils {
private static final StringDB_DIR ="db";
private static final StringTEMP_DIR ="temp";
private static final StringDOWNLOAD_DIR ="download";
private static final StringUPLOAD_DIR ="upload";
private static final StringIMAGE ="image";
private static final StringRECORD ="record";
private static final StringVIDEO ="video";
private static final StringAVATAR ="avatar";
/**
* 默认APP根目录.
*/
private static StringrootFileDir =null;
/**
* 默认数据库文件目录.
*/
private static StringdbFileDir =null;
/**
* 默认临时文件目录.
*/
private static StringtempFileDir =null;
/**
* 默认下载文件目录.
*/
private static StringdownLoadFileDir =null;
/**
* 默认上传文件目录.
*/
private static StringupLoadFileDir =null;
private static StringimageFileDir =null;
/**
* 语音存放的路径
*/
private static StringrecordFileDir =null;
private static StringvideoFileDir =null;
private static StringavatarFileDir =null;
private static StringrecorderFileDir =null;
/**
* 初始化基本文件以及当前用户文件
*
* @param context
*/
public static void initFileDir(Context context) {
String packageName = context.getPackageName();
// 默认文件存储根目录.
String rootPath = File.separator + packageName + File.separator;
// 默认DB目录.
String dbPath = File.separator +DB_DIR + File.separator;
// 默认临时文件目录
String tempPath = File.separator +TEMP_DIR + File.separator;
// 默认下载文件目录
String downloadPath = File.separator +DOWNLOAD_DIR + File.separator;
// 默认上传文件目录
String uploadPath = File.separator +UPLOAD_DIR + File.separator;
// 默认聊天图片
String imagePath = File.separator +IMAGE + File.separator;
//默认语音的路径
String recordPath = File.separator +RECORD + File.separator;
//默认视频的路径
String videoPath = File.separator +VIDEO + File.separator;
//头像路径
String avatarPath = File.separator +AVATAR + File.separator;
try {
if (!isCanUseSD()) {
return;
}else {
//在根目录Android下
/* File root = Environment.getExternalStorageDirectory();
File androidRoot = new File(root.getAbsolutePath() + File.separator + "Android");
if (!androidRoot.exists()) {
androidRoot.mkdirs();
}*/
//在根目录的包名下
File root = Environment.getExternalStorageDirectory();
File androidRoot =new File(root.getAbsolutePath() + File.separator + packageName);
if (!androidRoot.exists()) {
androidRoot.mkdirs();
}
File dataRoot =new File(androidRoot.getAbsolutePath() + File.separator +"data");
if (!dataRoot.exists()) {
dataRoot.mkdirs();
}
File rootDirFile =new File(dataRoot.getAbsolutePath() + rootPath);
if (!rootDirFile.exists()) {
rootDirFile.mkdirs();
}
File filesRoot =new File(rootDirFile.getAbsolutePath() + File.separator +"files");
if (!filesRoot.exists()) {
filesRoot.mkdirs();
}
rootFileDir = filesRoot.getPath();
File dbDirFile =new File(filesRoot.getAbsolutePath() + dbPath);
if (!dbDirFile.exists()) {
dbDirFile.mkdirs();
}
dbFileDir = dbDirFile.getPath();
File tempDirFile =new File(filesRoot.getAbsolutePath() + tempPath);
if (!tempDirFile.exists()) {
tempDirFile.mkdirs();
}
tempFileDir = tempDirFile.getPath();
File downloadDirFile =new File(filesRoot.getAbsolutePath() + downloadPath);
if (!downloadDirFile.exists()) {
downloadDirFile.mkdirs();
}
downLoadFileDir = downloadDirFile.getPath();
File uploadDirFile =new File(filesRoot.getAbsolutePath() + uploadPath);
if (!uploadDirFile.exists()) {
uploadDirFile.mkdirs();
}
upLoadFileDir = uploadDirFile.getPath();
File imageDirFile =new File(filesRoot.getAbsolutePath() + imagePath);
if (!imageDirFile.exists()) {
imageDirFile.mkdir();
}
imageFileDir = imageDirFile.getPath();
File recordDirFile =new File(filesRoot.getAbsolutePath() + recordPath);
if (!recordDirFile.exists()) {
recordDirFile.mkdir();
}
recordFileDir = recordDirFile.getPath();
File videoDirFile =new File(filesRoot.getAbsolutePath() + videoPath);
if (!videoDirFile.exists()) {
videoDirFile.mkdir();
}
videoFileDir = videoDirFile.getPath();
File avatarDirFile =new File(filesRoot.getAbsolutePath() + avatarPath);
if (!avatarDirFile.exists()) {
avatarDirFile.mkdir();
}
avatarFileDir = avatarDirFile.getPath();
}
}catch (Exception e) {
e.printStackTrace();
}
}
/**
* 描述:SD卡是否能用.
*
* @return true 可用,false不可用
*/
public static boolean isCanUseSD() {
try {
return Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);
}catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 获取默认APP根目录路径
*
* @param context
* @return
*/
public static String getRootFileDir(Context context) {
if (rootFileDir ==null) {
initFileDir(context);
}
return rootFileDir;
}
/**
* 录音者签名
*/
public static String getRecorder(Context context) {
if (recorderFileDir ==null) {
initFileDir(context);
}
return recorderFileDir;
}
/**
* 获取数据库文件路径
*
* @param context
* @return
*/
public static String getDbFileDir(Context context) {
if (dbFileDir ==null) {
initFileDir(context);
}
return dbFileDir;
}
/**
* 获取临时文件路径
*
* @param context
* @return
*/
public static String getTempFileDir(Context context) {
if (tempFileDir ==null) {
initFileDir(context);
}
return tempFileDir;
}
/**
* 获取下载文件路径
*
* @param context
* @return
*/
public static String getDownloadFileDir(Context context) {
if (downLoadFileDir ==null) {
initFileDir(context);
}
return downLoadFileDir;
}
/**
* 获取下载文件路径
*
* @param context
* @return
*/
public static String getUploadFileDir(Context context) {
if (upLoadFileDir ==null) {
initFileDir(context);
}
return upLoadFileDir;
}
public static String getImageFileDir(Context context) {
if (imageFileDir ==null) {
initFileDir(context);
}
return imageFileDir;
}
public static String getRecordFileDir(Context context) {
if (recordFileDir ==null) {
initFileDir(context);
}
return recordFileDir;
}
public static String getVideoFileDir(Context context) {
if (videoFileDir ==null) {
initFileDir(context);
}
return videoFileDir;
}
public static String getAvatarFileDir(Context context) {
if (avatarFileDir ==null) {
initFileDir(context);
}
return avatarFileDir;
}
public static String getExtension(File file) {
if (file.isDirectory())
return null;
String fileName = file.getName();
String extensionName =null;
int dot = fileName.lastIndexOf('.');
if ((dot > -1) && (dot < (fileName.length() -1))) {
extensionName = fileName.substring(dot +1);
}
return extensionName;
}
/**
* 获取文件名,不带后缀
*/
public static String getFileNameNoEx(File file) {
if (file ==null)
return null;
String filename = file.getName();
if (file.isDirectory()) {
return filename;
}
if ((filename !=null) && (filename.length() >0)) {
int dot = filename.lastIndexOf('.');
if ((dot > -1) && (dot < (filename.length()))) {
return filename.substring(0, dot);
}
}
return filename;
}
/**
* 创建文件
*
* @param strPath 路径
* @param strFileName 文件名
* @return true 成功, false 失败
*/
//
public static boolean createFile(String strPath, String strFileName) {
File createdFile =new File(strPath +"/" + strFileName);
if (!createdFile.exists()) {
try {
return createdFile.createNewFile();
}catch (IOException e) {
Log.i("Exception", e.getMessage());
return false;
}
}
return false;
}
public static void makeDir(File dir) {
if (!dir.getParentFile().exists()) {
makeDir(dir.getParentFile());
}
dir.mkdir();
}
public static boolean createDirectory(String directory) {
File dir =new File(directory);
if (!dir.exists()) {
return dir.mkdirs();
}
return false;
}
public static boolean deleteDirectory(String directory) {
File dir =new File(directory);
if (dir.isDirectory()) {
File[] files = dir.listFiles();
for (File file : files) {
deleteDirectory(file.getAbsolutePath());
}
}else {
return dir.delete();
}
return true;
}
public static boolean directoryExist(String directory) {
File dir =new File(directory);
return dir.exists();
}
/**
* 递归删除目录下的所有文件及子目录下所有文件
*
* @param dir 将要删除的文件目录
* @return boolean Returns "true" if all deletions were successful.
* If a deletion fails, the method stops attempting to
* delete and returns "false".
*/
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
//递归删除目录中的子目录下
for (int i =0; i < children.length; i++) {
boolean success =deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// 目录此时为空,可以删除
return true;
}
/**
* 删除指定目录的文件
*
* @param path 指定目录
*/
public static void deleteFile(String path) {
File file =new File(path);
if (file.exists()) {
file.delete();
}
}
/**
* 根据路径创建文件
*
* @param path 路径
* @return 文件
*/
public static File newFile(String path) {
try {
File file =new File(path);
if (!file.exists()) {
file.createNewFile();
}
return file;
}catch (IOException e) {
e.printStackTrace();
return null;
}
}
}