java中英文解决方案

/*
 * Created on 2005-9-9
 */
package cn.zami.gcp.common.configtool;

import java.io.FileInputStream;
import java.util.Properties;

/**
 *
 * @author Eric Wu , mlwu@zami.com.cn
 *
 */
public class ConfigProperties {
 protected static final ConfigProperties CP = new ConfigProperties();
 /**
  * get the Instance
  * @return
  */
    public static ConfigProperties newInstance(){
     return CP;
    }
 
 /* public static ConfigProperties newInstance()
 {
   if (CP == null)
   {
     synchronized(ConfigProperties.class) {
       ConfigProperties inst = CP;
       if (inst == null)
       {
         synchronized(ConfigProperties.class) {
           inst = new ConfigProperties();
         }
         CP= inst;
     }
   }
 }
   return CP;
 }*/

 /**
  * setSysNamePath() method sets the SysInit.conf's path,
  * and get the path of other .conf document which user needs
  * @param configType
  * @param configName
  * @return
  */
 public String setSysNamePath(String configType,String configName){
         try {
          Properties props = new Properties() ;
             FileInputStream in = new FileInputStream("SysInit.conf");
             props.load(in);   
             configType = new String(props.getProperty(configType).getBytes("ISO-8859-1"),"GBK"); 
             in.close();
         } catch (Exception e) {
          System.out.println(e.getMessage()+"error!");
             e.printStackTrace();
     }
         return configType;
 }
 public String setSysNamePath(String configName){
  try {
         Properties props = new Properties() ;
            FileInputStream in = new FileInputStream("SysInit.conf");
            props.load(in);   
            configName = new String(props.getProperty(configName).getBytes("ISO-8859-1"),"GBK");
            in.close();
  } catch (Exception e) {
   System.out.println(e.getMessage()+"error!");
            e.printStackTrace();
    }
        return configName;
 }

 /**
  * getConfigNamePath() method gets the path of the file like .conf
  * @param configType
  * @param configName
  * @return
  */
 public String getConfigNamePath(String configType,String configName){
   try{
    Properties props = new Properties();
    FileInputStream in = new FileInputStream(CP.setSysNamePath(configType,configName));
    props.load(in);
    configName = new String(props.getProperty(configName).getBytes("ISO-8859-1"),"GBK"); 
    in.close();
   }catch (Exception e){
    System.out.println(e.getMessage()+"error!");
             e.printStackTrace();
   } 
   return configName; 
  }
    public String getConfigNamePath(String configName){
  configName = CP.setSysNamePath(configName);
  return configName; 
    }
   
    
 
  Properties props = new Properties();    
  /**
   * getConfigHash() method get the path from getconfigNamePath() method,
      * then load the .conf file into the Properties class,create a hashtable
   * @param configType
   * @param configName
   */
  public void getConfigHash(String configType,String configName){
   try{ 
   FileInputStream in = new FileInputStream(CP.getConfigNamePath(configType,configName));
      props.load(in); 
      in.close();
   }catch(Exception e){
    System.out.println(e.getMessage()+"error!");
             e.printStackTrace();
   }
  
  }
  public void getConfigHash(String configName){
   try{ 
    FileInputStream in = new FileInputStream(CP.getConfigNamePath(configName));
       props.load(in);
       in.close();
    }catch(Exception e){
     System.out.println(e.getMessage()+"error!");
              e.printStackTrace();
    }
  }
   
  public  String value;
  public  String bytesSour;
  public  String bytesDst;
  /**
   * GetPropertyValue() method is to get the value from the Hashtanle
   * @param key
   * @return
   */
  public  String getPropertyValue(String key){ 
   try{    
   value = new String(CP.props.getProperty(key).getBytes(bytesSour),bytesDst);
   }catch(Exception e){
      System.out.println(e.getMessage()+"error");
      e.printStackTrace();
     }
     return value;
  }
 
  /**
   * setFont() method is for user to set the font coding
   * @param bytesSource
   * @param bytesDest
   */
  public void setFont(String bytesSource,String bytesDest){
   try{
    Properties props = new Properties() ;
          FileInputStream in = new FileInputStream("SysInit.conf");
          props.load(in);
          bytesSour = new String(props.getProperty(bytesSource).getBytes("ISO-8859-1"),"GBK"); 
          bytesDst = new String(props.getProperty(bytesDest).getBytes("ISO-8859-1"),"GBK"); 
          in.close();
        // value = new String(CP.props.getProperty("file").getBytes("ISO-8859-1"),"GBK");
    }catch(Exception e){
     System.out.println(e.getMessage()+"error!");
     e.printStackTrace();
    }
  }
}

测试类如下:

/*
 * Created on 2005-9-9
 */
package cn.zami.gcp.common.configtool;

/**
 *
 * @author Eric Wu , mlwu@zami.com.cn
 *
 */
public class PropertiesTest {

public static void main(String[] args){
 
 try{
    ConfigProperties CP = ConfigProperties.newInstance();
    CP.setFont("bytes8859","bytesGBK");
    CP.getConfigHash("LanguageConfigFile","GCP_CH");
 
    String file = CP.getPropertyValue("file");
    String options = CP.getPropertyValue("options");
    String toolset = CP.getPropertyValue("toolset");
    String help = CP.getPropertyValue("help");
    String changefont = CP.getPropertyValue("changefont");
    String exit = CP.getPropertyValue("exit");
 
  System.out.println(file);
        System.out.println(options);
        System.out.println(toolset);
        System.out.println(help);
        System.out.println(changefont);
        System.out.println(exit);
 }catch(Exception e){
  System.out.println(e.getMessage());
  e.printStackTrace();
 }
  

 try{
     ConfigProperties CP = ConfigProperties.newInstance();
     CP.setFont("bytes8859","bytesGBK");
     CP.getConfigHash("GCP_EN");
         
     String file = CP.getPropertyValue("file");
     String options = CP.getPropertyValue("options");
     String toolset = CP.getPropertyValue("toolset");
     String help = CP.getPropertyValue("help");
     String changefont = CP.getPropertyValue("changefont");
     String exit = CP.getPropertyValue("exit");
   
   System.out.println(file);
         System.out.println(options);
         System.out.println(toolset);
         System.out.println(help);
         System.out.println(changefont);
         System.out.println(exit);
  }catch(Exception e){
   System.out.println(e.getMessage());
   e.printStackTrace();
  }
}
}

一些说明:

  首先来说一下调用这个类的方法,

try{

          ConfigProperties CP = ConfigProperties.newInstance();         //1

          CP.setFont("bytes8859","bytesGBK");                       //2  

          CP.getConfigHash("LanguageConfigFile","GCP_CH");         //3

        

           String value = CP.GetPropertyValue("key");                  //4  

             System.out.println(value);

      

       }catch(Exception e){

              System.out.println(e.getMessage());

              e.printStackTrace();

       }

 

 

try{

ConfigProperties CP = ConfigProperties.newInstance();

CP.setFont("bytes8859","bytesGBK");

CP.getConfigHash("GCP_EN");                              //5

                           

String value = CP.GetPropertyValue("key");

System.out.println(value);

}catch(Exception e){

     System.out.println(e.getMessage());

     e.printStackTrace();

}

//1 处代码做的事情是调用ConfigProperties类的newInstance() 方法生成一个实例,用实例调用方法getConfigHash() 来得到一张Hashtable

//2 处代码做的事情是对字体编码方式进行处理,其中bytes8859bytesGBK为用户自己定义的变量,具体在配置文件中修改其值,前者用于将中文字符以某种编码方式(如“ISO-8859 -1” )转化成字节数组,而后者(如 “GBK”)是目标编码方式。举例如下:

String s2 = new String(s1.getBytes(“ISO-8859 -1” ),”GBK”);

我们从以ISO-8859-1方式编码的数据库中读出中文字符串 s1 ,经过上述转换过程,在支持 GBK 字符集的操作系统和应用软件中就能够正确显示中文字符串 s2

//3 处的代码所做的事情为从配置文件中的value值导入到properties类中,获得一张Hashtable表。

//4处的代码所做的事情为用户根据在配置文件中配置的 key值来得到value

//3//5getPropertiyValue()方法区别在于参数的不同,3//LanguageConfigFile定义的是配置文件的所在目录,GCP_CH为具体的配置文件名;而5//处提供了用户只需要给出配置文件名即可查找配置文件的方法。之所以这样设计,在3//中加一个配置文件目录变量是为了用户能更好的管理配置文件。


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值