import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; public class propfilex { static private class WrongPropertySyntaxException extends IllegalArgumentException { private static final long serialVersionUID = 1L; public WrongPropertySyntaxException(String message) { super(message); } } static Map getKeyValue(String fileName) throws IOException { Map m = new HashMap(); int i = 0; try (FileReader f = new FileReader(fileName); BufferedReader br = new BufferedReader(f)) { while (br.ready()) { String s = br.readLine(); i++; int o = s.indexOf(':'); if (o == 0 || s.substring(0, o).trim().length() == 0) throw new WrongPropertySyntaxException("Missing key name on line #" + i); if (o == -1) throw new WrongPropertySyntaxException("Missing colon on line #" + i); m.put(o == 0 ? "" : s.substring(0, o).trim(), s.substring(o + 1).trim()); } } catch (FileNotFoundException e) { System.out.println("File not found: " + fileName); } return m; } public static void main(String [] args) throws IOException { try { Map m = getKeyValue(args[0]); for (Entry kv : m.entrySet()) { System.out.println(kv.getKey() + ":" + kv.getValue()); } } catch (WrongPropertySyntaxException e) { System.out.println(e.getMessage()); } } }