📄 configfilereader.java
字号:
package org.osu.ogsa.stream.util;import java.io.*;import java.lang.*;import java.util.Hashtable;public class ConfigFileReader{ Hashtable store; public ConfigFileReader()// constructor { store = new Hashtable(); } public void init(String fileName) { // open the file for reading String s; try { BufferedReader d = new BufferedReader(new FileReader(fileName)); while ( true ) { s = d.readLine();//read next line // System.out.println(s); if ( s == null ) break; else putToTable(s); } d.close(); } catch ( IOException e ) { System.out.println( "File not closed properly\n" + e.toString() ); return; } } private void putToTable(String s) { int lastIndex = s.lastIndexOf('='); // System.out.println("lastIndex of //" + lastIndex); if(lastIndex == -1) { System.err.println("the format of the file is wrong"); return; } String strElement = s.substring(0, lastIndex); String strValue = s.substring(lastIndex + 1); int spaceIndex = strValue.indexOf(' '); if(spaceIndex >= 0) strValue = strValue.substring(0, spaceIndex); store.put(strElement, strValue); //System.out.println(strElement+"=="+strValue); } public String getString(String name) throws Exception { String strValue = (String)store.get(name); if(strValue == null) { throw new Exception("No Proper Name"); } return strValue; } public int getInt(String name) throws Exception { String strValue = (String)store.get(name); if(strValue == null) { throw new Exception("No Proper Name"); } int i; try{ i = Integer.valueOf(strValue).intValue(); } catch(NumberFormatException e) { throw new Exception("parsed as an integer"); } return i; } public boolean getBoolean(String name) throws Exception { String strValue = (String)store.get(name); if(strValue == null) { throw new Exception("No Proper Name"); } else if(strValue.equals("true")) return true; else if(strValue.equals("false")) return false; else throw new Exception("parsed as an integer"); } public double getDouble(String name) throws Exception { String strValue = (String)store.get(name); if(strValue == null) { throw new Exception("No Proper Name"); } double d; try{ d = Double.valueOf(strValue).doubleValue(); } catch(NumberFormatException e) { throw new Exception("parsed as a double"); } return d; }//An example of a main()/* public static void main (String args[] ) { ConfigFileReader myFile = new ConfigFileReader(); myFile.init("a.txt"); try{ System.out.println("aaaa"+myFile.getInt("aaaa")); System.out.println("bbbb"+myFile.getBoolean("bbbb")); System.out.println("cccc"+myFile.getDouble("cccc")); } catch(Exception e) { System.err.println(e.toString()); } } */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -