Properties的使用
Properties的各种相对路径,绝对路径的用法
所在包:

package com.test.javaAPI.collections; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Iterator;
import java.util.Properties;
import java.util.Random;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader; import org.junit.Test; import sun.util.spi.XmlPropertiesProvider; /**
* 见:尚学堂131课, Hashtable,和HashMap的使用类似,区别 一 Hashtable与HashMap的区别(面试题) 1
* Hashtable线程安全,同步,效率相对低下 HashMap线程不安全,非同步,效率相对高 2
* 父类不同,Hashtable的父类是Dictionary,HashMap的父类是Abstractmap 3
* null:Hashtable的键和值不能为null HashMap键最多一个是null,值可以多个null
* 这里着重使用Properties,主要用于读取资源配置文件
*
* @author Wei
* @time 2016年10月1日 下午6:48:34
*/
public class ProprtiesTest {
public static void main(String[] args) throws FileNotFoundException, IOException {
Properties prop = new Properties();
prop.setProperty("name", "weiyongle22");
prop.setProperty("sex", "male");
System.out.println(prop.getProperty("name"));
int r = new Random().nextInt(100);
String fileName = "wyl" + r + ".properties";
String xmlName = "wyl" + r + ".xml";
// 把properties写入到指定的文件中
prop.store(new FileOutputStream(new File("C:/Users/Wei/Desktop/" + fileName)), "i am 评论");
// 把properties读取到xml中 绝对路径
// prop.storeToXML(new FileOutputStream(new File("C:/Users/Wei/Desktop/"
// + xmlName)), "我是注释啊", "UTF-8");
// 相对路径 以当前工程为根路径
prop.storeToXML(new FileOutputStream(new File("src/yonle.xml")), "我是注释啊,哈哈哈", "UTF-8");
// XmlPropertiesProvider p = loadProvider();
// p.store(prop, new FileOutputStream(new
// File("C:/Users/Wei/Desktop"+"wyl.xxx")), "", "UTF-8");
prop.store(new FileOutputStream(new File("src/wyl.properties")), "我是注释,不知道你能不能看到我"); } /**
* 测试从new File(String path)相对路径读取文件
*
* @throws FileNotFoundException
* @throws IOException
*/
@Test
public void testLoad() throws FileNotFoundException, IOException {
Properties p = new Properties();
// 也是相对路径,不过这个相对路径是以项目名为相对路径的,即代表都是在MyTag这个工程下
p.load(new FileInputStream(new File("src/yonle.xml")));
p.load(new FileInputStream(new File("src/wyl.properties")));
System.out.println("" + p.getProperty("name")); } /**
* 重要 .class.getResourceAsStream("类相对路径"),例子:
* ProprtiesTest.class.getResourceAsStream(
* "/com/test/javaAPI/collections/wyl.properties") 使用类相对路径读取配置文件
*
* @throws IOException
*/
@Test
public void testFromClass() throws IOException {
Properties p = new Properties();
// 这种情况用的比较多,以类相对路径获取配置文件,以 "/"为bin
p.load(ProprtiesTest.class.getResourceAsStream("/com/test/javaAPI/collections/wyl.properties"));
// 以 "" 为bin
p.load(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("com/test/javaAPI/collections/wyl.properties"));
String sex = p.getProperty("sex");
System.out.println("sex:" + sex);
} private static XmlPropertiesProvider loadProvider() {
return AccessController.doPrivileged(new PrivilegedAction<XmlPropertiesProvider>() {
public XmlPropertiesProvider run() {
ClassLoader cl = ClassLoader.getSystemClassLoader();
XmlPropertiesProvider provider = loadProviderFromProperty(cl);
if (provider != null)
return provider;
provider = loadProviderAsService(cl);
if (provider != null)
return provider;
return new jdk.internal.util.xml.BasicXmlPropertiesProvider();
}
});
} private static XmlPropertiesProvider loadProviderAsService(ClassLoader cl) {
Iterator<XmlPropertiesProvider> iterator = ServiceLoader.load(XmlPropertiesProvider.class, cl).iterator();
return iterator.hasNext() ? iterator.next() : null;
} private static XmlPropertiesProvider loadProviderFromProperty(ClassLoader cl) {
String cn = System.getProperty("sun.util.spi.XmlPropertiesProvider");
if (cn == null)
return null;
try {
Class<?> c = Class.forName(cn, true, cl);
return (XmlPropertiesProvider) c.newInstance();
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException x) {
throw new ServiceConfigurationError(null, x);
}
}
}
Properties的使用的更多相关文章
- spring无法读取properties文件数据
只讲述异常点,关于怎么配置文件,这里不做说明. 1. controller中无法读取config.properties文件 controller中注入的@Value配置是从servlet-cont ...
- Android local.properties 文件读取
转载请标明出处:http://www.cnblogs.com/zhaoyanjun/p/6202369.html 本文出自[赵彦军的博客] 在Android Studio项目里面有个local.pro ...
- Crystal Clear Applied: The Seven Properties of Running an Agile Project (转载)
作者Alistair Cockburn, Crystal Clear的7个成功要素,写得挺好. 敏捷方法的关注点,大家可以参考,太激动所以转载了. 原文:http://www.informit.com ...
- hibernate-mapping-3.0.dtd;hibernate-configuration-3.0.dtd;hibernate.properties所在路径
hibernate-mapping-3.0.dtd 所在路径:hibernate-release-5.2.5.Final\project\hibernate-core\src\main\resourc ...
- Properties操作指南
一.简介: Properties是java中用的比较多的一个类,表示一个持久的属性集.继承于Hashtable,Properties可从流中加载,也可保存在流中.属性列表中每个键极其对应值共同组成一个 ...
- Titanium.App.Properties 对象
Titanium.App.Properties是用来管理键值对数据的一个很方便的对象.在保存数据的时候,在Ti.App.Properties.setString相对应的Key的值中设置你要保存的值即可 ...
- Android中使用java.util.Properties犯的错
今天尝试使用java.util.Properties来保存应用配置,然而遇到了好几个问题,对于熟悉此内容的来说可能都是猪一样的错误,但难免有像我一样的新手再次遇到,希望此文能有所帮助. 错误1 jav ...
- solr定时更新索引遇到的问题(SolrDataImportProperties Error loading DataImportScheduler properties java.lang.NullPointerException)
问题描述 报如下错误,很显然,问题原因:空指针异常: ERROR (localhost-startStop-1) [ ] o.a.s.h.d.s.SolrDataImportProperties ...
- 读取properties配置文件的方法
一般在.properties文件中配置数据库连接的相关信息,我们需要从中读取信息,以便建立与数据库的连接. 文件目录: application.properties配置信息: url=jdbc:ora ...
- JavaSe:Properties文件格式
Properties文件格式说明 Properties继承自Hashtable,是由一组key-value的集合. 在Java中,常用properties文件作为配置文件.它的格式是什么样的呢? 下图 ...
随机推荐
- 多版本jQuery的使用剖析
</div> </div> <!-- basic scripts --> <!--[if !IE]> --> <!-- <![endi ...
- Michael Kors成了时尚行业的公敌-股票频道-和讯网
Michael Kors成了时尚行业的公敌-股票频道-和讯网 Michael Kors成了时尚行业的公敌 字号 评论 邮件 纠错 2014年03月03日17:32 来源:财经天下 全球消费不 ...
- [ javascript ] 司徒正美的fadeOut-fadeIn效果!
首先感谢司徒正美的文章! 在司徒大神的博客看到一个简单的渐入渐出的效果.全然採用js实现. 例如以下: <!doctype html> <html dir="ltr&quo ...
- Oracle fga审计有这几个特性
fga审计有这几个特性: 本文为原创文章,转载请注明出处: http://blog.csdn.net/msdnchina/article/details/38409057 1.select * fro ...
- 远程登录阿里云上的MySQL
近期对云和server之类的感兴趣,想要将自己的数据什么的保存到远端server.研究了阿里云和百度云.今天算是有点进步吧. 我在阿里云上申请了个免费的云server(ECS),非常可惜仅仅能用5天. ...
- java 实现 一个账号只能在一个地方登陆,其他地方被下线
其实方法有很多的,我这献丑了. 使用理解java 四大作用域. 思路:理解java 四大作用域的关键. 第一个地方登陆: 1.得到请求的SessionId 和 登陆的 用户名 2.把SessionId ...
- 【转】VPN服务器配置详解
参考博文: VPN服务器配置详解 等公司上服务器开始配置 vpn
- 斯坦福 IOS讲义 课件总结 三
1,@property (nonatomic,readwrite)NSInteger score;注意这里有一个只读和只写的属性,readonly. 2,重写初始化方法也可以改名字和传参数,(改名一般 ...
- Linux下安装VNC Server
操作系统centos6.5,在其之上安装vnc server,可利用windows上的vnc client远程登录. 1. 安装 yum install tigervnc-server.x86_64 ...
- Flex的学习资源
学习网站 http://www.adobe.com/cn/devnet/flex.html Adobe Flex开发人员中心 http://www.adobe.com/cn/devnet/flex/v ...