package com.qiwu.inference.util; import org.apache.commons.io.FileUtils; import java.io.File; import java.io.InputStream; import java.util.List; public class FileUtil { public static InputStream getResourcesFileInputStream(String fileName) { return Thread.currentThread().getContextClassLoader().getResourceAsStream("" + fileName); } public static String getPath() { return FileUtil.class.getResource("/").getPath(); } public static File createNewFile(String pathName) { File file = new File(getPath() + pathName); if (file.exists()) { file.delete(); } else { if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } } return file; } public static void writeLog(List list, String filePath)throws Exception{ FileUtils.writeLines(new File(filePath), "utf-8", list,false); } public static File readFile(String pathName) { return new File(getPath() + pathName); } public static File readUserHomeFile(String pathName) { return new File(System.getProperty("user.home") + File.separator + pathName); } /** * 用以模糊删除头部为str的文件 * @param path 要删除的文件的文件夹的路径 * @param str 匹配的字符串的头 * @return boolean */ public static boolean delFilesByPath(String path,String str){ boolean flag=false; File file = new File(path); File[] tempFile = file.listFiles(); for (File value : tempFile) { if (value.getName().startsWith(str) || value.getName().endsWith(str)) { System.out.println("将被删除的文件名:" + value.getName()); boolean del = deleteFile(path + value.getName()); if (del) { System.out.println("文件" + value.getName() + "删除成功!"); flag = true; } else { System.out.println("文件" + value.getName() + "删除失败!"); } } } return flag; } /** * 用以删除文件 * @param path 要删除的文件的路径 * @return boolean */ private static boolean deleteFile(String path){ System.out.println(path); boolean del=false; File file=new File(path); if(file.isFile()){ file.delete(); del=true; } return del; } }