日期:2014-05-20 浏览次数:20866 次
import java.io.InputStream; import java.util.Hashtable; /** * Task: 可以读取配置文件,// 为注释行 * @author JAROD * */ public class TxtReader { private Hashtable hashtable; // 系统的一个词典 public void loadTxt(String nameP) { InputStream is = Resources.getInputStream(nameP); if(is == null) { System.out.println(nameP+"没有找到"); return; } try { String line; hashtable = new Hashtable(); while (true) { line = getLine(is); if(line == null) { break; } if(line.length()>2 & line.charAt(0) != '/') { int split = line.indexOf("="); if(split != -1) { String key = line.substring(0, split); String value = line.substring(split + 1, line.length()); key = key.trim(); value = value.trim(); hashtable.put(key, value); } } } } catch (Exception e) { e.printStackTrace(); } } private String getLine(InputStream is) { int nextByte = 0; byte[] byteArray = new byte[1024]; int count = 0; while(true) { try { nextByte = is.read(); byteArray[count]=(byte)nextByte; ++count; } catch(Exception e) { e.printStackTrace(); } if(nextByte == '\n') { try { return new String(byteArray,0,count,"UTF-8"); } catch(Exception e) { e.printStackTrace(); } } if(nextByte == -1) { return null; } } } public int getIntVal(String key, int defval) { String val = (String) hashtable.get(key); if(val == null) { return defval; } return Integer.parseInt(val); } public String getStringVal(String key, String defval) { String val = (String) hashtable.get(key); if(val == null) { return defval; } return val; } }