package com.yanzuoguang.util.helper; import com.yanzuoguang.util.YzgError; import java.io.*; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; /** * 登录相关函数 * * @author 颜佐光 */ public class FileHelper { public static final int FILE_TYPE_NONE = 0; public static final int FILE_TYPE_IMAGE = 1; public static final int FILE_TYPE_VIDEO = 2; public static final int FILE_TYPE_AUDIO = 3; /** * 获取文件类型 * * @param filePath 来源文件 * @return * @throws IOException */ public static String getMimeType(String filePath) { File file = new File(filePath); if (!file.exists()) { return StringHelper.EMPTY; } Path path = Paths.get(filePath); try { String contentType = Files.probeContentType(path); return contentType; } catch (Exception ex) { throw YzgError.getRuntimeException(ex,"044",filePath); } } /** * 获取文件媒体类型 * * @param file * @return * @throws IOException */ public static int getMediaType(String file) { String mimeType = FileHelper.getMimeType(file); if (mimeType == null) { throw YzgError.getRuntimeException("008",file); } int type = getMediaTypeByMime(mimeType); // System.out.println(mimeType); if (type == FILE_TYPE_NONE) { throw YzgError.getRuntimeException("009"); } return type; } /** * 获取视频类型 * * @param mimeType * @return */ public static int getMediaTypeByMime(String mimeType) { mimeType = StringHelper.getFirst(mimeType, StringHelper.EMPTY); if (mimeType.startsWith("video/")) { return FILE_TYPE_VIDEO; } else if (mimeType.startsWith("audio/")) { return FILE_TYPE_AUDIO; } else if (mimeType.startsWith("image/")) { return FILE_TYPE_IMAGE; } else { return FILE_TYPE_NONE; } } /** * 创建目录 * * @param path 文件路径 * @return 读取的内容 */ public static String createDirectory(String path) { return createDirectory(new File(path)); } /** * 创建目录 * * @param file 文件路径 * @return 读取的内容 */ public static String createDirectory(File file) { if (file.exists()) { //检查此路径名的文件是否是一个目录(文件夹) if (file.isDirectory()) { return StringHelper.EMPTY; } else { throw YzgError.getRuntimeException("010",file.getName()); } } file.mkdirs(); return file.getName(); } /** * 获取登录标记 * * @param file 文件路径 * @param encoding 文件编码 * @return 读取的内容 */ public static String readFile(File file, String encoding) { //检查此路径名的文件是否是一个目录(文件夹) if (file.isDirectory()) { return StringHelper.EMPTY; } if (!file.exists()) { return StringHelper.EMPTY; } try { InputStream inputStream = new FileInputStream(file); return readFile(inputStream, encoding); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } } /** * 读取文件内容 * * @param inputStream 文件流 * @param encoding 文件编码 * @return 文件内容的结果 */ public static String readFile(InputStream inputStream, String encoding) { if (inputStream == null) { return StringHelper.EMPTY; } StringBuilder sb = new StringBuilder(); try { try { InputStreamReader inputStreamReader = new InputStreamReader(inputStream, encoding); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String line; //分行读取 while ((line = bufferedReader.readLine()) != null) { if (sb.length() > 0) { sb.append("\n"); } sb.append(line); } } finally { inputStream.close();//关闭输入流 } } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } return sb.toString(); } /** * 写入文件内容 * * @param file 文件路径 * @param content 文件内容 * @param encoding 文件编码 */ public static void writeFileAppend(File file, String content, String encoding) { try { // 创建文件夹 File parentFile = file.getParentFile(); if (parentFile != null) { if (!parentFile.exists()) { if (!parentFile.mkdirs()) { throw YzgError.getRuntimeException("011"); } } if (!parentFile.exists()) { throw YzgError.getRuntimeException("011"); } } // 文件不存在则创建 if (!file.exists()) { file.createNewFile(); } // 写入文件 RandomAccessFile raf = new RandomAccessFile(file, "rwd"); raf.seek(file.length()); raf.write(content.getBytes(encoding)); raf.close(); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } } /** * 写入文件内容 * * @param file 文件路径 * @param content 文件内容 * @param encoding 文件编码 */ public static void writeFile(File file, String content, String encoding) { try { // 删除文件 if (file.exists()) { if (!file.delete()) { throw YzgError.getRuntimeException("012",file.getName()); } } if (file.exists()) { throw YzgError.getRuntimeException("012",file.getName()); } writeFileAppend(file, content, encoding); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } } /** * 根据路径删除指定的目录或文件,无论存在与否 * * @param sPath 要删除的目录或文件 * @return 删除成功返回 true,否则返回 false。 */ public static boolean deleteFolder(String sPath) { if (StringHelper.isEmpty(sPath)) { return false; } boolean flag = false; File file = new File(sPath); // 判断目录或文件是否存在 if (!file.exists()) { // 不存在返回 false return flag; } else { // 判断是否为文件 if (file.isFile()) { // 为文件时调用删除文件方法 return deleteFile(sPath); } else { // 为目录时调用删除目录方法 return deleteDirectory(sPath); } } } /** * 删除单个文件 * * @param sPath 被删除文件的文件名 * @return 单个文件删除成功返回true,否则返回false */ public static boolean deleteFile(String sPath) { boolean flag = false; File file = new File(sPath); // 路径为文件且不为空则进行删除 if (file.isFile() && file.exists()) { file.delete(); flag = true; } return flag; } /** * 删除目录(文件夹)以及目录下的文件 * * @param sPath 被删除目录的文件路径 * @return 目录删除成功返回true,否则返回false */ public static boolean deleteDirectory(String sPath) { //如果sPath不以文件分隔符结尾,自动添加文件分隔符 if (!sPath.endsWith(File.separator)) { sPath = sPath + File.separator; } File dirFile = new File(sPath); //如果dir对应的文件不存在,或者不是一个目录,则退出 if (!dirFile.exists() || !dirFile.isDirectory()) { return false; } boolean flag = true; //删除文件夹下的所有文件(包括子目录) File[] files = dirFile.listFiles(); if (files != null) { for (int i = 0; i < files.length; i++) { //删除子文件 if (files[i].isFile()) { flag = deleteFile(files[i].getAbsolutePath()); if (!flag) { break; } } //删除子目录 else { flag = deleteDirectory(files[i].getAbsolutePath()); if (!flag) { break; } } } } if (!flag) { return false; } //删除当前目录 return dirFile.delete(); } }