Properties类与配置文件
//加载文件
public static void testLoadProperties() throws Exception {
Properties properties = new Properties(); InputStream in = new FileInputStream("E:/itcast/config.properties");
properties.load(in); // 加载
in.close(); System.out.println(properties);
}
//写配置文件
public static void testStoreProperties() throws Exception { // 准备配置信息 Properties properties = new Properties(); properties.setProperty("name", "李四"); properties.setProperty("age", "20"); // 准备 OutputStream out = new FileOutputStream("d:/my.properties"); String comments = "这是我的配置文件"; // 写出去 properties.store(out, comments); out.close(); }
static Properties properties ;
static{
try {
properties = new Properties();
Class clazz = dd.class;
InputStream inputStream = clazz.getResourceAsStream("/my.properties"); // "/"������Classpath��·���� getResourceAsStream �÷�������ʹ�õ�·������ʹ�������ļ�·����
properties.load(inputStream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println("��ǰ·����"+ new File(".").getAbsolutePath() );
System.out.println( properties.getProperty("userName")+":"+properties.getProperty("password"));
}

Properties类对应.properties文件。文件内容是键值对,键值对之间使用"="或空格隔开。开头是"#"的表示注释
Properties类在加载.properties文件时使用的iso8859-1的编码。所以这个文件中的中文要特殊处理:如果这个配置文件中有中文就必须要进行转义,使用native2ascii.exe命令操作:
native2ascii d:/my.properties d:/my2.properties
使用Properties类中的load(InputStream) 方法可以加载配置文件,使用其中的store(OutputStream) 方法可以保存配置到指定文件。
更多的信息可以看Properties类的API文档。
/**
* 1. getRealPath读取,返回资源文件的绝对路径
*/
/*String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
System.out.println(path);
File file = new File(path);
FileInputStream in = new FileInputStream(file);*/ /**
* 2. getResourceAsStream() 得到资源文件,返回的是输入流
*/
InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties"); Properties prop = new Properties();
//读取资源文件
prop.load(in); String user = prop.getProperty("user");
String password = prop.getProperty("password");
System.out.println("user="+user);
System.out.println("password="+password); }
Properties类与配置文件的更多相关文章
- Java 数据类型:集合接口Map:HashTable;HashMap;IdentityHashMap;LinkedHashMap;Properties类读取配置文件;SortedMap接口和TreeMap实现类:【线程安全的ConcurrentHashMap】
Map集合java.util.Map Map用于保存具有映射关系的数据,因此Map集合里保存着两个值,一个是用于保存Map里的key,另外一组值用于保存Map里的value.key和value都可以是 ...
- 使用java.util.Properties类读写配置文件
J2SE 1.5 以前的版本要求直接使用 XML 解析器来装载配置文件并存储设置,虽说也并非难事,相比 java.util.Properties却要做额外的解析工作.而java.util.Proper ...
- Java Properties 类读配置文件保持顺序
前几天,公司项目中有一个需求是读取配置文件的,而且最好能够保证加载到内存中的顺序能够和配置文件中的顺序一致,但是,如果使用 jdk 中提供的 Properties 类的话,读取配置文件后,加载到内存中 ...
- JDBC中使用Properties类及配置文件的操作
同时发布于:https://blog.csdn.net/Activity_Time/article/details/81149710 一.properties配置文件 开发中获得连接的4个参数(驱动. ...
- day1 java基础回顾- Properties类与配置文件
Properties配置文件说明 Properties类对应.properties文件.文件内容是键值对,键值对之间使用"="或空格隔开.开头是"#"的表示注释 ...
- java properties类读取配置文件
1.JAVA Properties类,在java.util包里,具体类是java.util.properties.Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值 ...
- Properties类读取配置文件
package com.wzy.t4; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFound ...
- 基于Java Properties类设置本地配置文件
一.Java Properties类介绍 Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件, ...
- Java中Properties类知识的总结
一.Properties类与配置文件 注意:是一个Map集合,该集合中的键值对都是字符串.该集合通常用于对键值对形式的配置文件进行操作. 配置文件:将软件中可变的部分数据可以定义到一个文件中,方便以后 ...
随机推荐
- springboot 解决 The bean 'userRepository', defined in null, could not be registered. A bean with that name has already been defined in file XXX and overriding is disabled.
1.springboot 启动时报错: 2019-02-20 14:59:58.226 INFO 10092 --- [ main] c.f.s.SpringbootssmApplication : ...
- 20170814xlVBA PowerPoint分类插图加说明
Public Sub AddPictures() Dim ppApp As PowerPoint.Application Set ppApp = New PowerPoint.Application ...
- 卸载 PrestaShop 1.7
PrestaShop 的卸载非常简单: 在你的 Web 服务器上删除所有 PrestaShop 的文件和目录.你可以使用 FTP 客户端,你也可以使用 SSH 工具. 使用数据库工具删除数据库中所有以 ...
- 44 CSS 浮动 模态框 定位
一.浮动 float : 浮动的盒子不占原来的位置,其下方的盒子会上移 父盒子会发生塌陷现象.同一级盒子right浮动,同级左边的盒子需要左浮动,right浮动的盒子才能上来 由于浮动框不在文档的普通 ...
- CF-500div2-A/B/C
A. Piles With Stones time limit per test 1 second memory limit per test 256 megabytes input standard ...
- POJ-1511 Invitation Cards (双向单源最短路)
Description In the age of television, not many people attend theater performances. Antique Comedians ...
- Leetcode 17
//狂练回溯,巧用备胎class Solution { public: vector<string> letterCombinations(string digits) { vector& ...
- 【转】C# 生成二维码并且在中间加Logo(图片合并)
public class QRCodeHelper { public static Bitmap GetThumbnail(Bitmap b, int destHeight, int destWidt ...
- forget word a out 1
a 1★ a 不,非,无;在~ 的 2★ ab 相反,变坏,离去 3★ abs 相反,变坏,离去
- Chrome nacl开启
Chrome nacl开启