1. 程式人生 > >文件操作的工具類

文件操作的工具類

direct str 方式 cnblogs 獲取文件 exception 內容 source let

相關代碼如下:包含創建文件,創建目錄,創建壓縮文件,獲取文件等相關操作。

public class FileUtil {

    private static final Log LOGGER = LogFactory.getLog(FileUtil.class);

    private static ArrayList<File> fileList = new ArrayList<File>();

    private static boolean  dirExist = false;

    public static boolean isDirExist() {
        
return dirExist; } public static void setDirExist(boolean dirExist) { FileUtil.dirExist = dirExist; } /** * 讀取指定目錄下面的所有文件, * @param fileDir * @return */ public static ArrayList<File> ReadFile(String fileDir) { if (!fileList.isEmpty()) { cleanFiles(); } File[] files
= new File(fileDir).listFiles();// 獲取目錄下的所有文件或文件夾 if (files == null) {// 如果目錄為空,直接退出 return null; } // 遍歷,目錄下的所有文件 for (File file : files) { if (file.isFile()) { fileList.add(file); } else if (file.isDirectory()) { ReadFile(file.getAbsolutePath());
//采用遞歸的方式讀取子文件裏面的內容 } } return fileList; } public static void cleanFiles() { fileList.clear(); } /** * 把指定文件夾打包成zip格式的壓縮包 * @param sourceFilePath * @param zipFilePath * @param fileName * @return */ public static boolean fileToZip(String sourceFilePath, String zipFilePath, String fileName) { boolean flag = false; File sourceFile = new File(sourceFilePath); FileInputStream fis = null; BufferedInputStream bis = null; FileOutputStream fos = null; ZipOutputStream zos = null; if (!sourceFile.exists()) { LOGGER.error("待壓縮的文件目錄:" + sourceFilePath + "不存在."); } else { try { File zipFile = new File(zipFilePath + "/" + fileName + ".zip"); if (zipFile.exists()) { LOGGER.info(zipFilePath + "目錄下已經存在名字為:" + fileName + ".zip" + "打包文件."); } else { File[] sourceFiles = sourceFile.listFiles(); if (sourceFiles == null || sourceFiles.length < 1) { LOGGER.info("待壓縮的文件目錄:" + sourceFilePath + "裏面不存在文件,無需壓縮."); } else { fos = new FileOutputStream(zipFile); zos = new ZipOutputStream(new BufferedOutputStream(fos)); byte[] bufs = new byte[1024 * 10]; for (int i = 0; i < sourceFiles.length; i++) { // 創建ZIP實體,並添加進壓縮包 ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName()); zos.putNextEntry(zipEntry); // 讀取待壓縮的文件並寫進壓縮包裏 fis = new FileInputStream(sourceFiles[i]); bis = new BufferedInputStream(fis, 1024 * 10); int read = 0; while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) { zos.write(bufs, 0, read); zos.flush(); } } flag = true; } } } catch (FileNotFoundException e) { LOGGER.error(e.getMessage(), e); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } finally { try { if (null != bis) bis.close(); if (null != zos) zos.close(); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } } } return flag; } /** * 創建指定目錄 * @param destDirName * @return */ public static boolean createDir(String destDirName) { dirExist = true; File dir = new File(destDirName); if (dir.exists()) { return false; } // 創建目錄 if (dir.mkdirs()) { return true; } else { return false; } } /** * 創建指定文件 * @param destFilePath * @return */ public static File createFile(String destFilePath) { File file = new File(destFilePath); if (file.exists()) { System.out.println("創建文件" + destFilePath + "失敗,目標文件已存在!"); return file; } if (!file.getParentFile().exists()) { // 如果目標文件所在的目錄不存在,則創建父目錄 if (!file.getParentFile().mkdirs()) { LOGGER.error("創建目標文件所在目錄失敗!"); } } try { if (file.createNewFile()) { return file; } } catch (IOException e) { LOGGER.error("創建目標文件失敗!" + e); } return file; } /** * 把指定內容寫入文件 * @param type 字段名 * @param audioId * @param info * @param csspFilePath 路徑 */ public static void writeToFile(String testWorkId, String type, int audioId, String info, String csspFilePath) { String fileName = csspFilePath.substring(csspFilePath.lastIndexOf(/) + 1, csspFilePath.lastIndexOf(.)); String destDir = "resultInfo/" + testWorkId + "/"; if (FileUtil.createDir(destDir)) {//如果沒目錄則創建一個 write(type, info, fileName, destDir); } else { write(type, info, fileName, destDir); } } private static void write(String type, String info, String fileName, String destDir) { File files = new File(destDir + fileName + ".txt"); FileWriter fw = null; try { fw = new FileWriter(files, true); fw.write(type + ":" + info.toString() + "\r\n\r\n"); fw.flush(); } catch (IOException e) { LOGGER.error(e); } finally { if (fw != null) { try { fw.close(); } catch (IOException e) { LOGGER.error("寫入文件失敗"); } } } } }

文件操作的工具類