廖雪峰Java5集合-3Map-Properties的使用
Properties用于读取配置
- properties文件只能使用ASCII码
- #表示注释
- 可以从文件系统读取.properties文件
Properties props = new Properties();
String f = "C:\\conf\\setting.properties";
props.load(new FileInputStream(f));
- 可以从classpath读取.properties文件
Properties props = new Properties();
props.load(getClass().getResourceAsStream("/com/testList/setting.properties"));
- 读取多个.properties文件
- 后读取的key-value会覆盖已读取的key-value。可以把默认的配置放在classpath中,根据环境编写另外的配置。
Properties设计的目的是为了存储String类型的key-value,但Properties实际上是从Hashtable派生的,存在兼容型问题,且未修复,get和put方法不要用
- String getProperty(String key)
- void setProperty(String key,String value)
- Object get(Object key)
- void put(Object key,Object value)
setting.properties
url=https://www.baidu.com
language=Java
course.title=java\u96c6\u5408\u7c7b
Main.java
public class Main {
public static void main(String[] args) throws IOException {
Properties props = new Properties();
props.load(Main.class.getResourceAsStream("/com/testList/setting.properties"));
String url = props.getProperty("url");
String courseTitle = props.getProperty("course.title");
String language = props.getProperty("language");
String desc = props.getProperty("description","no desciption");
System.out.println(url);
System.out.println(courseTitle);
System.out.println(language);
System.out.println(desc);
}
}

## 总结
* Properties用于读写配置文件xx.properties
* .properties文件只能使用ASCII编码
* 可以从ClassPath或文件系统读取.properties文件
* 读写Properties时:
* 仅使用getProperty()/setProperty()方法
* 不要调用继承而来的get()/put()方法
廖雪峰Java5集合-3Map-Properties的使用的更多相关文章
- 廖雪峰Java5集合-3Map-1使用Map
廖雪峰的视频不全,以下是疯狂Java关于Map的讲解 1.Map定义 Map是一种键值映射表,可以通过key快速查找value,同python的dict.key不允许重复,value可以重复. Map ...
- 廖雪峰Java5集合-4Set-1使用Set
集合 Set用于存储不重复的元素集合: boolean add(E e) boolean remove(Object o) boolean contains(Object o) int size() ...
- 廖雪峰Java5集合-1Java集合简介-1Java结合简介
1.集合 定义:集合就是一堆东西.集合里的东西,称为元素Element 数学中的集合: 有限集合: * 一个班所有的学生组成的集合 * 一个网站所有的商品组成的集合 无限集合: * 全体自然数集合 * ...
- 廖雪峰Java5集合-6Stack-1使用Stack
1.栈的定义 栈Stack是一种后进先出(LIFO: Last In First Out)的数据结构,可以看作一端封闭的容器,先进去的元素永远在底部,最后出来. 栈有2个重要的方法: push(E e ...
- 廖雪峰Java5集合-2List-2编写equals方法
List是一种有序链表: List内部按照放入元素的先后顺序存放 每个元素都可以通过索引确定自己的位置 boolean contains(Object o) 是否包含某个元素 int indexOf( ...
- 廖雪峰Java5集合-2List-1使用List
1.List定义 List是一种有序链表: List内部按照元素的先后顺序存放 每个元素都可以通过索引确定自己的位置 类似数组,但大小可变 //List<E>是一种有序链表: //* Li ...
- 廖雪峰Java5Java集合-5Queue-1使用Queue
Queue特性和基本方法 Queue实现一个先进先出(FIFO, First In First Out)的队列.如收银台排队支付. Java中LinkedList实现了Queue接口,可以直接把Lin ...
- 爬取廖雪峰的python3教程
从廖雪峰老师的python教程入门的,最近在看python爬虫,入手了一下 代码比较low,没有用到多线程和ip代理池 然后呢,由于robots.txt的限定,构建了一些user-agent,并放慢的 ...
- 廖雪峰Java6 IO编程-2input和output-4Filter模式
1.JDK提供的InputStream分为两类: 直接提供数据的InputStream * FileInputStream:从文件读取 * ServletInputStream:从HTTP请求读取数据 ...
随机推荐
- 05 面向对象:构造方法&static&继承&方法 &final
构造方法及其重载: /* 构造方法格式特点 * a:方法名与类名相同(大小也要与类名一致) * b:没有返回值类型,连void都没有 * c:没有具体的返回值return; * 构造方法的重载 * 重 ...
- python django day 1
cmd d:\python\python.exe pip install --upgrade pip d:\python\python.exe pip install Django 123.py im ...
- 深入浅出理解 COOKIE MAPPING
转载自:http://www.myttnn.com/digital-marketing/cookie-mapping-introduction/ 在RTB(实时竞价广告,Real-Time-Biddi ...
- day 51 html 学习 js 学习
函数 函数定义 JavaScript中的函数和Python中的非常类似,只是定义方式有点区别 // 普通函数定义 function f1() { console.log("Hello wor ...
- C libraries in Linux
Copy from a book. There are several C libraries to choose from. The main options are as follows: gli ...
- 配置B类内网 和 配置A类内网
首先 A 类网 对应的 子网掩码是255.0.0.0 B 类网 对应的 子网掩码是255.255.0.0 C 类网 对应的 子网掩码是255.255.255.0 一般来说 10 开头的都是 A 类网 ...
- C++Builder XE7 up1 简单测试
很久没用BCB了, 新装了BCBXE7up1试试了,发现有点找不到北了,好像与BCB6的一些默认设置项不一样,编译了一个空APP,提示找不到bpl 和 dll. 设置为不带包编译后,还是提示DLL找不 ...
- SpringBoot入门学习笔记
SpringBoot是SpringMVC的升级版,SpringBoot的特点: application.properties文件配置: server.port = 8080端口配置 server.co ...
- RGB Resampler IP核的测试
关于RGB Resampler IP核的测试 1.RGB Resampler功能描述 将输入的RGB数据流转换成其它格式的RGB数据流. 2.功能验证 设置源图像像素数据为:3X4格式. 设置RGB ...
- Complexity and Tractability (3.44) - The Traveling Salesman Problem
Copied From:http://csfieldguide.org.nz/en/curriculum-guides/ncea/level-3/complexity-tractability-TSP ...