import java.io.*;
import java.util.*;

public class EnumProcessModulesDemo {

	public static void main(String[] args) {
		EnumProcessModulesDemo epmd = new EnumProcessModulesDemo("processmodules.txt");
		epmd.printAll();
	}

	// ----------------------------------------------------

	private final String filename;

	public EnumProcessModulesDemo(String filename) {
		this.filename = filename;
	}

	public void printAll() {
		deleteFile();
		callExternalProgram();
		waitForFile();
		final List<String> information = readAll();
		for (String info : information) {
			System.out.println(info);
		}
	}

	private List<String> readAll() {
		final List<String> information = new ArrayList<>();
		try {
			final BufferedReader br = new BufferedReader(new FileReader(filename));
			StringBuilder sb = new StringBuilder();
			String line = "";
			while((line = br.readLine()) != null) {
				if (line.equals("--- END ---")) {
					information.add(sb.toString());
					continue;
				}
				if (line.equals("--- START ---")) {
					sb = new StringBuilder();
					continue;
				}
				sb.append(line + "\n");
			}
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
		return information;
	}

	private void callExternalProgram() {
		try {
			Runtime.getRuntime().exec("EnumProcessModules.exe " + filename);
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	private void deleteFile() {
		final File file = new File(filename);
		file.delete();
	}

	private void waitForFile() {
		final File file = new File(filename);
		while (!(file.isFile() && file.canRead())) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException ie) {
				ie.printStackTrace();
			}
		}
	}
}