java读取配置文件
来源:互联网 发布:淘宝网店运营流程 编辑:程序博客网 时间:2024/06/01 07:43
package com.tianjian.property.jieshun.utils;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* 配置文件帮助类,完成属性文件的加载及缓存
*
* @author 梁晓磊
*
*/
public class ConfigHelper {
// 缓存
private static HashMap<String, Properties> cached = new HashMap<String, Properties>();
/**
* 根据属性文件名加取配置文件内容
*
* @param fileName
* 属性文件名
* @return 返回Properties类型的属性文件
*/
public static Properties getProperties(StringfileName) {属性文件名(.properties的名字,和.class文件名一样)
// 如果缓存已有,即从缓存中取
if (cached.containsKey(fileName)) {
return cached.get(fileName);
}
// 否则,从文件系统加载及缓存起来
//Properties prop = loadProperties("config/" + fileName);
Properties prop = loadProperties("src/main/resources/config/" + fileName);//红色部分为根目录
if (prop != null) {
cached.put(fileName, prop);
}
System.out.println();
return prop;
}
/**
* 从文件 系统中加载指定的属性文件
*
* @param fileName
* @return
*/
private static Properties loadProperties(String fileName) {
try {
String pattern = "\\.properties$/";
File file = new File(fileName.matches(pattern) ? fileName
: fileName + ".properties");
Properties prop = new Properties();
// prop.load(new FileInputStream(file));
prop.load(new FileReader(file));
return prop;
} catch (IOException e) {
e.printStackTrace();
}
Map<String, Object> map = new HashMap<String, Object>();
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
list.add(map);
return null;
}
}
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* 配置文件帮助类,完成属性文件的加载及缓存
*
* @author 梁晓磊
*
*/
public class ConfigHelper {
// 缓存
private static HashMap<String, Properties> cached = new HashMap<String, Properties>();
/**
* 根据属性文件名加取配置文件内容
*
* @param fileName
* 属性文件名
* @return 返回Properties类型的属性文件
*/
public static Properties getProperties(StringfileName) {属性文件名(.properties的名字,和.class文件名一样)
// 如果缓存已有,即从缓存中取
if (cached.containsKey(fileName)) {
return cached.get(fileName);
}
// 否则,从文件系统加载及缓存起来
//Properties prop = loadProperties("config/" + fileName);
Properties prop = loadProperties("src/main/resources/config/" + fileName);//红色部分为根目录
if (prop != null) {
cached.put(fileName, prop);
}
System.out.println();
return prop;
}
/**
* 从文件 系统中加载指定的属性文件
*
* @param fileName
* @return
*/
private static Properties loadProperties(String fileName) {
try {
String pattern = "\\.properties$/";
File file = new File(fileName.matches(pattern) ? fileName
: fileName + ".properties");
Properties prop = new Properties();
// prop.load(new FileInputStream(file));
prop.load(new FileReader(file));
return prop;
} catch (IOException e) {
e.printStackTrace();
}
Map<String, Object> map = new HashMap<String, Object>();
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
list.add(map);
return null;
}
}
阅读全文
0 0
- java读取配置文件
- java读取配置文件
- java 读取properties配置文件
- Java读取properties配置文件
- JAVA读取配置文件路径
- java读取xml配置文件
- JAVA读取配置文件
- Java读取配置文件
- Java配置文件读取
- java读取xml配置文件
- java读取properties配置文件
- java 读取配置文件properties
- java读取配置文件
- Java读取配置文件
- java读取配置文件
- java读取配置文件property
- java 读取*.properties配置文件
- JAVA [ 配置文件的读取 ]
- 欢迎使用CSDN-markdown编辑器
- WEB audio--播放上传的音乐
- a标签href值解析
- 亮风台HIAR增强现实SDK的使用入门(扫描一张图片,在图片上出现一个物体)
- 三级联动实例代码
- java读取配置文件
- mysql——数据库设计中int与varchar中的长度含义
- PHP 5大运行模式
- 剑指offer——调整数组顺序使奇数位于偶数前面
- 利用xib自定义cell xib里面包含两个cell时点击Button没有响应问题
- git学习
- MySQL入门
- android.os.NetworkOnMainThreadException
- The user specified as a definer ('root'@'%') does not exist 解决方法