ファイル・ディレクトリを操作するユーティリティクラスを作成してみた。
1.ユーティリティクラス
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
package util; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.List; /** * <pre> * ファイルに関するユーティリティクラス. * </pre> */ public final class FileUtil { /** * <pre> * コンストラクタ. * </pre> */ private FileUtil() { } /** * <pre> * ディレクトリの作成. * </pre> * @param dirPath ディレクトリパス * @return true:成功、false:失敗 */ public static boolean makeDirs(final String dirPath) { File dir = new File(dirPath); if (dir.exists()) { return true; } return dir.mkdirs(); } /** * <pre> * ディレクトリコピー. * ・中身もコピー * </pre> * @param srcDirPath コピー元ディレクトリパス * @param dstDirPath コピー先ディレクトリパス * @throws IOException 例外 */ public static void copyDir(final String srcDirPath, final String dstDirPath) throws IOException { // コピー元ディレクトリ File srcDir = new File(srcDirPath); // コピー先ディレクトリ File dstDir = new File(dstDirPath); if (!dstDir.exists()) { dstDir.mkdirs(); } for (File srcFileDir : srcDir.listFiles()) { // コピー元ファイル or ディレクトリ名 String srcFileDirName = srcFileDir.getAbsolutePath(); // コピー先ファイル or ディレクトリ名 String newFileDirName = srcFileDirName.replace(srcDirPath, dstDirPath); if (srcFileDir.isDirectory()) { // ディレクトリの場合、再帰処理 copyDir(srcFileDirName, newFileDirName); } else if (srcFileDir.isFile()) { // ファイルの場合(上書き) copyFile(srcFileDirName, newFileDirName, true); } } } /** * <pre> * ディレクトリ移動. * ・中身も移動 * </pre> * @param srcDirPath 移動元ディレクトリパス * @param dstDirPath 移動先ディレクトリパス * @throws IOException 例外 */ public static void moveDir(final String srcDirPath, final String dstDirPath) throws IOException { // 移動元ディレクトリ File srcDir = new File(srcDirPath); // 移動先ディレクトリ File dstDir = new File(dstDirPath); if (!dstDir.exists()) { dstDir.mkdirs(); } for (File srcFileDir : srcDir.listFiles()) { // 移動元ファイル or ディレクトリ名 String srcFileDirName = srcFileDir.getAbsolutePath(); // 移動先ファイル or ディレクトリ名 String newFileDirName = srcFileDirName.replace(srcDirPath, dstDirPath); if (srcFileDir.isDirectory()) { // ディレクトリの場合、再帰処理 moveDir(srcFileDirName, newFileDirName); } else if (srcFileDir.isFile()) { // ファイルの場合(上書き) moveFile(srcFileDirName, newFileDirName, true); } } } /** * <pre> * ディレクトリ削除. * ・中身も削除 * </pre> * @param dirPath ディレクトリパス * @throws IOException 例外 */ public static void deleteDir(final String dirPath) throws IOException { // ディレクトリ or ファイル File dirFile = new File(dirPath); // 存在しない場合終了 if (!dirFile.exists()) { return; } // ディレクトリの場合、再帰処理 if (dirFile.isDirectory()) { for (File childFile : dirFile.listFiles()) { deleteDir(childFile.getAbsolutePath()); } } // 対象がファイル or 空のディレクトリ 削除 dirFile.delete(); } /** * <pre> * ファイル読み込み. * </pre> * @param filePath ファイルパス * @param encode エンコード * @return コンテンツ * @throws IOException 例外 */ public static List<String> readFile(final String filePath, final String encode) throws IOException { List<String> lines = new ArrayList<String>(); // try()内では自動的にクローズされる try (FileInputStream fis = new FileInputStream(filePath); InputStreamReader isr = new InputStreamReader(fis, encode); BufferedReader reader = new BufferedReader(isr);) { String str = ""; while ((str = reader.readLine()) != null) { lines.add(str); } } catch (IOException e) { throw e; } return lines; } /** * <pre> * ファイル作成. * </pre> * @param filePath ファイルパス * @param contents 中身 * @param encode エンコード * @throws IOException 例外 */ public static void makeFile(final String filePath, final String contents, final String encode) throws IOException { // 作成ファイルパス Path srcPath = Paths.get(filePath); // 親フォルダが存在しない場合はディレクトリ作成 if (!Files.exists(srcPath.getParent())) { Files.createDirectories(srcPath.getParent()); } // try()内では自動的にクローズされる try (FileOutputStream fos = new FileOutputStream(filePath); OutputStreamWriter osr = new OutputStreamWriter(fos, encode); BufferedWriter bw = new BufferedWriter(osr); PrintWriter pw = new PrintWriter(bw)) { // ファイル書き込み pw.write(contents); } catch (IOException e) { throw e; } } /** * <pre> * ファイルコピー. * </pre> * @param srcFilePath コピー元ファイルパス * @param dstFilePath コピー先ファイルパス * @param isReplaceExisting 上書き有無(true:上書き) * @throws IOException 例外 */ public static void copyFile(final String srcFilePath, final String dstFilePath, final boolean isReplaceExisting) throws IOException { // コピー元 Path srcPath = Paths.get(srcFilePath); // コピー先 Path dstPath = Paths.get(dstFilePath); // 親フォルダが存在しない場合作成 if (Files.exists(dstPath.getParent())) { Files.createDirectories(dstPath.getParent()); } if (isReplaceExisting) { // ファイルコピー(上書き) Files.copy(srcPath, dstPath, StandardCopyOption.REPLACE_EXISTING); } else { // ファイルコピー Files.copy(srcPath, dstPath); } } /** * <pre> * ファイル移動. * </pre> * @param srcFilePath 移動元ファイルパス * @param dstFilePath 移動先ファイルパス * @param isReplaceExisting 上書き有無(true:上書き) * @throws IOException 例外 */ public static void moveFile(final String srcFilePath, final String dstFilePath, final boolean isReplaceExisting) throws IOException { // 移動元 Path srcPath = Paths.get(srcFilePath); // 移動先 Path dstPath = Paths.get(dstFilePath); // 親フォルダが存在しない場合作成 if (Files.exists(dstPath.getParent())) { Files.createDirectories(dstPath.getParent()); } if (isReplaceExisting) { // ファイル移動(上書き) Files.move(srcPath, dstPath, StandardCopyOption.REPLACE_EXISTING); } else { // ファイル移動 Files.move(srcPath, dstPath); } } /** * <pre> * ファイル削除. * </pre> * @param filePath ファイルパス * @return true:成功、false:失敗 * @throws IOException 例外 */ public static boolean deleteFile(final String filePath) throws IOException { Path path = Paths.get(filePath); return Files.deleteIfExists(path); } } |
以上☆