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的使用的更多相关文章

  1. spring无法读取properties文件数据

    只讲述异常点,关于怎么配置文件,这里不做说明.   1. controller中无法读取config.properties文件 controller中注入的@Value配置是从servlet-cont ...

  2. Android local.properties 文件读取

    转载请标明出处:http://www.cnblogs.com/zhaoyanjun/p/6202369.html 本文出自[赵彦军的博客] 在Android Studio项目里面有个local.pro ...

  3. Crystal Clear Applied: The Seven Properties of Running an Agile Project (转载)

    作者Alistair Cockburn, Crystal Clear的7个成功要素,写得挺好. 敏捷方法的关注点,大家可以参考,太激动所以转载了. 原文:http://www.informit.com ...

  4. 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 ...

  5. Properties操作指南

    一.简介: Properties是java中用的比较多的一个类,表示一个持久的属性集.继承于Hashtable,Properties可从流中加载,也可保存在流中.属性列表中每个键极其对应值共同组成一个 ...

  6. Titanium.App.Properties 对象

    Titanium.App.Properties是用来管理键值对数据的一个很方便的对象.在保存数据的时候,在Ti.App.Properties.setString相对应的Key的值中设置你要保存的值即可 ...

  7. Android中使用java.util.Properties犯的错

    今天尝试使用java.util.Properties来保存应用配置,然而遇到了好几个问题,对于熟悉此内容的来说可能都是猪一样的错误,但难免有像我一样的新手再次遇到,希望此文能有所帮助. 错误1 jav ...

  8. solr定时更新索引遇到的问题(SolrDataImportProperties Error loading DataImportScheduler properties java.lang.NullPointerException)

    问题描述 报如下错误,很显然,问题原因:空指针异常: ERROR (localhost-startStop-1) [   ] o.a.s.h.d.s.SolrDataImportProperties ...

  9. 读取properties配置文件的方法

    一般在.properties文件中配置数据库连接的相关信息,我们需要从中读取信息,以便建立与数据库的连接. 文件目录: application.properties配置信息: url=jdbc:ora ...

  10. JavaSe:Properties文件格式

    Properties文件格式说明 Properties继承自Hashtable,是由一组key-value的集合. 在Java中,常用properties文件作为配置文件.它的格式是什么样的呢? 下图 ...

随机推荐

  1. HDU 3501 Calculation 2

    题目大意:求小于n的与n不互质的数的和. 题解:首先欧拉函数可以求出小于n的与n互质的数的个数,然后我们可以发现这样一个性质,当x与n互质时,n-x与n互质,那么所有小于n与n互质的数总是可以两两配对 ...

  2. contextServlet

    一:读取配置文件中的参数信息 1.新建servlet文件ContextServlet1,代码为: import java.io.IOException; import java.util.Enumer ...

  3. CodeIgniter框架开发的统计程序源代码开放

    文章来源: PHP开发学习门户 自己初学php时,用CodeIgniter框架开发的后台统计程序源代码 程序部分页面如图: 具体配置及下载源代码:http://bbs.phpthinking.com/ ...

  4. HTML系列(一):创建HTML文档

    从本学期开始我打算把我以前学的知识点系统地总结一下,先从HTML开始.(本系列内容总结自博文视点出版社•代码逆袭系列书籍,包括代码片段.) 一.HTML文档类型 HTML版本众多,浏览器如何得知使用的 ...

  5. BZOJ 2440 完全平方数(莫比乌斯反演+二分查找)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=23362 题意:定义含有平方数因子的数为完全平方数(平方数因子不包含 ...

  6. GE_OG_CALC_COLUMN_EMPTY

    CREATE OR REPLACE PROCEDURE CUST_MKT_DWH.GE_OG_CALC_COLUMN_EMPTY(P_TABLE_NAME IN VARCHAR2) IS --TYPE ...

  7. bzoj 1057: [ZJOI2007]棋盘制作 单调栈

    题目链接 1057: [ZJOI2007]棋盘制作 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 2027  Solved: 1019[Submit] ...

  8. cocos2dx定时器

    cocos2dx三种定时器的使用以及停止schedule,scheduleUpdate,scheduleOnce 首先,什么是定时器呢?或许你有时候会想让某个函数不断的去执行,或许只是执行一次,获取你 ...

  9. ubuntu NFS

    NFS(Network File System, 网络文件系统)可以通过网络将分享不同主机(不同的OS)的目录——可以通过NFS挂载远程主机的目录, 访问该目录就像访问本地目录一样! 一般而言, 使用 ...

  10. android-意图Intent

    Android基本的设计理念是鼓励减少组件间的耦合,因此Android提供了Intent (意图) ,Intent提供了一种通用的消息系统,它允许在你的应用程序与其它的应用程序间传递 Intent 来 ...