www.pudn.com > TXWW.rar > Locale.java


package org.gamecollege.j2me.rpg; 
 
import java.io.InputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.Hashtable; 
 
/** 
 * 文本资源读取类 
 *   
 */ 
public class Locale { 
	//单例 
	private final static Locale instance = new Locale(); 
 
	//映射 
	private static Hashtable map; 
 
	//私有构造函数 
	private Locale() { 
		map = new Hashtable(); 
		InputStream is = null; 
		try { 
			is = this.getClass().getResourceAsStream("/locale.txt"); 
			boolean done = false; 
			char buffer[] = new char[1]; 
 
			StringBuffer line = new StringBuffer(); 
			InputStreamReader isr = new InputStreamReader(is); 
			// read a line and add the entries 
			while (!done) { 
				int bytesRead = isr.read(buffer); 
				if (bytesRead != -1) { 
					//读取一行 
					if (buffer[0] == '\n') { 
						String s = line.toString().trim(); 
						//跳过用'#'开头的行,跳过不带'='的行 
 
						if (!s.startsWith("#") && s.length() > 2 
								&& s.indexOf('=') != -1) { 
							String key = s.substring(0, s.indexOf('=')).trim() 
									.toLowerCase(); 
							String value = s.substring(s.indexOf('=') + 1) 
									.trim(); 
 
							map.put(key, value); 
 
						} 
						line.setLength(0); 
					} else 
						line.append((char) buffer[0]); 
				} else 
					done = true; 
 
			} 
 
		} catch (IOException io) { 
		} 
 
		finally { 
			try { 
				is.close(); 
			} 
 
			catch (IOException io) { 
			} 
		} 
	} 
	 
	/** 
	 * 获取某个key对应的字串 
	 * @param key 
	 * @return 
	 */ 
 
	public static String getString(String key) { 
		String s = (String) map.get(key.toLowerCase()); 
		if (s != null) 
			return s; 
 
		return key; 
	} 
 
}