configuration file

自我练习

dependence jar file
commons-configuration-1.6\commons-configuration-1.6.jar;
commons-lang-2.4.jar;
commons-logging-1.1.1\commons-logging-1.1.1.jar;
commons-collections-3.2.1\commons-collections-3.2.1.jar


import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.apache.commons.configuration.PropertiesConfiguration;

public class ScheduleConfig {

public static void main(String[] args) throws Exception {

/**
* load by name
*/
PropertiesConfiguration cfg = new PropertiesConfiguration(
"schdule.properties");
String name = cfg.getString("raul");
System.out.println("----------" + name);
Iterator it = null;
it = cfg.getKeys();
while (it.hasNext()) {
// System.out.println(cfg.getString(it.next().toString()));
System.out.println(it.next());
}

String[] str = cfg.getStringArray("raul");
List list = cfg.getList("raul");

for (int i = 0; i < str.length; i++) {
System.out.println(str[i]);
System.out.println(list.get(i));
}

cfg.clear();
// cfg.load(new File("/names.properties"));

// System.out.println(str);

// // String path = new File("schedule.properties").getPath();
/**
* load by file
*/
File file = new File("bin/names.properties");
String path = file.getAbsolutePath();
System.out.println(path+"--------properties after clear ");
cfg.setFile(file);
cfg.load(file);
it = cfg.getKeys();
while (it.hasNext()) {
System.out.println(it.next());
}

// file.delete(); //this method is delete a file from disk
file = new File("bin/jdbc.properties");
InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
System.out.println("user file stream:--->" + file.getName()
+ file.getAbsolutePath() + ">>>>>");
cfg.getInclude(); ///this method make two config file into a file;
cfg.clear();//if you not use properties any more clear it.
cfg.load(isr);
while (isr.read() != -1) {
System.out.println((char) isr.read());
}
it = cfg.getKeys();
while (it.hasNext()) {
System.out.println(it.next());
}

// print system properties
Properties p = System.getProperties();
// p.list(System.out);
System.out.println("______:" + System.getProperty("java.class.path"));


}

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值