一、前言
基于empire-db-codegen-2.4.2.jar源码包中的org.apache.empire.db.codegen.util.FileUtils文件工具类,对清除指定文件目录cleanDirectory、删除指定文件deleteFiles、文件读取readObject、文件对象读写序列化转换writeObject/readObject等。
二、源码说明
package org.apache.empire.db.codegen.util;@b@@b@import java.io.BufferedReader;@b@import java.io.BufferedWriter;@b@import java.io.Closeable;@b@import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.FileNotFoundException;@b@import java.io.FileOutputStream;@b@import java.io.FileReader;@b@import java.io.FileWriter;@b@import java.io.IOException;@b@import java.io.ObjectInputStream;@b@import java.io.ObjectOutputStream;@b@import org.slf4j.Logger;@b@import org.slf4j.LoggerFactory;@b@@b@public class FileUtils@b@{@b@ private static final Logger log = LoggerFactory.getLogger(FileUtils.class);@b@ public static final String SEPARATOR = System.getProperty("file.separator");@b@@b@ public static File getFileFromPackage(File target, String pack)@b@ {@b@ StringBuilder sb = new StringBuilder();@b@ sb.append(target.getAbsolutePath()).append(SEPARATOR);@b@ sb.append(pack.replace(".", SEPARATOR));@b@ File f = new File(sb.toString());@b@ if (!(f.exists()))@b@ f.mkdirs();@b@@b@ return f;@b@ }@b@@b@ public static void cleanDirectory(File directory)@b@ {@b@ String[] children;@b@ int i;@b@ if (directory.isDirectory())@b@ {@b@ children = directory.list();@b@ for (i = 0; i < children.length; ++i)@b@ {@b@ boolean success;@b@ File child = new File(directory, children[i]);@b@ if (child.isDirectory())@b@ {@b@ success = deleteDirectory(child);@b@ }@b@ else@b@ {@b@ success = child.delete();@b@ }@b@ if (!(success));@b@ }@b@ }@b@ }@b@@b@ public static boolean deleteDirectory(File directory)@b@ {@b@ String[] children;@b@ int i;@b@ boolean globalSuccess = true;@b@@b@ if (directory.isDirectory())@b@ {@b@ children = directory.list();@b@ for (i = 0; i < children.length; ++i)@b@ {@b@ success = deleteDirectory(new File(directory, children[i]));@b@ globalSuccess = (success) && (globalSuccess);@b@ }@b@ }@b@@b@ boolean success = directory.delete();@b@ globalSuccess = (success) && (globalSuccess);@b@ return globalSuccess;@b@ }@b@@b@ public static void deleteFiles(File directory)@b@ {@b@ String[] children;@b@ int i;@b@ if (directory.isDirectory()) {@b@ children = directory.list();@b@ for (i = 0; i < children.length; ++i)@b@ new File(directory, children[i]).delete();@b@ }@b@ }@b@@b@ public static Object readObject(String fileName)@b@ {@b@ Object o = null;@b@ ObjectInputStream ois = null;@b@ try {@b@ ois = new ObjectInputStream(new FileInputStream(fileName));@b@@b@ o = ois.readObject();@b@ } catch (FileNotFoundException e) {@b@ e.printStackTrace();@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ } catch (ClassNotFoundException e) {@b@ e.printStackTrace();@b@ } finally {@b@ close(ois);@b@ }@b@ return o;@b@ }@b@@b@ public static void writeObject(String fileName, Object o)@b@ {@b@ ObjectOutputStream oos = null;@b@ try {@b@ oos = new ObjectOutputStream(new FileOutputStream(fileName));@b@@b@ oos.writeObject(o);@b@ } catch (FileNotFoundException e) {@b@ e.printStackTrace();@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ } finally {@b@ close(oos);@b@ }@b@ }@b@@b@ public static String getFileAsString(File file)@b@ {@b@ StringBuilder sb = new StringBuilder();@b@ BufferedReader br = null;@b@ try {@b@ br = new BufferedReader(new FileReader(file));@b@@b@ while ((line = br.readLine()) != null)@b@ {@b@ String line;@b@ sb.append(line).append("\n");@b@ }@b@ }@b@ catch (IOException e)@b@ {@b@ log.error(e.getMessage(), e);@b@ }@b@ finally@b@ {@b@ close(br);@b@ }@b@ return sb.toString();@b@ }@b@@b@ public static void writeStringToFile(File file, String contents)@b@ {@b@ BufferedWriter bw = null;@b@ try {@b@ if (!(file.exists()))@b@ {@b@ file.createNewFile();@b@ }@b@@b@ bw = new BufferedWriter(new FileWriter(file));@b@ bw.write(contents);@b@ }@b@ catch (IOException e)@b@ {@b@ log.error(e.getMessage(), e);@b@ }@b@ finally@b@ {@b@ close(bw);@b@ }@b@ }@b@@b@ public static void replaceAll(File file, String input, String output)@b@ {@b@ String fileText = getFileAsString(file);@b@ StringBuilder sb = new StringBuilder(fileText);@b@ int index = -1;@b@ int length = input.length();@b@ while ((index = sb.indexOf(input)) != -1)@b@ {@b@ sb.replace(index, index + length, output);@b@ }@b@ BufferedWriter bw = null;@b@ try {@b@ bw = new BufferedWriter(new FileWriter(file));@b@@b@ bw.write(sb.toString());@b@ }@b@ catch (IOException e)@b@ {@b@ log.error(e.getMessage(), e);@b@ }@b@ finally@b@ {@b@ close(bw);@b@ }@b@ }@b@@b@ public static void close(Closeable closeable)@b@ {@b@ if (closeable != null)@b@ {@b@ try@b@ {@b@ closeable.close();@b@ }@b@ catch (IOException e)@b@ {@b@ log.warn(e.getMessage());@b@ }@b@ }@b@ }@b@}