哪位大哥告诉我,怎样将.properties文件,读出后放到一个HASH MAP中,单纯的读出key值和value我会读,另外就是我把读出的内容不放到SESSION里,怎样才能在别的地方调用存储的内容。就是只有在第一次登陆时才访问文件,并读入一个地方存储起来,以后再访问时不再读文件,做的是一个共通的东西,所以没有页面,不能用SESSION存储,哪位高手告诉我,应该怎么存储

解决方案 »

  1.   

    java.util.Properties extends from Hashtable<Object,Object>
    and Hashtable implements Map<Object,Object>if you look at the java doc HashMap, you may notice it implements Map<K,V>, which means you are able to cast a Properties to HashMap. And that's what java allows you to do if you want, since HashMap has a constructor does take a Map as argument.So the code may look like:java.util.Properties rop=new java.util.Properties();
    //read from properties file to Properties 
    java.util.HashMap<Object,Object> map=null;
    if(prop istanceof java.util.Map)
        table=new java.util.HashMap<Object,Object>((java.util.Map<Object,Object>)prop);
    if(table!=null)
     // your logic hereNow you got a hashmap from properties.
    Your second question depends on what framework u choose.Basically, for a traditional JSP/Servlet app, I would prefer to override init() method of the servlet because it is only called once when the app starts. In this scenario, your config file would be read only once and be shared by all users that access this servlet.
      

  2.   

    用Properties类
    这个类实现了Map接口 和HashMap差不多了。import java.util.*;
    import java.io.*;public class test {
    public static void main(String[] args) {
    try {
    Properties pro = new Properties();  //实例化
    pro.load(new FileInputStream("d:\\a.txt")); //从a.txt得到键值对
    System.out.println(pro.get("username")); //得到键为username的值
    } catch (Exception e) {
    // TODO: handle exception
    }
    }
    }
    不放session里? 放application里不知道好不好
      

  3.   


       HashMap sourceMap = new HashMap();
    try{
       Properties   myProperties   =   new   Properties();   
    String filePath = System.getProperty("user.dir")+System.getProperty("file.separator")+"returnMessageAscii.properties";
    InputStream   is   =   new FileInputStream(filePath);        
    if(is!=null){   
    //加载属性文件
    myProperties.load(is);   
    //得到所有的主键信息(这里的主键信息主要是简化的主键,也是信息的关键)
    Enumeration   enmObject =  myProperties.keys();
    //对每一个主键信息进行检索处理,跟传入的返回值信息是否有相同的地方(如果有相同的地方,取出主键信息的属性,传回给返回信息)
    while(enmObject.hasMoreElements()){
    String curKey = (String)enmObject.nextElement();
    String curMessage = myProperties.getProperty(curKey);
    log.info("键:"+curKey+"  值:"+curMessage);
    sourceMap.put(curKey, curMessage);
    }
     } 
    }catch(Exception e){
    log.info(e.getMessage());
    e.printStackTrace();
    }
      

  4.   


    至于怎么读取文件中的键值对,楼上的都说得很清楚了,最好是用Properties类来实现,只要是文件内容是类似这样的:
    key=value
    或者key:value
    使用Properties都能自动解析文件,注意一个键值对为一行不清楚你的登陆是指服务器启动后的,相对服务器第一次登陆,还是用户的第一次登陆:
    如果是指服务器的第一次登陆:
    写一个servlet就可以的 覆盖其int()方法,在方法类加入你的逻辑,并在web.xml中将其<load-on-startup>等级设为1:
    或者写一个ServletContextListener监听器来实现.如果是用户的第一次登陆:
    实现的方法就很多了:
    1.写一个过滤器:
    在过滤器中利用Cookie来做:
    在过滤器中:
    Cookie[] allcookies = request.getCookies();
    String[] values = null;
    if(allcookies == null){
        Properties proper = null;
        try{
        proper = new Properties();  
         proper.load(new FileInputStream("文件路径"));
        }catch(Exception e){
            throw new HttpServletExcetion();
        }
        Set set =proper.keySet();
        for(String s : set)
            Cookie cookie=new Cookie(s(也就是key),proper.get(s));
            response.addCookie(cookie);
        }  
    }
    else{
       for(int i=0;i<allcookies.length ; i++){
         if(allcookies[i].getName().equals("在对应response中addCookie()设置的cookie值")){
                 values[i] = allcookies[i].getValue(); 
         }
         if(allcookies[i].getName().equals("在对应response中addCookie()设置的cookie值")){
                 values[i] = allcookies[i].getValue(); 
         }
         if(.......){
                 ............
         }  }
    }也可以使用用HttpSessionListener来做:具体怎么用去看servlet_api吧在第一种的情况的话 其实你不用Properties类来做 可以直接把key和value写入web.xml中:利用
    <context-param>
        <param-name>key1</param-name>
        <param-value>value1</param-value>
    <context-param>
    <context-param>
        <param-name>key2</param-name>
        <param-value>value2</param-value>
    <context-param><context-param>
        <param-name>key3</param-name>
        <param-value>value3</param-value>
    <context-param>
    来做