单例模式读取配置文件到内存中
2014-01-06 09:55
176 查看
/** * */ package com.cplatform.ir.util; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import org.apache.log4j.Logger; import com.cplatform.ir.engine.module.ShopCategandGradeMoudle; /** * @Description:读取 数据到内存中的工具类。 * @Company: * @author * @date Nov * @version */ public class ReadPopulationShopUtil { private static Logger logger=Logger.getLogger(ReadPopulationShopUtil.class); private static ReadPopulationShopUtil instance = new ReadPopulationShopUtil(); private static Map<String, String> popuFeatureMap3=null; private static Map<String, Map<String, String>> popuFeatureMap2=null; private static Map<String, Map<String, Map<String, String>>> popuFeatureMap1=null; private ReadPopulationShopUtil(){ } public static ReadPopulationShopUtil getInstance(){ if(instance==null){ instance = new ReadPopulationShopUtil(); } return instance; } /** * @Description: */ public Map<String, Map<String, Map<String, String>>> getPopulationShopMap() { if (popuFeatureMap1==null) { popuFeatureMap1=new HashMap<String, Map<String, Map<String, String>>>(); FileInputStream fin=null; try { //存储位置 String path = ShopCategandGradeMoudle.class.getClassLoader().getResource("/shopCategandGradeMoudle.txt").getPath(); fin = new FileInputStream(path); InputStreamReader in = new InputStreamReader(fin,"UTF-8"); BufferedReader bf = new BufferedReader(in); String line; while((line=bf.readLine())!=null){ //遍历每一行的数据 //sex 0 grade 30元以下 0.021868971657516 String[] wordArray=line.split("\t"); if (popuFeatureMap1.get(wordArray[0])!=null) { popuFeatureMap2=popuFeatureMap1.get(wordArray[0]); if (popuFeatureMap2.get(wordArray[1])!=null) { popuFeatureMap3=popuFeatureMap2.get(wordArray[1]); popuFeatureMap3.put(wordArray[2]+"_"+wordArray[3], wordArray[4]); }else { popuFeatureMap3=new HashMap<String, String>(); popuFeatureMap3.put(wordArray[2]+"_"+wordArray[3], wordArray[4]); popuFeatureMap2.put(wordArray[1], popuFeatureMap3); } popuFeatureMap2.put(wordArray[1], popuFeatureMap3); }else { popuFeatureMap3=new HashMap<String, String>(); popuFeatureMap2=new HashMap<String, Map<String,String>>(); popuFeatureMap3.put(wordArray[2]+"_"+wordArray[3], wordArray[4]); popuFeatureMap2.put(wordArray[1], popuFeatureMap3); popuFeatureMap1.put(wordArray[0], popuFeatureMap2); } popuFeatureMap1.put(wordArray[0], popuFeatureMap2); } } catch (IOException e) { logger.error(e.getMessage(), e); } } return popuFeatureMap1; } }
相关文章推荐
- C# 单例模式读取xml配置文件属性值
- Java opencsv--使用 Spring 的 PostConstruct机制,项目启动时,将CSV配置文件读取到内存 map 中
- 单列模式读取properties配置文件
- 单例模式之读取配置文件
- 基于单例模式的配置文件读取方法
- 单例模式将配置文件加载在jvm内存,修改配置文件,可调用reload方法重新加载至内存
- 从配置文件中读取自定义扩展类的模式
- android,保存文件到内存和读取,文件访问权限,文件模式
- 单例模式实现读取配置文件
- JDBC | 从配置文件中读取驱动 (设计模式)
- 这个路口再次遇见你------单例模式在读取配置文件时的应用
- 一份采用单例模式编写,可读取配置文件的代码
- java Config配置文件读取#应用反射、工场模式
- 单例模式实现DBCP数据库连接池,并从ini文件中读取数据库连接池配置参数
- 单例模式读取配置文件只创建一次
- 单例模式的java配置文件读取器
- Java基础之设计模式、读取配置文件、算法、linux命令
- JDOM读取XML配置文件--单例模式
- 配置文件读取类conf.class.php 单例模式应用
- [Java 15 反射机制 ] 工厂模式与 properties 读取配置文件