您的位置:首页 > 编程语言 > Java开发

java 操作属性文件

2016-06-12 00:00 134 查看
摘要: java 操作属性文件

package com.demo.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Properties;
import java.util.Map.Entry;

import javax.servlet.ServletException;

public class Test {
public static void main(String[] args) {
systenGet();
filestreamGet();
filestreamGetSrc();
classloaderGet();
classLoadUTFGet();
init();
setProParams();
}
/**
* system读取propertise文件
/
public static void systenGet(){
Properties properties=System.getProperties();
System.out.println(properties.getProperty("user.dir"));
System.out.println("全局系统参数key_______:"+System.getProperty("000000"));
}
/*
* 属相文件信息
/
public static void filestreamGet() {
Properties p = new Properties();
try {
FileInputStream fileInputStream = new FileInputStream("test.properties");
p.load(fileInputStream);
System.out.println("工程下___:" + p.getProperty("000000"));
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void filestreamGetSrc() {
Properties p = new Properties();
try {
FileInputStream fileInputStream = new FileInputStream("test.properties");
p.load(fileInputStream);
System.out.println("工程src下___:"+p.getProperty("000000"));
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* classloader读取属性文件
/
public static void classloaderGet(){
ClassLoader load=ClassLoader.getSystemClassLoader();
Properties p=new Properties();
try {
p.load(load.getResourceAsStream("code.properties"));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(p.getProperty("000000"));
}
/*
* 读取properties 文件乱码解决方法
/
public static void classLoadUTFGet(){
ClassLoader load=ClassLoader.getSystemClassLoader();
Properties p=new Properties();
try {
p.load(new InputStreamReader(load.getResourceAsStream("code.properties"), "UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("解决中文乱码的方法___:"+p.getProperty("000000"));
}
public static void init(){
try {
Properties prop = new Properties();
FileInputStream fis = null;
fis = new FileInputStream("src/config.properties");
prop.load(fis);
Iterator<Entry<Object, Object>> itr = prop.entrySet().iterator();
while (itr.hasNext()) {
Entry<Object, Object> e = itr.next();
System.setProperty(e.getKey().toString(), e.getValue().toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 项目启动时 加载属性文件数据到系统内存中
*/
public static void setProParams(){
try {
ClassLoader load=ClassLoader.getSystemClassLoader();
Properties properties=new Properties();
properties.load(new InputStreamReader(load.getResourceAsStream("config.properties"), "UTF-8"));
Iterator<Entry<Object, Object>>iterator=properties.entrySet().iterator();
while(iterator.hasNext()){
Entry<Object, Object> e = iterator.next();
System.out.println("建本地属性文件写到内存中___:{"+e.getKey().toString()+"="+e.getValue().toString()+"}");
System.setProperty(e.getKey().toString(), e.getValue().toString());
}
System.out.println(System.getProperty("username"));
} catch (Exception e) {
e.printStackTrace();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 操作属性文件