***********************************.properties文件****************************
public static void main(String[] args) throws Exception {
Properties prop = new Properties();//属性集合对象
FileInputStream fis = new FileInputStream("engineToolTestSrc/config/config.properties");//属性文件输入流
prop.load(fis);//将属性文件流装载到Properties对象中
fis.close();//关闭流
//获取属性值,sitename已在文件中定义
System.out.println("获取属性值:name=" + prop.getProperty("name"));
//获取属性值,country未在文件中定义,将在此程序中返回一个默认值,但并不修改属性文件
System.out.println("获取属性值:password=" + prop.getProperty("password", "中国"));
System.out.println("获取" + prop.keySet().size());
System.out.println("获取" + prop.values().size());
System.out.println("获取" + prop.stringPropertyNames().size());
Enumeration enums = prop.keys();
while (enums.hasMoreElements()) {
Object key = enums.nextElement();
System.out.println("key = " + key + " value = " + prop.get(key));
}
// //修改sitename的属性值
// prop.setProperty("name", "Boxcode");
// //添加一个新的属性studio
// prop.setProperty("password", "Boxcode Studio");
// //文件输出流
// FileOutputStream fos = new FileOutputStream("engineToolTestSrc/config/config.properties");
// //将Properties集合保存到流中
// prop.store(fos, "Copyright (c) Boxcode Studio");
// fos.close();//关闭流
}
######################################.nin文件###########################
public class FileUtil {
/**
* 读取INI配置
* @param file INI配置文件完整路径
* @param sec 项
* @param key 键
* @param defaults 默认值
* @return
*/
@SuppressWarnings("unchecked")
public static String GetPrivateProfileString(String file,String sec,String key,String defaults)
{
String result = defaults;
Map map = getIniAllValue(file);
if(map==null)
return result;
ArrayList section = (ArrayList)map.get(sec);
if(section!=null)
{
Iterator iter = section.iterator();
while(iter.hasNext()){
String[] kv = (String[])iter.next();
if(kv!=null && kv[0].equals(key.trim())){
return dealCorpsSign(kv[1],2);
}
}
}
return defaults;
}
/**
* 写入配置 INI文件
* @param file INI配置文件完整路径
* @param sec 项
* @param key 键
* @param value 值
* @return
*/
@SuppressWarnings("unchecked")
public static boolean WritePrivateProfileString(String file,String sec,String key,String value)
{
value = dealCorpsSign(value,1);
Map map = getIniAllValue(file);
if(map==null)
{
map = new HashMap();
ArrayList section = new ArrayList();
section.add(new String[]{key,value});
map.put(sec, section);
}
else {
int x = 0,y = 0;
ArrayList al = (ArrayList)map.get(sec);
if(al!=null){
Iterator iter = al.iterator();
while(iter.hasNext()){
x++;
String[] kv = (String[])iter.next();
if(kv!=null && kv[0].equals(key)){
kv[1] = value;
y++;
}
}
}
if(x==0){
ArrayList section = new ArrayList();
section.add(new String[]{key,value});
map.put(sec, section);
}
else if(y==0){
al.add(new String[]{key,value});
map.put(sec, al);
}
}