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. 一維條碼 EAN13 的編碼方式

    何謂 EAN-13碼 ? ▲ 國家代號(3位數) 中華民國的國家代號為471. ▲ 廠商代號(6位數) 由本會核發給廠商6位數的廠商代號. ▲ 商品代號(3位數) 由廠商自行編定,按一物一號的原則,不 ...

  2. ubuntu使用crontab

    crond服务通常被放在/etc/init.d/crond , 这样就可以在系统启动后自动启动crond服务. linux中的用户使用crontab命令来配置cron任务. crontab在/etc目 ...

  3. vc++实现avi文件的操作

    为了对avi进行读写,微软提供了一套API,总共50个函数,他们的用途主要有两类,一个是avi文件的操作,一类是数据流streams的操作. 1.打开和关闭文件 AVIFileOpen ,AVIFil ...

  4. linux 知识整理1linux 常见的目录

    linux 系统现在也是搭配啦图形操作界面. 本人初次学习linux,不是为工作,我的工作是玩Asp.net 的.学习linux 也算是知识的储备吧. 学习linux必须知道一些基本的知识. 目录 用 ...

  5. Grunt的配置和使用(一)

    Grunt的配置和使用(一) Grunt 和 Grunt 的插件都是通过 Node.js 的包管理器 npm 来安装和管理的.为了方便使用 Grunt ,你应该在全局范围内安装 Grunt 的命令行接 ...

  6. zoj 3706 Break Standard Weight

    /*题意:将两个砝码中的其中一个分成两块,三块组合最多有几种情况(可以只有一块,或者两块). 组合情况 i j m 三块砝码 (i+j)-m=m-(i+j) i+j i-j=j-i  i j m (i ...

  7. La=LaULb (单链表)

    #include<stdio.h> typedef struct LNode { int data; struct LNode *next; }LNode,*LinkList; void ...

  8. Redis的入门Demo(java)

    java代码部分参考了:http://www.runoob.com/redis/redis-java.html 在java中使用Redis,需要引入Redis驱动,就是jedis-2.1.0.jar, ...

  9. Android 开发笔记 “Sqlite数据库删除”

    1.代码方式 Context.deleteDatabase(String databaseName);//删除某一数据库 2.设置里面 进入应用程序 ,然后清除数据就ok了

  10. Maven本地

    <localRepository></localRepository>