package simple.file;
import java.util.Properties;
import org.junit.Test;
/**
* 属性文件操作类
* @author Simple Rong
*
*/
public class PropFileHandle {
/**
* 获取classpath下的XML属性文件和Properties属性文件
* @throws Exception
*/
@Test
public void testGetProps1() throws Exception {
Properties props = new Properties();
// props.loadFromXML(PropFileHandle.class.getResourceAsStream("/test.xml")); //由本类来加载classpath下的XML属性文件
props.load(PropFileHandle.class.getClassLoader().getResourceAsStream("test.properties")); //由本类来加载classpath下的.properties属性文件
String username = props.getProperty("username"); //找不到则返回null
String password = props.getProperty("password"); //找不到则返回null
// String username = props.getProperty("username","sa"); //找不到则返回默认值sa
// String password = props.getProperty("password","sa"); //找不到则返回默认值sa
// Assert.assertEquals(username, "simple");
// Assert.assertEquals(password, "rong");
System.out.println(username);
System.out.println(password);
}
}
Properties files example
最新推荐文章于 2025-12-16 20:51:38 发布
本文介绍了一个简单的Java程序,用于从类路径下加载XML属性文件和Properties属性文件,并演示了如何通过键名获取属性值。
3500

被折叠的 条评论
为什么被折叠?



