java中Properites的使用问题 java类中读取资源文件,资源文件是Properites形式的,应该怎么样去读取,返回给我的是一个什么形式的呢?初学者求教。。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://hi.baidu.com/hgd0324/item/1d5e923973b77c4d033edcaf这里面有Java读取Properties文件的六种方法,完全可以看懂,去看看吧!! properties里面应该是这种形式:author=zeigeteam=SUTMOUNTINGcampus=\u5927\u8FDE\u6C11\u65CF\u5B66\u9662使用方法也挺简单的:InputStream in = this.getClass().getResourceAsStream("test.properties"); Properties p = new Properties(); p.load(in);然后直接p.get(author);这样就行了,个人感觉properties里面有可能存了一个Map吧,本人比较懒,未看过源码。 就拿一个普通的jdbc配置文件来说。这个db.properties配置文件一般存放在项目的src目录下private static String username = null; private static String password = null; private static String url = null; static { String path = JdbcUtils.class.getClassLoader().getResource("db.properties").getPath(); Properties properties = new Properties(); try { properties.load(new FileInputStream(path)); String driverClass = properties.getProperty("driver"); username = properties.getProperty("username"); password = properties.getProperty("password"); url = properties.getProperty("url"); } catch (Exception e) { e.printStackTrace(); throw new ExceptionInInitializerError(e); } }上面的代码是放到一个jdbc的工具类中的.配置文件在整个项目运行的过程中只需要加载一次。所以放在静态代码块中。加载完后根据key取出里面的数据就行了。 Properties p = new Properties(); //加载属性文件,注意路径为当前类目录下 p.load(this.getClass().getResourceAsStream("hello.properties")); p.get("hi");//获取值 Properties类继承HashTable类,HashTable是一个线程安全的键值对的集合,Properties类会先将文件中的键值对读取出来,放在内存中的键值对集合中,这一就可以像操控普通的map一样进行get(key)的读取操作了 java 读取文本文件并组装成对象 swing图片显示 关于JFreeChart,有输出到jpeg文件的简单例子么? 用myeclipse开发jsp,用什么界面设计插件呢? 求助,打印三角形问题 怎么获取某文本文件内容的行数? 各位大哥,救救小弟,给小弟我一点信心吧!我真的快要失去学习JAVA的信心了! 苦恼,因水平低,不知如何将这个c写的函数改成java的..... java设计模式之备忘录模式求助 数值转换问题? java读取SFTP服务器上的大数据文件 正则表达式。。匹配 中文跟(中文
这里面有Java读取Properties文件的六种方法,完全可以看懂,去看看吧!!
author=zeige
team=SUTMOUNTING
campus=\u5927\u8FDE\u6C11\u65CF\u5B66\u9662
使用方法也挺简单的:
InputStream in = this.getClass().getResourceAsStream("test.properties");
Properties p = new Properties();
p.load(in);
然后直接p.get(author);这样就行了,
个人感觉properties里面有可能存了一个Map吧,本人比较懒,未看过源码。
private static String username = null;
private static String password = null;
private static String url = null; static {
String path = JdbcUtils.class.getClassLoader().getResource("db.properties").getPath();
Properties properties = new Properties();
try {
properties.load(new FileInputStream(path));
String driverClass = properties.getProperty("driver");
username = properties.getProperty("username");
password = properties.getProperty("password");
url = properties.getProperty("url");
} catch (Exception e) {
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
}上面的代码是放到一个jdbc的工具类中的.配置文件在整个项目运行的过程中只需要加载一次。所以放在静态代码块中。加载完后根据key取出里面的数据就行了。
Properties p = new Properties();
//加载属性文件,注意路径为当前类目录下
p.load(this.getClass().getResourceAsStream("hello.properties"));
p.get("hi");//获取值