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. 《转》使用JAVA如何对图片进行格式检查以及安全检查处理

    本文出自冯立彬的博客,原地址:http://www.fenglibin.com/use_java_to_check_images_type_and_security.html 一.通常情况下,验证一个 ...

  2. delphi 7 下安装 indy 10.5.8 教程

    本教程用 indy 10.5.8 替换 delphi 7 自带的 indy 版本,让大家深入了解 delphi 组件安装的方法. 第一步:下载 indy 10.5.8 组件,解压到合适的目录里.如 D ...

  3. android 源代码快速搜索引擎OpenCrok

    使用OpenGrok的Android系统源码搜索引擎,搜索速度简直太快速了!: http://androidxref.com/ 另外,OpenGrok的也是可以在本地计算机中安装配置的,主要是安装To ...

  4. C# DLL文件注册问题(涉及AxInterop.WMPLib.dll等)

    近日遇到问题,给客户安装软件涉及视频等音影播放,安装软件启动过程遇到这样问题: 分析报错原因: 没有注册类别 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG ...

  5. 阻止长按复制页面中的内容;zepto中touch中的应用必须先加载event模块之后;

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Candy----HDU4465----数学题

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4465 题目意思: 有两个箱子,每个箱子装有N个糖果 打开第一个箱子的概率是P,另外一个就是1-P 当小 ...

  7. curl向web服务器发送json数据

    c++使用libcurl: /* *g++ demo.cpp -g -Wall -lcurl */ #include <string.h> #include <stdlib.h> ...

  8. Set 与 Multiset

    Set 与 Multiset 会根据待定的排序准则,自动将元素排序,两者不同之处在于前者不允许元素重复,后者允许,下面介绍一下set中的函数: 一.set 中的 begin.end.rbegin.re ...

  9. C++的常量折叠(三)

    背景知识 在开始之前先说一下符号表,这个编译器中的东西.下面看一下百度百科中的描述: 符号表是一种用于语言翻译器中的数据结构.在符号表中,程序源代码中的每个标识符都和它的声明或使用信息绑定在一起,比如 ...

  10. 数据库水平切分的实现原理解析——分库,分表,主从,集群,负载均衡器(转)

    申明:此文为转载(非原创),文章分析十分透彻,已添加原文链接,如有任何侵权问题,请告知,我会立即删除. 第1章 引言 随着互联网应用的广泛普及,海量数据的存储和访问成为了系统设计的瓶颈问题.对于一个大 ...