Skip to content

Instantly share code, notes, and snippets.

@sdqali
Created October 6, 2011 06:32
Show Gist options
  • Save sdqali/1266674 to your computer and use it in GitHub Desktop.
Save sdqali/1266674 to your computer and use it in GitHub Desktop.
package org.bar.foo;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
public class ObjectMap {
private ArrayList<String> lines;
private HashMap<String, String> objects;
public ObjectMap(String file) {
readLinesFrom(file);
parseObjects();
}
public String idFor(String key) {
return objects.get(key);
}
private void parseObjects() {
objects = new HashMap<String, String>();
for (String line : lines) {
String key = line.split(":")[0].trim();
String value = line.split(":")[1].trim();
objects.put(key, value);
}
}
private void readLinesFrom(String file) {
FileInputStream fstream;
lines = new ArrayList<String>();
try {
fstream = new FileInputStream(file);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = br.readLine()) != null) {
lines.add(line);
}
in.close();
} catch (IOException e) {
System.out.println("Error reading object map file " + file);
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment