📄 constant.java~2~
字号:
package chat;import java.lang.reflect.*;import java.util.*;import java.io.*;import java.util.*;public class Constant { static Object configObject;//配置类的一个新实例 private Properties config=new Properties();//记录配置项 private String fn=null;//记录配置文件名 /** * * @param name 要得到常量值的培植项的名称 * @return 配置项的值 */ public static String getLang(String name){//使用默认配置类 try{ Class propertyclass = Class.forName("hellojxta.property"); Field field=propertyclass.getField(name); property property=new property(); getproperty getproperty=new getproperty(); return (String)field.get(property); }catch(Exception e){ e.printStackTrace(); } return null; } /** * * @param configClassName 配置类文件名,详细到类的路径 * @param name 要得到常量值的配置项的名称 * @return 相应的常量值 */ public static String getLang(String configClassName,String name){//使用指定配置类 try{ Class configClass=Class.forName(configClassName); Field field=configClass.getField(name); configObject=configClass.newInstance(); return (String)field.get(configObject); }catch(Exception e){ e.printStackTrace(); } return null; } /** * * @param configClassName 配置类文件名,详细到类的路径 * @param name 要得到常量值的配置项名称 * @param value 欲替换ID_VARIABLE的常量值 * @return 相应的常量值和改变后的ID_VARIABLE的值 */ public static String getLang(String configClassName,String name,String value){ try{ Class configClass=Class.forName(configClassName); Field field1=configClass.getField(name); Field field2=configClass.getField("ID_VARIABLE"); configObject=configClass.newInstance(); field2.set(configObject,value); return (String)field1.get(configObject)+(String)field2.get(configObject); }catch(Exception e){ e.printStackTrace(); } return null; } /*public static String getLang(String configClassName,String name,Vector values){ try{ Class configClass=Class.forName(configClassName); Field field1=configClass.getField(name); Field field2=configClass.getField("ID_VARIABLES"); //field2.getType() configObject=configClass.newInstance(); field2.set(configObject,values); return (String)field1.get(configObject)+(String)field2.get(configObject); }catch(Exception e){ e.printStackTrace(); } return null;}*/ /**此构造方法用于新建配置文件 * */ public Constant(){} /**从指定文件名读入配置信息 * * @param fileName 指定的文件名 * @throws ConstantException 异常类 */ public Constant(String fileName)throws ConstantException { try { FileInputStream fin = new FileInputStream(fileName); config.load(fin); //载入文件 fin.close(); } catch (IOException ex) { throw new ConstantException("无法读取指定的配置文件:"+fileName); } fn=fileName; } /**指定配置项名称,返回配置值 * * @param itemName 配置项的名称 * @return 返回配置项的值 */ public String getValue(String itemName){ return config.getProperty(itemName); } /**指定配置项名称和默认值,返回配置值 * * @param itemName * @param defaultValue * @return */ public String getValue(String itemName,String defaultValue){ return config.getProperty(itemName,defaultValue); } /**设置配置项名称及其值 * * @param itemName 配置项名称 * @param value配置项值 */ public void setValue(String itemName,String value){ config.setProperty(itemName,value); return; } /**保存配置文件,指定文件名和抬头描述 * * @param fileName 配置文件的名字 * @param description 配置文件的描述 * @throws ConstantException 异常类 */ public void saveFile(String fileName,String description) throws ConstantException { try { FileOutputStream fout= new FileOutputStream(fileName); config.store(fout, description);//保存文件 fout.close(); } catch (IOException ex) { throw new ConstantException("无法保存指定的配置文件:"+fileName); } } /**保存配置文件,指定文件名 * * @param fileName 配置文件的名字 * @throws ConstantException 异常类 */ public void saveFile(String fileName) throws ConstantException { saveFile(fileName,""); } /**保存配置文件,采用原文件名 * * @throws ConstantException 异常类 */ public void saveFile() throws ConstantException { if(fn.length()==0) throw new ConstantException("需指定保存的配置文件名"); saveFile(fn); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -