配置文件*.properties/*.ini类型文件管理
来源:互联网 发布:钉钉软件使用说明 编辑:程序博客网 时间:2024/06/06 09:40
package com.sf.oracle.lesson03;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
static FileInputStream fileInputStream = null;
static FileOutputStream fileOutputStream = null;
static Properties properties = null;
static {
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
fileInputStream = new FileInputStream(file);
properties = new Properties();
properties.load(fileInputStream);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 通过Key去获取value
*
* @param key
* @param defaultValue
* @return
*/
return value;
}
/**
* 保存配置
*
* @param key
* @param value
*/
public static void setProperty(String key, String value) {
// key如果原来没有,新建
// 如果原来有,修改其对应的值
properties.setProperty(key, value);
try {
fileOutputStream = new FileOutputStream(file);
properties.store(fileOutputStream, "QQ config info");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
String url = TestProperies.getProperty("url",
"jdbc:oracle:thin:@127.0.0.1:1521:orcl");
String user = TestProperies.getProperty("user", "oracle1103");
String password = TestProperies.getProperty("password", "888888");
String driver = TestProperies.getProperty("driver",
"oracle.jdbc.driver.OracleDriver");
String port = TestProperies.getProperty("port", "8099");
System.out.println("user:" + user);
System.out.println("password:" + password);
TestProperies.setProperty("url",
"jdbc:oracle:thin:@127.0.0.1:1521:orcl");
TestProperies.setProperty("user", "1111111111111111");
TestProperies.setProperty("password", "2222222222222");
user = TestProperies.getProperty("user", "oracle1103");
password = TestProperies.getProperty("password", "888888");
System.out.println("user:" + user);
System.out.println("password:" + password);
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class TestProperies {
//properties与ini文件存储方式[key=value]、操作方法基本上没差,唯一的不同在于:当文件中Key值出现重复时,ini处理面的key,properties处理最后一个key
static FileInputStream fileInputStream = null;
static FileOutputStream fileOutputStream = null;
static Properties properties = null;
static {
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
fileInputStream = new FileInputStream(file);
properties = new Properties();
properties.load(fileInputStream);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 通过Key去获取value
*
* @param key
* @param defaultValue
* @return
*/
public static String getProperty(String key, String defaultValue) {
//通过key获取value,如果key不存在返回默认值defaultValue
return value;
}
/**
* 保存配置
*
* @param key
* @param value
*/
public static void setProperty(String key, String value) {
// key如果原来没有,新建
// 如果原来有,修改其对应的值
properties.setProperty(key, value);
try {
fileOutputStream = new FileOutputStream(file);
properties.store(fileOutputStream, "QQ config info");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
String url = TestProperies.getProperty("url",
"jdbc:oracle:thin:@127.0.0.1:1521:orcl");
String user = TestProperies.getProperty("user", "oracle1103");
String password = TestProperies.getProperty("password", "888888");
String driver = TestProperies.getProperty("driver",
"oracle.jdbc.driver.OracleDriver");
String port = TestProperies.getProperty("port", "8099");
System.out.println("user:" + user);
System.out.println("password:" + password);
TestProperies.setProperty("url",
"jdbc:oracle:thin:@127.0.0.1:1521:orcl");
TestProperies.setProperty("user", "1111111111111111");
TestProperies.setProperty("password", "2222222222222");
user = TestProperies.getProperty("user", "oracle1103");
password = TestProperies.getProperty("password", "888888");
System.out.println("user:" + user);
System.out.println("password:" + password);
}
}
- 配置文件*.properties/*.ini类型文件管理
- 读取配置文件Ini文件
- 使用java读取配置文件(ini、properties)
- 读写ini 和 properties文件
- TIniFile类型以及配置文件ini
- 读取配置文件(properties文件)
- 配置文件中用到ini文件
- python配置文件(ini文件)
- 秘技1:创建一个类,专门负责保管从配置文件properties、ini等文件中获取属性对应的值
- 使用IniEditor读写INI类型配置文件
- 使用IniEditor读写INI类型配置文件
- 使用IniEditor读写INI类型配置文件
- 使用IniEditor读写INI类型配置文件
- Spring配置文件加载properties文件
- spring配置文件读取properties文件
- xml配置文件引入properties文件
- spring注入properties类型的配置文件信息
- Delphi中的INI文件管理
- LDAP诠释
- win7系统中HANDLE ProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, id)失败
- uCOS-II函数解析
- 系统时钟(汇编语言程序设计)
- 一年半经验的程序员年终总结
- 配置文件*.properties/*.ini类型文件管理
- 大道至简,职场上做人做事做管理
- 如何进入到嵌入式系统开发行业
- 读写ini文件 java
- Android.mk编译APK范例
- The 2nd GIS-focused algorithm competition, GISCUP 2013
- 一些电商英文缩写的解释
- php 操作 MySQL 中的Blob类型 存储图片
- 直面W5100发热问题