import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class FileUtil { // ログオブジェクト private Log log = LogFactory.getLog(this.getClass()); /** * ファイルコピー処理<br> * * ファイルを指定されたディレクトリにコピーします。 * コピー先ディレクトリが存在しない場合は新規に作成されます。 * コピー先に同名のファイルが存在する場合は上書きします。 * * @param srcFile * 対象ファイル * @param destDir * コピー先ディレクトリ * @throws IOException * IO例外 */ public void copyFile( File srcFile, String destDir) throws IOException { String method = "copyFile()"; log.debug(method + " start"); if (!(destDir.lastIndexOf("/") == (destDir.length() - 1) || destDir.lastIndexOf("//") == (destDir.length() - 1))) { // パスの最後に区切り文字がない場合は追加 destDir = destDir + "/"; } File destFile = new File(destDir + srcFile.getName()); log.info("copyFile From:" + srcFile.getPath()); log.info("copyFile To:" + destFile.getPath()); // 対象ファイルをコピー FileUtils.copyFile(srcFile, destFile); log.debug(method + " end"); } /** * ファイル移動処理<br> * * 指定されたファイルを移動先ディレクトリに * コピーした後、コピー元ファイルを削除します。 * * @param srcFile * 移動元ファイル * @param destDir * 移動先ディレクトリ * @return 処理結果 * @throws IOException * IO例外 */ public void moveFile(File srcFile, String destDir) throws IOException { String method = "moveFile()"; log.debug(method + " start"); // ファイルコピー処理を呼び出す copyFile(srcFile, destDir); // コピー元ファイルを削除 deleteFile(srcFile); log.debug(method + " end"); } /** * ファイル削除 * * @param delFile 削除ファイル * @return 削除結果 * @throws IOException */ public void deleteFile(File delFile) throws IOException { String method = "deleteFile()"; log.debug(method + " start"); // ファイルを削除 if (delFile.delete()) { log.info("deleteFile:" + delFile.getPath()); } else { // 削除に失敗した場合はIO例外をスロー throw new IOException("cannot delete file:" + delFile.getPath()); } log.debug(method + " end"); } /** * 対象ファイルリスト取得<br> * * 指定されたディレクトリから、対象となるファイルのリストを取得します。 * * @param srcDir * 対象ディレクトリ * @param regexList * 対象ファイル名の正規表現リスト * @return 対象ファイルリスト */ public File[] listMatchFile(String srcDir, List regexList) { String method = "listMatchFile()"; log.debug(method + " start"); for (int i = 0; i < regexList.size(); i++) { log.info("regex" + i + ":" + (String)regexList.get(i)); } File[] targetFile = null; if (srcDir != null && srcDir.length() > 0) { File srcFile = new File(srcDir); if (srcFile.isDirectory()) { // ファイルのリストを取得 File[] fileList = srcFile.listFiles(); List targetList = new ArrayList(); for (int j = 0; j < fileList.length; j++) { if (fileList[j].isFile()) { // リストの要素がファイルの場合 for (int k = 0; k < regexList.size(); k++) { if (fileList[j].getName().matches( (String) regexList.get(k))) { // 正規表現に一致したファイルをリストに追加 targetList.add(fileList[j]); log.info("matchFile:" + fileList[j].getName()); break; } } } } // ListからFile[]に変換 targetFile = (File[]) targetList.toArray(new File[targetList.size()]); } } log.debug(method + " end"); return targetFile; } /** * 対象ファイルコピー処理<br> * * コピー元ディレクトリに含まれる対象ファイルを * 複数のディレクトリにコピーします。 * * @param srcDir * コピー元ディレクトリ * @param destDirList * コピー先ディレクトリリストs * @param regexList * 対象ファイル名の正規表現リスト * @throws IOException * IO例外 */ public void copyMatchFile( String srcDir, List destDirList, List regexList) throws IOException { String method = "copyMatchFile()"; log.debug(method + " start"); // 対象ファイルリストを取得 File[] fileList = listMatchFile(srcDir, regexList); for (int i = 0; i < fileList.length; i++) { for (int j = 0; j < destDirList.size(); j++) { // コピー先の数分、ファイルをコピー copyFile(fileList[i], destDirList.get(j).toString()); } } log.debug(method + " end"); } /** * 対象ファイル移動処理<br> * * 移動元ディレクトリに含まれる対象ファイルを * 複数のディレクトリにコピー後、 * コピー元ファイルを削除します。 * * @param srcDir * 移動元ディレクトリ * @param destDirList * コピー先ディレクトリリスト * @param regexList * 対象ファイル名の正規表現リスト * @return 処理結果 * @throws IOException * IO例外 */ public void moveMatchFile( String srcDir, List destDirList, List regexList) throws IOException { String method = "moveMatchFile()"; log.debug(method + " start"); // 対象ファイルリストを取得 File[] fileList = listMatchFile(srcDir, regexList); for (int i = 0; i < fileList.length; i++) { for (int j = 0; j < destDirList.size(); j++) { // コピー先の数分、ファイルをコピー copyFile(fileList[i], destDirList.get(j).toString()); } // コピー元ファイルの削除 deleteFile(fileList[i]); } log.debug(method + " end"); } }