Properties文件及与之相关的System.getProperties操作(转)

简介: 如何使用Java读写系统属性? 读: 简述properties文件的结构和基本用法结构:扩展名为properties的文件,内容为key、value的映射,例如"a=2"  示例用到的properties文件: test.
如何使用Java读写系统属性?
读:

简述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

 

 

 

 

 

相关文章
ResourceBundle.getBundle()来读取自定义的properties配置文件
ResourceBundle.getBundle()来读取自定义的properties配置文件
252 1
log4j.properties日志配置文件内容
log4j.properties日志配置文件内容
94 0
【Spring配置相关】启动类为Current File,如何更改
问题场景:当我们切换类的界面的时候,重新启动的按钮是灰色的,不能使用,并且只有一个Current File 项目,下面介绍两种方法来解决这个问题。
|
7月前
文件名: ?Ciwindows\system32 inetsrconfiglapplicationHost.config 错误:无法写入配置文件
文件名: ?Ciwindows\system32 inetsrconfiglapplicationHost.config 错误:无法写入配置文件
76 0
System.setProperty配置系统属性详解
System.setProperty配置系统属性详解
properties配置文件的读取
properties配置文件的读取
108 0
log4j.properties配置文件详解
log4j.properties配置文件详解
704 0
Java获取当前项目下的文件或目录物理地址System.getProperty(“user.dir“)
Java获取当前项目下的文件或目录物理地址System.getProperty(“user.dir“)
Properties文件操作
Properties文件操作
90 0
SpringCloud项目启动失败 not found config file[log.properties]
SpringCloud项目启动失败 not found config file[log.properties]
253 0
SpringCloud项目启动失败 not found config file[log.properties]
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等