Properties文件及与之相关的System.getProperties操作(转)
简述properties文件的结构和基本用法
结构:扩展名为properties的文件,内容为key、value的映射,例如"a=2"
示例用到的properties文件:
test.properties
a=testA
b:testB
package properties; import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties; public class PropertiesManipulate { public static void main(String[] args) throws IOException {
readProperties();
readSystemProperties();
} private static void readSystemProperties() {
Properties props = System.getProperties();
Enumeration<?> prop_names = props.propertyNames();
while (prop_names.hasMoreElements()) {
String prop_name = (String) prop_names.nextElement();
String property = props.getProperty(prop_name);
System.out.println("Property \"" + prop_name + "\" is \"" + property
+ "\"");
}
} private static void readProperties() throws FileNotFoundException,
IOException {
String name = "test.properties";
InputStream in = new BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);
System.out.println("a==>" + p.getProperty("a"));
} }
输出:
a==>testA
Property "java.runtime.name" is "Java(TM) SE Runtime Environment"
...(omit)
写:
System.setProperties(props);
http://pda158.iteye.com/blog/2160442
Properties文件及与之相关的System.getProperties操作(转)的更多相关文章
- java代码如何读取properties文件
我们在开发工程中,有时候需要在Java代码中定义一些在部署生产环境时容易改变的变量,还需要我们单独放在一个外部属性文件中,方便我们将来修改.这里列出了两种比较方便的方式. 一.在Spring配置文件中 ...
- Java代码操作properties文件(读取,新增/修改,删除)
项目中需要用到操作properties文件中的数据,记录一下 package com.bonc.savepic.save; import java.io.FileNotFoundException; ...
- 读取Properties文件的六种方法
1.使用java.util.Properties类的load()方法 示例: InputStream in = new BufferedInputStream(new FileInputStream( ...
- 【spring boot logback】日志使用自定义的logback-spring.xml文件后,application.properties中关于日志的相关配置还会起作用么
本篇 将针对[日志使用自定义的logback-spring.xml文件后,application.properties中关于日志的相关配置还会起作用么]这一个主题进行探索. 这个测试项目是根据[spr ...
- 关于properties文件在项目中的使用
这个是当时在学习JDBC的时候老师给讲的.web项目中把一些常用的用户名和密码都填写到一个对应的配置文件中,这样每次修改密码或者用户名的时候就可以直接修改这个配置文件了,不用动源码. 老师讲了两种读取 ...
- 【Spring源码分析】.properties文件读取及占位符${...}替换源码解析
前言 我们在开发中常遇到一种场景,Bean里面有一些参数是比较固定的,这种时候通常会采用配置的方式,将这些参数配置在.properties文件中,然后在Bean实例化的时候通过Spring将这些.pr ...
- SpringBoot读取application.properties文件
http://blog.csdn.net/cloume/article/details/52538626 Spring Boot中使用自定义的properties Spring Boot的applic ...
- properties文件读取工具类
项目中防止硬编码,经常会将一些与业务相关的数据存在properties文件中,根据不同的key加载不同的数据,所以就会有读取properties文件的东西,这篇文章仅为了以后copy方便写的. 1.添 ...
- spring配置文件引入properties文件:<context:property-placeholder>标签使用总结
一.问题描述: 1.有些参数在某些阶段中是常量,比如: (1)在开发阶段我们连接数据库时的连接url.username.password.driverClass等 (2)分布式应用中client端访问 ...
随机推荐
- spring Annotation 笔记2.1
使用注解替代xml 在前几章的笔记基础上添加使用注解的形式 1.配置applicationContext 添加context schema <?xml version="1.0&quo ...
- STL string 模拟
下面的代码来自c++ primer plus第5版第12章,书中代码写的非常好: // string1.h -- fixed and augmented string class definition ...
- Girls and Boys(匈牙利)
Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- BZOJ 3240([Noi2013]矩阵游戏-费马小定理【矩阵推论】-%*s-快速读入)
3240: [Noi2013]矩阵游戏 Time Limit: 10 Sec Memory Limit: 256 MB Submit: 123 Solved: 73 [ Submit][ St ...
- error: property's synthesized getter follows Cocoa naming convention for returning 'owned' objects
出现这样的情况,主要是属性名中包括 keyword. You can solve this by: Renaming that property: @property (strong, nonato ...
- [Swust OJ 1091]--土豪我们做朋友吧(并查集,最值维护)
题目链接:http://acm.swust.edu.cn/problem/1091/ Time limit(ms): 1000 Memory limit(kb): 32768 人都有缺钱的时候,缺 ...
- 转百度前辈的Trados使用心得
我用Trados的时间不长,可以说是一个新手.但我在较短的时间内就已经初步掌握这个工具,说明它并不是那么神秘,并不是那么深不可测.这里,我说一说学习它的一点体会.在我转发的文章中有的内容,我就少讲一些 ...
- 错误处理try catch
<?phpfunction inverse($x) { if (!$x) { throw new Exception('被除数不能为0'); } if ($x>31) { throw ne ...
- 统计图表类库--libchart使用简介
1.饼图 #载入类文件 include "../libchart/classes/libchart.php"; header("Content-type: image/p ...
- ThinkPHP验证码类
//ThinkPHP验证码类使用$config = array( 'fontSize' => 30, // 验证码字体大小 'length' => 3, // 验证码位数 'useNois ...