package com.moral.andbrickslib.compresshelper; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.provider.MediaStore; import android.provider.OpenableColumns; import android.util.Log; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * 文件工具类 * * Author: nanchen * Email: liushilin520@foxmail.com * Date: 2017-03-08 9:03 */ public class FileUtil { static final String FILES_PATH = "CompressHelper"; private static final int EOF = -1; private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; private FileUtil() { throw new UnsupportedOperationException("u can't instantiate me..."); } /** * 根据文件路径获取文件 * * @param filePath 文件路径 * @return 文件 */ public static File getFileByPath(String filePath) { return StringUtils.isSpace(filePath) ? null : new File(filePath); } /** * 判断文件是否存在 * * @param filePath 文件路径 * @return {@code true}: 存在
{@code false}: 不存在 */ public static boolean isFileExists(String filePath) { return isFileExists(getFileByPath(filePath)); } /** * 判断文件是否存在 * * @param file 文件 * @return {@code true}: 存在
{@code false}: 不存在 */ public static boolean isFileExists(File file) { return file != null && file.exists(); } /** * 重命名文件 * * @param filePath 文件路径 * @param newName 新名称 * @return {@code true}: 重命名成功
{@code false}: 重命名失败 */ public static boolean rename(String filePath, String newName) { return rename(getFileByPath(filePath), newName); } /** * 重命名文件 * * @param file 文件 * @param newName 新名称 * @return {@code true}: 重命名成功
{@code false}: 重命名失败 */ public static boolean rename(File file, String newName) { // 文件为空返回false if (file == null) return false; // 文件不存在返回false if (!file.exists()) return false; // 新的文件名为空返回false if (StringUtils.isSpace(newName)) return false; // 如果文件名没有改变返回true if (newName.equals(file.getName())) return true; File newFile = new File(file.getParent() + File.separator + newName); // 如果重命名的文件已存在返回false return !newFile.exists() && file.renameTo(newFile); } /** * 判断是否是目录 * * @param dirPath 目录路径 * @return {@code true}: 是
{@code false}: 否 */ public static boolean isDir(String dirPath) { return isDir(getFileByPath(dirPath)); } /** * 判断是否是目录 * * @param file 文件 * @return {@code true}: 是
{@code false}: 否 */ public static boolean isDir(File file) { return isFileExists(file) && file.isDirectory(); } /** * 判断是否是文件 * * @param filePath 文件路径 * @return {@code true}: 是
{@code false}: 否 */ public static boolean isFile(String filePath) { return isFile(getFileByPath(filePath)); } /** * 判断是否是文件 * * @param file 文件 * @return {@code true}: 是
{@code false}: 否 */ public static boolean isFile(File file) { return isFileExists(file) && file.isFile(); } /** * 重命名文件 * @param file 文件 * @param newName 新名字 * @return 新文件 */ public static File renameFile(File file, String newName) { File newFile = new File(file.getParent(), newName); if (!newFile.equals(file)) { if (newFile.exists()) { if (newFile.delete()) { Log.d("FileUtil", "Delete old " + newName + " file"); } } if (file.renameTo(newFile)) { Log.d("FileUtil", "Rename file to " + newName); } } return newFile; } /** * 获取临时文件 * @param context 上下文 * @param uri url * @return 临时文件 * @throws IOException */ public static File getTempFile(Context context, Uri uri) throws IOException { InputStream inputStream = context.getContentResolver().openInputStream(uri); String fileName = getFileName(context, uri); String[] splitName = splitFileName(fileName); File tempFile = File.createTempFile(splitName[0], splitName[1]); tempFile = renameFile(tempFile, fileName); tempFile.deleteOnExit(); FileOutputStream out = null; try { out = new FileOutputStream(tempFile); } catch (FileNotFoundException e) { e.printStackTrace(); } if (inputStream != null) { copy(inputStream, out); inputStream.close(); } if (out != null) { out.close(); } return tempFile; } /** * 截取文件名称 * @param fileName 文件名称 */ static String[] splitFileName(String fileName) { String name = fileName; String extension = ""; int i = fileName.lastIndexOf("."); if (i != -1) { name = fileName.substring(0, i); extension = fileName.substring(i); } return new String[]{name, extension}; } /** * 获取文件名称 * @param context 上下文 * @param uri uri * @return 文件名称 */ static String getFileName(Context context, Uri uri) { String result = null; if (uri.getScheme().equals("content")) { Cursor cursor = context.getContentResolver().query(uri, null, null, null, null); try { if (cursor != null && cursor.moveToFirst()) { result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); } } catch (Exception e) { e.printStackTrace(); } finally { if (cursor != null) { cursor.close(); } } } if (result == null) { result = uri.getPath(); int cut = result.lastIndexOf(File.separator); if (cut != -1) { result = result.substring(cut + 1); } } return result; } /** * 获取真实的路径 * @param context 上下文 * @param uri uri * @return 文件路径 */ static String getRealPathFromURI(Context context, Uri uri) { Cursor cursor = context.getContentResolver().query(uri, null, null, null, null); if (cursor == null) { return uri.getPath(); } else { cursor.moveToFirst(); int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); String realPath = cursor.getString(index); cursor.close(); return realPath; } } static int copy(InputStream input, OutputStream output) throws IOException { long count = copyLarge(input, output); if (count > Integer.MAX_VALUE) { return -1; } return (int) count; } static long copyLarge(InputStream input, OutputStream output) throws IOException { return copyLarge(input, output, new byte[DEFAULT_BUFFER_SIZE]); } static long copyLarge(InputStream input, OutputStream output, byte[] buffer) throws IOException { long count = 0; int n; while (EOF != (n = input.read(buffer))) { output.write(buffer, 0, n); count += n; } return count; } }