Skip to content

Instantly share code, notes, and snippets.

@mapiondev
Created June 8, 2010 00:39
Show Gist options
  • Save mapiondev/429437 to your computer and use it in GitHub Desktop.
Save mapiondev/429437 to your computer and use it in GitHub Desktop.
[java] staticなクラスのフィールドにキャッシュする
a,い
b,ろ
c,は
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* info.txtの中身
* a,い
* b,ろ
* c,は
*
* 使い方
* String result = StaticMemoryUtil.resolve("a");
* System.out.println(result); // い
*
* @author honjo
*
*/
public final class StaticMemoryUtil {
private static final String PATH = "C:/develop/eclipse/pleiades-e3.5-java_20100226/workspace/javatest/info.txt";
private static Map<String, String> map;
static {
init();
}
private StaticMemoryUtil() {
}
public static void init() {
Map<String, String> localMap = new HashMap<String, String>();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(PATH));
String line = null;
while ((line = br.readLine()) != null) {
String[] lineSplit = line.split(",");
localMap.put(lineSplit[0], lineSplit[1]);
}
map = localMap;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static String resolve(String token) {
return map.get(token);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment