Java对properties配置文件的操作
package com.yorsun;
import java.io.File;
import java.io.FileInputStream;
import
java.io.FileNotFoundException;
import java.io.FileOutputStream;
import
java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletContext;
import
javax.servlet.http.HttpServlet;
public class PropertiesUnit {
private String filename;
private Properties p;
private FileInputStream in;
private FileOutputStream out;
public PropertiesUnit(String filename)
{
this.filename =
filename;
File file = new
File(filename);
try
{
in = new
FileInputStream(file);
p = new
Properties();
p.load(in);
in.close();
}
catch (FileNotFoundException e) {
// TODO
Auto-generated catch
block
System.err.println("配置文件config.properties找不到!");
e.printStackTrace();
}
catch (IOException e) {
// TODO
Auto-generated catch
block
System.err.println("读取配置文件config.properties错误!");
e.printStackTrace();
}
}
public static String getConfigFile(HttpServlet hs)
{
return getConfigFile(hs,
"config.properties");
}
private static String getConfigFile(HttpServlet hs, String
configFileName) {
String configFile =
"";
ServletContext sc =
hs.getServletContext();
configFile = sc.getRealPath("/"
+ configFileName);
if (configFile == null ||
configFile.equals("")) {
configFile = "/" +
configFileName;
}
// TODO
Auto-generated method stub
return configFile;
}
public void list() {
p.list(System.out);
}
public String getValue(String itemName) {
return
p.getProperty(itemName);
}
public String getValue(String itemName, String defaultValue)
{
return p.getProperty(itemName,
defaultValue);
}
public void setValue(String itemName, String value)
{
p.setProperty(itemName, value);
}
public void saveFile(String filename, String description) throws Exception
{
try {
File f =
new File(filename);
out = new
FileOutputStream(f);
p.store(out,
description);
out.close();
}
catch (IOException ex) {
throw new
Exception("无法保存指定的配置文件:" +
filename);
}
}
public void saveFile(String
filename) throws
Exception{
saveFile(filename,"");
}
public
void saveFile() throws
Exception{
if(filename.length()==0)
throw
new
Exception("需指定保存的配置文件名");
saveFile(filename);
}
public
void deleteValue(String
value){
p.remove(value);
}
public static void
main(String args[]){
String
file="/eclipse/workspace/NewsTest/WEB-INF/config.properties";
//
String
file="D:\\eclipse\\workspace\\NewsTest\\WEB-INF\\config.properties";
PropertiesUnit
pu=new PropertiesUnit(file);
pu.list();
}
}
--------------------------------------------------------------------------------------------
package com.test.TestClass;
import java.io.BufferedInputStream;
import java.io.File;
import
java.io.FileInputStream;
import java.io.FileNotFoundException;
import
java.io.IOException;
import java.io.InputStream;
import
java.util.Properties;
public class ReadPropertiesFile ...{
public void readPropertiesFile(String
fileName) throws FileNotFoundException
...{
String
str =
"";
Properties
prop = new
Properties();
InputStream
stream =
null;
//读取这个类在同一包中的properties文件
//stream
=
getClass().getClassLoader().getResourceAsStream(fileName);
System.out.println("path:"
+
getClass().getResource(fileName));
//读取SRC下的的properties文件
String
path =
getClass().getResource("/").getPath();
stream
= new BufferedInputStream(new FileInputStream(new File(path+fileName)));
try
...{
prop.load(stream);
str
=
prop.getProperty("localname");
System.out.println("localname:"
+
str);
System.out.println("properties:"
+
prop);
stream.close();
}
catch (IOException e)
...{
//
TODO Auto-generated catch
block
e.printStackTrace();
}
}
public
static void main(String[] args) throws FileNotFoundException
...{
//
TODO Auto-generated method
stub
new
ReadPropertiesFile().readPropertiesFile("config.properties");
}
}
--------------------------------------------------------------
//=================sprin配置文件================================
<bean
id="userService"
class="com.thtf.ezone.ezesb.jmx.admin.service.impl.UserServiceImpl">
<property
name="filePath"
value="config/userInfo.properties"
/>
</bean>
//=================java文件================================
package com.thtf.ezone.ezesb.jmx.admin.service.impl;
import java.io.FileInputStream;
import java.io.IOException;
import
java.util.Properties;
public class UserServiceImpl implements UserService {
String filePath
= null;
public void setFilePath(String filePath)
{
this.filePath = filePath;
}
public static void main(String dd[])throws Exception{
Properties p =
new Properties();
FileInputStream ferr=new
FileInputStream((getClass().getClassLoader()
.getResource("")
+ filePath).toString().substring(6));//
用subString(6)去掉:file:/try{
p.load(ferr);
ferr.close();
Set s =
p.keySet();
Iterator it = s.iterator();
while(it.hasNext()){
String id
= (String)it.next();
String value =
p.getProperty(id);
System.out.println(id+":="+value);
}
}catch(Exception
e){
e.printStackTrace();
}
}
}
//==============databaseconfing.properties
文件=====================
#----------------------------------
# sql server
2000数据厍连接信息
#----------------------------------
dp.sd.DataBaseTyp=mssql
DataBaseHost=127.0.0.1:1433
DataBase=formpro
UserName=sa
PassWord=01
#----------------------------------
#
mysql
数据厍连接信息
#----------------------------------
#DataBaseHost=127.0.0.1:3306
#DataBaseTyp=mysql
#DataBase=snow
#UserName=root
#PassWord=01
//==========================运行结果=======================
PassWord:=01
DataBaseHost:=127.0.0.1:1433
DataBase:=formpro
dp.sd.DataBaseTyp:=mssql
UserName:=sa