Skip to content

Instantly share code, notes, and snippets.

@rasient
Created June 18, 2016 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rasient/b414e80182e309d1a8a426541a225c51 to your computer and use it in GitHub Desktop.
Save rasient/b414e80182e309d1a8a426541a225c51 to your computer and use it in GitHub Desktop.
it compares multiple properties files, orders the contents by keys and adds missing keys at the end
package org.alexander.berg;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.*;
public class PropertiesFileManipulator {
//used only for reading
private static String ORIGINAL_PATH = "/home/mrx/workspace/idea_projects/openmrs-core/api/src/main/resources/";
//saving the ordered properties files
private static String NEW_PATH = "/home/mrx/workspace/test/";
//the prefix of the properties files
private static String FILE_PREFIX = "messages";
//the suffix of the properties files
private static String FILE_SUFFIX = "properties";
public static void main(String[] args) {
Set<String> allKeySet = new HashSet<>();
Map<String,Set<String>> fileNameKeySet = new HashMap<>();
new File(ORIGINAL_PATH).listFiles((File dir, String fileName) -> {
if(fileName.startsWith(FILE_PREFIX) && fileName.endsWith(FILE_SUFFIX)) {
try {
Properties properties = new Properties();
properties.load(new FileInputStream(ORIGINAL_PATH + fileName));
Set<String> keySet = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
keySet.addAll(properties.stringPropertyNames());
allKeySet.addAll(keySet);
fileNameKeySet.put(fileName,keySet);
StringBuilder stringBuilder = new StringBuilder();
for (String key : keySet) {
stringBuilder.append(key);
stringBuilder.append("=");
stringBuilder.append(properties.getProperty(key));
stringBuilder.append("\n");
}
Files.write(Paths.get(NEW_PATH +fileName), stringBuilder.toString().getBytes());
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
else {
return false;
}
});
allKeySet.remove(".");
fileNameKeySet.forEach((fileName, keySet) -> {
if (!keySet.containsAll(allKeySet)) {
System.out.println("---------------> " + fileName + " <---------------");
Set<String> missingKeys = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
missingKeys.addAll(allKeySet);
missingKeys.removeAll(keySet);
System.out.println("Missing: " + missingKeys.size() + " | All: " + allKeySet.size());
StringBuilder stringBuilder = new StringBuilder("##########################");
stringBuilder.append("\n");
stringBuilder.append("##### Missing values #####");
stringBuilder.append("\n");
stringBuilder.append("##########################");
stringBuilder.append("\n");
missingKeys.forEach((missingKey) -> {
stringBuilder.append(missingKey);
stringBuilder.append("=");
stringBuilder.append("\n");
});
try {
Files.write(Paths.get(NEW_PATH +fileName), stringBuilder.toString().getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment