public class FileUtils {
private static final String TAG = "FileUtils";
public static final String BASE_PATH = "/storage/sdcard0";
/**
* 判断是否插入SD卡
* @return
*/
public static boolean isSDCardMounted() {
String state = Environment.getStorageState(new File(BASE_PATH));
File dir = new File(BASE_PATH);
if (!dir.exists()) {
return false;
}
if (Environment.MEDIA_MOUNTED.equals(state) && dir.isDirectory() && dir.canWrite()) {
Log.d(TAG, "External storage state=" + state);
return true;
}
return false;
}
/**
* 复制单个文件
* @param oldPath
* @param newPath
*/
public static void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
} catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
/**
* 复制整个文件夹内容
* @param oldPath
* @param newPath
* @return
*/
public static boolean copyFolder(String oldPath, String newPath) {
try {
File newFile = new File(newPath);
if (!newFile.exists()) {
if (!newFile.mkdirs()) {
Log.e("--Method--", "copyFolder: cannot create directory.");
return false;
}
}
File oldFile = new File(oldPath);
String[] files = oldFile.list();
File temp;
for (String file : files) {
if (oldPath.endsWith(File.separator)) {
temp = new File(oldPath + file);
} else {
temp = new File(oldPath + File.separator + file);
}
if (temp.isDirectory()) { //如果是子文件夹
copyFolder(oldPath + "/" + file, newPath + "/" + file);
} else if (!temp.exists()) {
Log.e("--Method--", "copyFolder: oldFile not exist.");
return false;
} else if (!temp.isFile()) {
Log.e("--Method--", "copyFolder: oldFile not file.");
return false;
} else if (!temp.canRead()) {
Log.e("--Method--", "copyFolder: oldFile cannot read.");
return false;
} else {
FileInputStream fileInputStream = new FileInputStream(temp);
FileOutputStream fileOutputStream = new FileOutputStream(newPath + "/" + temp.getName());
byte[] buffer = new byte[1024];
int byteRead;
while ((byteRead = fileInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, byteRead);
}
fileInputStream.close();
fileOutputStream.flush();
fileOutputStream.close();
}
/* 如果不需要打log,可以使用下面的语句
if (temp.isDirectory()) { //如果是子文件夹
copyFolder(oldPath + "/" + file, newPath + "/" + file);
} else if (temp.exists() && temp.isFile() && temp.canRead()) {
FileInputStream fileInputStream = new FileInputStream(temp);
FileOutputStream fileOutputStream = new FileOutputStream(newPath + "/" + temp.getName());
byte[] buffer = new byte[1024];
int byteRead;
while ((byteRead = fileInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, byteRead);
}
fileInputStream.close();
fileOutputStream.flush();
fileOutputStream.close();
}
*/
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 删除某个目录下面所有文件以及本身
* @param pPath
*/
public static void deleteDir(final String pPath) {
File dir = new File(pPath);
deleteDirWihtFile(dir);
}
public static void deleteDirWihtFile(File dir) {
if (dir == null || !dir.exists() || !dir.isDirectory())
return;
for (File file : dir.listFiles()) {
if (file.isFile())
file.delete(); // 删除所有文件
else if (file.isDirectory())
deleteDirWihtFile(file); // 递规的方式删除文件夹
}
dir.delete();// 删除目录本身
}
/**
* 获取文件夹大小
*
* @param file File实例
* @return long
*/
public static long getFolderSize(File file) {
long size = 0;
try {
File[] fileList = file.listFiles();
for (int i = 0; i < fileList.length; i++) {
if (fileList[i].isDirectory()) size = size + getFolderSize(fileList[i]);
else size = size + fileList[i].length();
}
} catch (Exception e) {
e.printStackTrace();
}
return size;
}
/**
* 将文件大小转换成字节
* @param fSize
* @return
*/
public static String formatFileSize(long fSize){
DecimalFormat df = new DecimalFormat("#");
String fileSizeString = "";
fileSizeString = df.format((double) fSize/1048576 );
return fileSizeString;
}
/**
* 判断文件是否存在
* @param strFile
* @return
*/
public static boolean fileIsExists(String strFile)
{
try
{
File f=new File(strFile);
if(!f.exists())
{
return false;
}
}
catch (Exception e)
{
return false;
}
return true;
}
/**
* 删除单个文件
* @param filePath
* @return
*/
public static boolean deleteFile(String filePath) {
File file = new File(filePath);
if (file.isFile() && file.exists()) {
return file.delete();
}
return false;
}
/**
* 删除空的的文件夹
* @param tempFile
*/
public static void deleteDirectory(File tempFile) {
try {
if(!tempFile.exists()) return;
if(tempFile.isDirectory()){
File[] files = tempFile.listFiles();
if(files == null || files.length == 0) {
tempFile.delete();
return;
}
for (File file: files){
if(file.isFile()){
file.delete();
} else if(file.isDirectory()){
deleteDirectory(file);
}
}
tempFile.delete();
}else {
tempFile.delete();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
文件处理工具类
最新推荐文章于 2021-09-30 14:24:36 发布