package com.yuyh.library.utils.io;
import android.content.Context;
import android.content.res.AssetManager;
import com.yuyh.library.AppUtils;
import com.yuyh.library.utils.log.LogUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;
import java.text.DecimalFormat;
/**
* @author yuyh.
* @date 16/4/9.
*/
public class FileUtils {
private static final String TAG = FileUtils.class.getSimpleName();
/**
* 创建根缓存目录
*
* @return
*/
public static String createRootPath() {
String cacheRootPath = "";
if (StorageUtils.isSdCardAvailable()) {
// /sdcard/Android/data/<application package>/cache
cacheRootPath = AppUtils.getAppContext().getExternalCacheDir().getPath();
} else {
// /data/data/<application package>/cache
cacheRootPath = AppUtils.getAppContext().getCacheDir().getPath();
}
return cacheRootPath;
}
/**
* 递归创建文件夹
*
* @param dirPath
* @return 创建失败返回""
*/
public static String createDir(String dirPath) {
try {
File file = new File(dirPath);
if (file.getParentFile().exists()) {
LogUtils.i("----- 创建文件夹"+file.getAbsolutePath());
file.mkdir();
return file.getAbsolutePath();
} else {
createDir(file.getParentFile().getAbsolutePath());
LogUtils.i("----- 创建文件夹"+file.getAbsolutePath());
file.mkdir();
}
} catch (Exception e) {
e.printStackTrace();
}
return dirPath;
}
/**
* 递归创建文件夹
*
* @param file
* @return 创建失败返回""
*/
public static String createFile(File file) {
try {
if (file.getParentFile().exists()) {
LogUtils.i("----- 创建文件"+file.getAbsolutePath());
file.createNewFile();
return file.getAbsolutePath();
} else {
createDir(file.getParentFile().getAbsolutePath());
file.createNewFile();
LogUtils.i("----- 创建文件"+file.getAbsolutePath());
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
/**
* 获取图片缓存目录
*
* @return 创建失败, 返回""
*/
public static String getImageCachePath() {
String path = createDir(createRootPath() + File.separator + "img" + File.separator);
return path;
}
/**
* 获取图片裁剪缓存目录
*
* @return 创建失败, 返回""
*/
public static String getImageCropCachePath() {
String path = createDir(createRootPath() + File.separator + "imgCrop" + File.separator);
return path;
}
/**
* 将内容写入文件
*
* @param filePath eg:/mnt/sdcard/demo.txt
* @param content 内容
*/
public static void writeFileSdcard(String filePath, String content, boolean isAppend) {
try {
FileOutputStream fout = new FileOutputStream(filePath, isAppend);
byte[] bytes = content.getBytes();
fout.write(bytes);
fout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 打开Asset下的文件
*
* @param context
* @param fileName
* @return
*/
public static InputStream openAssetFile(Context context, String fileName) {
AssetManager am = context.getAssets();
InputStream is = null;
try {
is = am.open(fileName);
} catch (IOException e) {
e.printStackTrace();
}
return is;
}
/**
* 获取Raw下的文件内容
*
* @param context
* @param resId
* @return 文件内容
*/
public static String getFileFromRaw(Context context, int resId) {
if (context == null) {
return null;
}
StringBuilder s = new StringBuilder();
try {
InputStreamReader in = new InputStreamReader(context.getResources().openRawResource(resId));
BufferedReader br = new BufferedReader(in);
String line;
while ((line = br.readLine()) != null) {
s.append(line);
}
return s.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 文件拷贝
*
* @param src 源文件
* @param desc 目的文件
*/
public static void fileChannelCopy(File src, File desc) {
FileInputStream fi = null;
FileOutputStream fo = null;
try {
fi = new FileInputStream(src);
fo = new FileOutputStream(desc);
FileChannel in = fi.getChannel();//得到对应的文件通道
FileChannel out = fo.getChannel();//得到对应的文件通道
in.transferTo(0, in.size(), out);//连接两个通道,并且从in通道读取,然后写入out通道
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fo != null) fo.close();
if (fi != null) fi.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 转换文件大小
*
* @param fileLen 单位B
* @return
*/
public static String formatFileSizeToString(long fileLen) {
DecimalFormat df = new DecimalFormat("#.00");
String fileSizeString = "";
if (fileLen < 1024) {
fileSizeString = df.format((double) fileLen) + "B";
} else if (fileLen < 1048576) {
fileSizeString = df.format((double) fileLen / 1024) + "K";
} else if (fileLen < 1073741824) {
fileSizeString = df.format((double) fileLen / 1048576) + "M";
} else {
fileSizeString = df.format((double) fileLen / 1073741824) + "G";
}
return fileSizeString;
}
/**
* 删除指定文件
*
* @param file
* @return
* @throws IOException
*/
public static boolean deleteFile(File file) throws IOException {
return deleteFileOrDirectory(file);
}
/**
* 删除指定文件,如果是文件夹,则递归删除
*
* @param file
* @return
* @throws IOException
*/
public static boolean deleteFileOrDirectory(File file) throws IOException {
try {
if (file != null && file.isFile()) {
return file.delete();
}
if (file != null && file.isDirectory()) {
File[] childFiles = file.listFiles();
// 删除空文件夹
if (childFiles == null || childFiles.length == 0) {
return file.delete();
}
// 递归删除文件夹下的子文件
for (int i = 0; i < childFiles.length; i++) {
deleteFileOrDirectory(childFiles[i]);
}
return file.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/***
* 获取文件扩展名
*
* @param filename 文件名
* @return
*/
public static String getExtensionName(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot > -1) && (dot < (filename.length() - 1))) {
return filename.substring(dot + 1);
}
}
return filename;
}
/**
* 获取文件内容
*
* @param path
* @return
*/
public static String getFileOutputString(String path) {
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(path), 8192);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
sb.append("\n").append(line);
}
bufferedReader.close();
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}