import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import org.apache.log4j.Logger; public class FileUtils { /** * 讀取一個文件夾下全部文件的絕對路徑。忽略此文件夾下的問價夾 * * @param absFolderPath * 文件夾絕對路徑 * @return List<String> */ public static List<String> readFilePath(String absFolderPath) { List<String> paths = new ArrayList<String>(); File file = new File(absFolderPath); // 文件夾 if (file.isDirectory()) { for (File childFile : file.listFiles()) { // 不是文件夾而且沒有被操做 if (!childFile.isDirectory()) { // 路徑經過"/"手動建立,避免因window下出現反斜槓\ 路徑的狀況 paths.add(absFolderPath + "/" + childFile.getName()); } } } return paths; } /** * 經過文件的絕對路徑刪除文件,若是傳入是文件夾路徑,忽略並返回false * * @param absFilePath * 文件絕對路徑 * @return boolean */ public static boolean deleteFile(String absFilePath) { boolean result = false; File file = new File(absFilePath); if (!file.isDirectory()) { file.delete(); result = true; } return result; } /** * 刪除問價夾下全部文件 * * @param absFolderPath * @return boolean */ public static boolean deleteAllFileInTheFolder(String absFolderPath) { boolean result = true; for (String filePath : FileDealUtil.readFilePath(absFolderPath)) { result = result && FileDealUtil.deleteFile(filePath); } return result; } /** * 將文件移動到指定問價夾下 * * @param filePath * 文件路徑 * @param folderPath * 文件夾路徑 * @return String 移動後文件路徑,filePath爲文件夾或folderPath爲文件或文件正在被其餘進程打開沒法移動返回null */ public static String moveFile(String filePath, String folderPath) { String path = null; File file = new File(filePath); if (file.isDirectory()) return path; File folder = new File(folderPath); if (!folder.exists()) { folder.mkdirs(); } if (!folder.isDirectory()) return path; File nowFile = new File(folderPath + "/" + file.getName()); if (nowFile.exists()) nowFile.delete(); if (file.renameTo(new File(folder, file.getName()))) path = folderPath + "/" + file.getName(); return path; } /** * 獲得文件輸入流 * * @param path * 絕對路徑 * @return InputStream */ public static InputStream getFile(String path) { // First try to read from the file system ... File file = new File(path); if (file.exists() && file.canRead()) { try { return new FileInputStream(file); } catch (FileNotFoundException e) { // continue return null; } } return null; } /** * 判斷文件夾是否存在 void * */ public static boolean existFolder(String folderPath) { File file = new File(folderPath); return file.exists(); } /** * 建立文件夾 * * @param folderPath * void */ public static void creatFolder(String folderPath) { if (!existFolder(folderPath)) { File file = new File(folderPath); file.mkdirs(); } } /** * 獲取文件大小 若是文件存在而且不是目錄,返回文件大小,若是文件不存在或者是目錄,返回0 * * @return Long 單位bytes */ public static Long getFileSize(String filePath) { File file = new File(filePath); if (file.exists() && !file.isDirectory()) { return file.length(); } else { return 0L; } } /** * 從文件路徑中分離出文件名 * * @param filePath * @return String */ public static String splitFileName(String filePath) { return filePath.substring(filePath.lastIndexOf("/") + 1); } /** * * @param filePath * @param inputStream * @return boolean */ public static boolean writeFile(String filePath, InputStream inputStream) { OutputStream outputStream = null; try { outputStream = new FileOutputStream(filePath); int bytesRead = 0; byte[] buffer = new byte[2048]; while ((bytesRead = inputStream.read(buffer, 0, 2048)) != -1) { outputStream.write(buffer, 0, bytesRead); } return true; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } finally { try { outputStream.close(); inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }