package com.basic.project.idcardservice.idcard;
|
import android.os.Environment;
|
import android.os.StatFs;
|
import android.util.Log;
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
/**
|
* Created by Administrator on 2018/7/25.
|
*/
|
public class FileUtils {
|
public static boolean isSDCardState() {
|
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
|
}
|
public static boolean isFileExist(String fileName) {
|
File file = new File("绝对路径" + fileName);
|
return file.exists();
|
}
|
public static boolean createFolder(String path) {
|
File file = new File(path);
|
return file.mkdir();
|
}
|
public static boolean createFile(String path, String fileName) {
|
File file = new File(path + File.separator + fileName);
|
if (file.exists()) {
|
return false;
|
} else {
|
try {
|
return file.createNewFile();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
return false;
|
}
|
public static boolean deleteFile(String path, String fileName) {
|
File file = new File(path + File.separator + fileName);
|
return file.exists() && file.delete();
|
}
|
public static boolean deleteDirection(File dir) {
|
if (dir == null || !dir.exists() || dir.isFile()) {
|
return false;
|
}
|
for (File file : dir.listFiles()) {
|
if (file.isFile()) {
|
file.delete();
|
} else if (file.isDirectory()) {
|
deleteDirection(file);
|
}
|
}
|
dir.delete();
|
return true;
|
}
|
public static void writeFile(String text, String fileStr, boolean isAppend) {
|
try {
|
File file = new File(fileStr);
|
File parentFile = file.getParentFile();
|
if (!parentFile.exists()) {
|
parentFile.mkdirs();
|
}
|
if (!file.exists()) {
|
file.createNewFile();
|
}
|
FileOutputStream f = new FileOutputStream(fileStr, isAppend);
|
f.write(text.getBytes());
|
f.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
public static boolean copyFile(String srcPath, String destDir) {
|
boolean flag = false;
|
File srcFile = new File(srcPath);
|
if (!srcFile.exists()) {
|
Log.i("FileUtils is copyFile:", "源文件不存在");
|
return false;
|
}
|
String fileName = srcPath.substring(srcPath.lastIndexOf(File.separator));
|
String destPath = destDir + fileName;
|
if (destPath.equals(srcPath)) {
|
Log.i("FileUtils is copyFile:", "源文件路径和目标文件路径重复");
|
return false;
|
}
|
File destFile = new File(destPath);
|
if (destFile.exists() && destFile.isFile()) {
|
Log.i("FileUtils is copyFile:", "该路径下已经有一个同名文件");
|
return false;
|
}
|
File destFileDir = new File(destDir);
|
destFileDir.mkdirs();
|
try {
|
FileInputStream fis = new FileInputStream(srcPath);
|
FileOutputStream fos = new FileOutputStream(destFile);
|
byte[] buf = new byte[1024];
|
int c;
|
while ((c = fis.read(buf)) != -1) {
|
fos.write(buf, 0, c);
|
}
|
fis.close();
|
fos.close();
|
flag = true;
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return flag;
|
}
|
public static boolean renameTo(String oldPath, String newPath) {
|
if (oldPath.equals(newPath)) {
|
Log.i("FileUtils is renameTo:", "文件重命名失败:新旧文件名绝对路径相同");
|
return false;
|
}
|
File oldFile = new File(oldPath);
|
File newFile = new File(newPath);
|
return oldFile.renameTo(newFile);
|
}
|
public static long getFileSize(String path) {
|
File file = new File(path);
|
return file.length();
|
}
|
public static double getDirSize(File file) {
|
if (file.exists()) {
|
if (file.isDirectory()) {
|
File[] children = file.listFiles();
|
double size = 0;
|
for (File f : children)
|
size += getDirSize(f);
|
return size;
|
} else {
|
return (double) file.length() / 1024 / 1024;
|
}
|
} else {
|
return 0.0;
|
}
|
}
|
public static File[] getFileList(String path) {
|
File file = new File(path);
|
if (file.isDirectory()) {
|
File[] files = file.listFiles();
|
if (files != null) {
|
return files;
|
} else {
|
return null;
|
}
|
} else {
|
return null;
|
}
|
}
|
public static int getFileCount(String path) {
|
File directory = new File(path);
|
File[] files = directory.listFiles();
|
return files.length;
|
}
|
public long getSDCardTotal(String path) {
|
if (null != path && path.equals("")) {
|
StatFs statfs = new StatFs(path);
|
long totalBlocks = statfs.getBlockCount();
|
long blockSize = statfs.getBlockSize();
|
return totalBlocks * blockSize / 1024 / 1024;
|
} else {
|
return 0;
|
}
|
}
|
public long getSDCardFree(String path) {
|
if (null != path && path.equals("")) {
|
StatFs statfs = new StatFs(path);
|
long availaBlocks = statfs.getAvailableBlocks();
|
long blockSize = statfs.getBlockSize();
|
return availaBlocks * blockSize / 1024 / 1024;
|
} else {
|
return 0;
|
}
|
}
|
}
|