ファイル別Grep置換ツール

```java:StartUp.java
package jp.co.replaceTool;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import jp.co.replaceTool.dto.TargetFileDto;

public class StartUp {

	private static final String RESOUCES_FILE_PATH = "C:/pleiades/workspace/ReplaceTool/resouces";
	private static final String OUTPUT_FILE_PATH = "C:/pleiades/workspace/ReplaceTool/output";
	private static final String KEYWORD_FILE_PATH = "C:/pleiades/workspace/ReplaceTool/resouces/keyword";
	private static final String REPLACE_FILE_PATH = "C:/pleiades/workspace/ReplaceTool/resouces/replace";

	public static void main(String[] args) {

		List<TargetFileDto> targetFileDtos = getTargetFiles();
		for (TargetFileDto targetFileDto : targetFileDtos) {

			System.out.println(
					"------------------------------------------------------------------------------------------------");
			System.out.println(getCurrentTimeMillis() + " 変換対象ファイル : " + targetFileDto.getOutputFileName());
			System.out.println(getCurrentTimeMillis() + " 変換前ファイル : " + targetFileDto.getKeywordFileName());
			System.out.println(getCurrentTimeMillis() + " 変換後ファイル : " + targetFileDto.getReplaceFileName());
			List<String> outputFileLines = new ArrayList<String>();
			File outputFile = new File(OUTPUT_FILE_PATH, targetFileDto.getOutputFileName());
			try (BufferedReader reader = new BufferedReader(
					new InputStreamReader(new FileInputStream(outputFile), StandardCharsets.UTF_8));) {
				String line;
				while ((line = reader.readLine()) != null) {
					outputFileLines.add(line);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

			List<String> keywordFileLines = new ArrayList<String>();
			File keywordFile = new File(KEYWORD_FILE_PATH, targetFileDto.getKeywordFileName());
			try (BufferedReader reader = new BufferedReader(
					new InputStreamReader(new FileInputStream(keywordFile), StandardCharsets.UTF_8));) {
				String line;
				while ((line = reader.readLine()) != null) {
					keywordFileLines.add(line);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

			List<String> replaceFileLines = new ArrayList<String>();
			File replaceFile = new File(REPLACE_FILE_PATH, targetFileDto.getReplaceFileName());
			try (BufferedReader reader = new BufferedReader(
					new InputStreamReader(new FileInputStream(replaceFile), StandardCharsets.UTF_8));) {
				String line;
				while ((line = reader.readLine()) != null) {
					replaceFileLines.add(line);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

			List<String> outputDatas = new ArrayList<String>();
			for (int i = 0; i < outputFileLines.size(); i++) {
				boolean isReplace = true;
				for (int j = 0; j < keywordFileLines.size(); j++) {
					if (outputFileLines.size() - 1 < i + j) {
						outputDatas.add(outputFileLines.get(i));
						isReplace = false;
						break;
					} else if (!outputFileLines.get(i + j).equals(keywordFileLines.get(j))) {
						outputDatas.add(outputFileLines.get(i));
						isReplace = false;
						break;
					}
				}
				if (isReplace) {
					for (int j = 0; j < replaceFileLines.size(); j++) {
						outputDatas.add(replaceFileLines.get(j));
					}
					i = i + keywordFileLines.size() - 1;
				}
			}

			try (PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(outputFile)));) {
				for (String outputData : outputDatas) {
					pw.println(outputData);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.out.println(
					"------------------------------------------------------------------------------------------------");
		}

	}

	/**
	 * 対象のファイル、キーワードのファイル、置換ファイルを取得する。
	 * @return
	 */
	private static List<TargetFileDto> getTargetFiles() {

		List<TargetFileDto> targetFiles = new ArrayList<TargetFileDto>();

		for (String line : getFileList(RESOUCES_FILE_PATH, true)) {
			TargetFileDto targetFileDto = new TargetFileDto();
			String[] strs = line.split(",");
			if (strs.length == 3) {
				targetFileDto.setOutputFileName(strs[0]);
				targetFileDto.setKeywordFileName(strs[1]);
				targetFileDto.setReplaceFileName(strs[2]);
				targetFiles.add(targetFileDto);
			}
		}

		return targetFiles;
	}

	private static List<String> getFileList(String absolutePath, boolean isGetAll) {

		List<String> lines = new ArrayList<String>();

		if (isGetAll) {
			List<File> files = findAllFile(absolutePath);
			for (File file : files) {
				try (BufferedReader reader = new BufferedReader(
						new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));) {
					String line;
					while ((line = reader.readLine()) != null) {
						lines.add(line);
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return lines;
	}

	/**
	 * 与えられたディレクトリの下にあるファイルを再帰的に探索する。
	 * @param absolutePath ディレクトリの絶対パス。
	 * @return ファイルの一覧
	 */
	private static List<File> findAllFile(String absolutePath) {

		List<File> files = new ArrayList<>();

		Stack<File> stack = new Stack<>();
		stack.add(new File(absolutePath));
		while (!stack.isEmpty()) {
			File item = stack.pop();
			if (item.isFile())
				files.add(item);

			if (item.isDirectory()) {
				for (File child : item.listFiles())
					stack.push(child);
			}
		}

		return files;
	}

	private static String getCurrentTimeMillis() {
		Timestamp currentTimestamp = new Timestamp(System.currentTimeMillis());
		String currentTimestampToString = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS").format(currentTimestamp);
		return currentTimestampToString;
	}

}

```

```java:TargetFileDto.java
package jp.co.replaceTool.dto;

public class TargetFileDto {

	private String outputFileName;

	private String keywordFileName;

	private String replaceFileName;

	public String getOutputFileName() {
		return outputFileName;
	}

	public void setOutputFileName(String outputFileName) {
		this.outputFileName = outputFileName;
	}

	public String getKeywordFileName() {
		return keywordFileName;
	}

	public void setKeywordFileName(String keywordFileName) {
		this.keywordFileName = keywordFileName;
	}

	public String getReplaceFileName() {
		return replaceFileName;
	}

	public void setReplaceFileName(String replaceFileName) {
		this.replaceFileName = replaceFileName;
	}

}

```