正在查看: 微商客 v2.1.2 应用的 FileUtil.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
正在查看: 微商客 v2.1.2 应用的 FileUtil.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
package com.jianzhen.wsk.utils;
import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.text.TextUtils;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
public class FileUtil {
public static File currentPhotoFile;
private boolean flag = true;
private File file = null;
public static boolean isSDcardExist() {
return Environment.getExternalStorageState().equals("mounted");
}
public static File getDiskCacheDir(Context context, String str) {
return new File((isSDcardExist() ? getExternalCacheDir(context) : context.getCacheDir()).getPath() + File.separator + str);
}
public static File getExternalCacheDir(Context context, String str) {
return new File(getExternalCacheDir(context).getPath() + File.separator + str);
}
public static File getUserCacheDir(Context context, String str) {
return new File(context.getCacheDir().getPath() + File.separator + str);
}
public static File getExternalCacheDir(Context context) {
if (context == null) {
return new File(Environment.getExternalStorageDirectory().getPath() + "/Android/data/com.com.wxhelper/cache/");
}
return new File(Environment.getExternalStorageDirectory().getPath() + ("/Android/data/" + context.getPackageName() + "/cache/"));
}
public static String md5(String str) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
if (str != null) {
messageDigest.update(str.getBytes());
}
return byteToHexString(messageDigest.digest());
} catch (Exception unused) {
return str;
}
}
public static String byteToHexString(byte[] bArr) {
StringBuffer stringBuffer = new StringBuffer();
for (byte b : bArr) {
String hexString = Integer.toHexString(b & 255);
if (hexString.length() == 1) {
hexString = "0" + hexString;
}
stringBuffer.append(hexString.toUpperCase());
}
return stringBuffer.toString();
}
public static String getFileSize(Context context, File file) {
if (!file.exists()) {
return "0";
}
return FormatFileSize(getFileSize(file));
}
public static String getFileMD5(File file) {
if (!file.isFile()) {
return "";
}
byte[] bArr = new byte[1024];
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
FileInputStream fileInputStream = new FileInputStream(file);
while (true) {
int read = fileInputStream.read(bArr, 0, 1024);
if (read != -1) {
messageDigest.update(bArr, 0, read);
} else {
fileInputStream.close();
return new BigInteger(1, messageDigest.digest()).toString(16);
}
}
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
public static long getFileSize(File file) {
long length;
File[] listFiles = file.listFiles();
long j = 0;
for (int i = 0; i < listFiles.length; i++) {
if (listFiles[i].isDirectory()) {
length = getFileSize(listFiles[i]);
} else {
length = listFiles[i].length();
}
j += length;
}
return j;
}
public static long getlist(File file) {
File[] listFiles = file.listFiles();
long length = listFiles.length;
for (int i = 0; i < listFiles.length; i++) {
if (listFiles[i].isDirectory()) {
length = (length + getlist(listFiles[i])) - 1;
}
}
return length;
}
public static String FormatFileSize(long j) {
DecimalFormat decimalFormat = new DecimalFormat("#.00");
if (j < 1024) {
if (j == 0) {
return "0.0M";
}
return decimalFormat.format(j) + "B";
}
if (j < 1048576) {
return decimalFormat.format(j / 1024.0d) + "K";
}
if (j < 1073741824) {
return decimalFormat.format(j / 1048576.0d) + "M";
}
return decimalFormat.format(j / 1.073741824E9d) + "G";
}
public static void deleteFile(File file) {
if (file == null || !file.exists()) {
return;
}
if (file.isDirectory()) {
for (File file2 : file.listFiles()) {
if (file2.isFile()) {
file2.delete();
} else if (file2.isDirectory()) {
deleteFile(file2);
}
}
return;
}
file.delete();
}
public static LinkedList<File> getFile(File file, String str) {
String[] list;
LinkedList<File> linkedList = new LinkedList<>();
if (file.exists() && (list = file.list()) != null) {
for (int i = 0; i < list.length; i++) {
if (list[i].startsWith(str)) {
linkedList.add(new File(file.getAbsoluteFile() + File.separator + list[i]));
}
}
}
return linkedList;
}
public static long getFreeSize(File file) {
StatFs statFs = new StatFs(file.getPath());
return statFs.getAvailableBlocks() * statFs.getBlockSize();
}
public static boolean isExist(String str) {
return !TextUtils.isEmpty(str) && new File(str).exists();
}
public static void copyFile(String str, String str2) {
try {
if (!new File(str).exists()) {
return;
}
FileInputStream fileInputStream = new FileInputStream(str);
FileOutputStream fileOutputStream = new FileOutputStream(create(str2));
byte[] bArr = new byte[1444];
while (true) {
int read = fileInputStream.read(bArr);
if (read != -1) {
fileOutputStream.write(bArr, 0, read);
} else {
fileInputStream.close();
fileOutputStream.close();
return;
}
}
} catch (Exception e) {
Log.e("copyFile", "复制单个文件操作出错");
e.printStackTrace();
}
}
public static void copyfile(File file, File file2, Boolean bool) {
if (!file.exists() || !file.isFile() || !file.canRead()) {
return;
}
if (!file2.getParentFile().exists()) {
file2.getParentFile().mkdirs();
}
if (file2.exists() && bool.booleanValue()) {
file2.delete();
}
try {
FileInputStream fileInputStream = new FileInputStream(file);
FileOutputStream fileOutputStream = new FileOutputStream(file2);
byte[] bArr = new byte[1024];
while (true) {
int read = fileInputStream.read(bArr);
if (read > 0) {
fileOutputStream.write(bArr, 0, read);
} else {
fileInputStream.close();
fileOutputStream.close();
return;
}
}
} catch (Exception e) {
Log.e("readfile", e.getMessage());
}
}
public static void DeleteFile(File file) {
if (file.exists()) {
if (file.isFile()) {
File file2 = new File(file.getAbsolutePath() + System.currentTimeMillis());
file.renameTo(file2);
file2.delete();
return;
}
if (file.isDirectory()) {
File file3 = new File(file.getAbsolutePath() + System.currentTimeMillis());
file.renameTo(file3);
File[] listFiles = file3.listFiles();
if (listFiles == null || listFiles.length == 0) {
file3.delete();
return;
}
for (File file4 : listFiles) {
DeleteFile(file4);
}
file3.delete();
}
}
}
public static List<File> getFiles(String str, List<File> list) {
File file = new File(str);
if (file.isDirectory()) {
for (File file2 : file.listFiles()) {
list.add(file2);
}
}
return list;
}
public static List<File> getFileSort(String str) {
List<File> files = getFiles(str, new ArrayList());
if (files != null && files.size() > 0) {
Collections.sort(files, new Comparator<File>() {
@Override
public int compare(File file, File file2) {
if (file.lastModified() < file2.lastModified()) {
return 1;
}
return file.lastModified() == file2.lastModified() ? 0 : -1;
}
});
}
return files;
}
public static String getExtensionName(String str) {
int lastIndexOf;
if (str != null && str.length() > 0 && (lastIndexOf = str.lastIndexOf(46)) > -1 && lastIndexOf < str.length() - 1) {
return str.substring(lastIndexOf + 1);
}
return "";
}
public static File create(String str) {
if (TextUtils.isEmpty(str)) {
return null;
}
File file = new File(str);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try {
file.createNewFile();
return file;
} catch (IOException unused) {
if (file.exists()) {
file.delete();
}
return null;
}
}
public static byte[] load(String str) {
try {
File file = new File(str);
int length = (int) file.length();
byte[] bArr = new byte[length];
FileInputStream fileInputStream = new FileInputStream(file);
int i = 0;
do {
int read = fileInputStream.read(bArr, i, length);
i += read;
length -= read;
} while (length != 0);
fileInputStream.close();
return bArr;
} catch (FileNotFoundException | IOException unused) {
return null;
}
}
public static boolean delete(String str) {
if (TextUtils.isEmpty(str)) {
return false;
}
File file = new File(str);
if (file.exists()) {
return renameOnDelete(file).delete();
}
return false;
}
private static File renameOnDelete(File file) {
File file2 = new File(file.getParent() + "/" + System.currentTimeMillis() + "_tmp");
return file.renameTo(file2) ? file2 : file;
}
public static boolean move(String str, String str2) {
if (!TextUtils.isEmpty(str) && !TextUtils.isEmpty(str2)) {
File file = new File(str);
if (file.exists() && file.isFile()) {
File file2 = new File(str2);
if (file2.getParentFile() == null) {
return false;
}
if (!file2.getParentFile().exists()) {
file2.getParentFile().mkdirs();
}
return file.renameTo(file2);
}
}
return false;
}
public static File getExternalHistoryDir(Context context) {
return new File(Environment.getExternalStorageDirectory().getPath() + ("/Android/data/" + context.getPackageName() + "/sendhistory/"));
}
public boolean deleteFolder(String str) {
this.flag = false;
File file = new File(str);
this.file = file;
if (!file.exists()) {
return this.flag;
}
if (this.file.isFile()) {
return deleteFile(str);
}
return deleteDirectory(str);
}
private boolean deleteDirectory(String str) {
if (!str.endsWith(File.separator)) {
str = str + File.separator;
}
File file = new File(str);
if (file.exists() && file.isDirectory()) {
this.flag = true;
File[] listFiles = file.listFiles();
for (int i = 0; i < listFiles.length; i++) {
if (listFiles[i].isFile()) {
boolean deleteFile = deleteFile(listFiles[i].getAbsolutePath());
this.flag = deleteFile;
if (!deleteFile) {
break;
}
} else {
boolean deleteDirectory = deleteDirectory(listFiles[i].getAbsolutePath());
this.flag = deleteDirectory;
if (!deleteDirectory) {
break;
}
}
}
if (this.flag && file.delete()) {
return true;
}
}
return false;
}
private boolean deleteFile(String str) {
this.flag = false;
File file = new File(str);
this.file = file;
if (file.isFile() && this.file.exists()) {
this.file.delete();
this.flag = true;
}
return this.flag;
}
}