package cn.com.mfsoft.config;
import java.io.*;
import java.net.URL;
import java.net.URLDecoder;
import java.util.*;
import org.apache.log4j.Logger;
import cn.com.mfsoft.config.ConfigurationException;
public class Configuration
{
private Properties config=new Properties();//记录配置项
private String fn=null;//记录配置文件名
//此构造方法用于新建配置文件
public Configuration()
{
String path="";
try
{
//File file = new File("system.conf");
URL u = this.getClass().getResource(this.getClass().getSimpleName()+".conf");
path = u.getPath();
//获得用户工程目录System.getProperty("user.dir")
//path=(System.getProperty("user.dir").replace("\\", "/"))+"/model/Model_"+model_id+".snet";
// path=URLDecoder.decode(path, "utf-8");
System.out.println("11"+path);
path=URLDecoder.decode(path, "utf-8");
System.out.println("22"+path);
FileInputStream fin = new FileInputStream(path);
config.load(fin); //载入文件
fin.close();
}
catch (IOException ex)
{
try
{
throw new ConfigurationException
("无法读取指定的配置文件:"+path);
} catch (ConfigurationException e)
{
e.printStackTrace();
}
}
fn=path;
}
public String getPrjRoot()
{
URL u = this.getClass().getResource(this.getClass().getSimpleName()+".conf");
String str = u.getPath();
if (str.indexOf(":") != -1)
str = str.substring(1);// 在windows下面为/F:/MyPrj/WORK/post
else
str = str.substring(0);// 在Linux下面为/usr/local/apache...
return str;
}
//从指定文件名读入配置信息
public Configuration(String fileName)throws ConfigurationException
{
try
{
FileInputStream fin = new FileInputStream(fileName);
config.load(fin); //载入文件
fin.close();
}
catch (IOException ex)
{
throw new ConfigurationException
("无法读取指定的配置文件:"+fileName);
}
fn=fileName;
}
//指定配置项名称,返回配置值
public String getValue(String itemName)
{
String value=getValue("language","chinese");
String st="";
if(value.equals("chinese"))
{
st=toGb(config.getProperty(itemName));
}
else
{
st=config.getProperty(itemName);
}
return st;//config.getProperty(itemName);
}
public String getValue2(String ItemName)
{
String value="";
try
{
String path="";
path=getClass().getResource("/system.conf").getPath().substring(1);
path=URLDecoder.decode(path, "utf-8");
System.out.println();
Configuration config= new Configuration(path);
value=config.getValue(ItemName);
} catch (Exception e)
{
e.printStackTrace();
}
return value;
}
//指定配置项名称和默认值,返回配置值
public String getValue(String itemName,
String defaultValue)
{
return config.getProperty(itemName,defaultValue);
}
//设置配置项名称及其值
public void setValue(String itemName,String value){
config.setProperty(itemName,value);
return;
}
//保存配置文件,指定文件名和抬头描述
public void saveFile(String fileName,String description)throws ConfigurationException
{
try {
FileOutputStream fout
= new FileOutputStream(fileName);
config.store(fout, description);//保存文件
fout.close();
}
catch (IOException ex) {
throw new ConfigurationException
("无法保存指定的配置文件:"+fileName);
}
}
//保存配置文件,指定文件名
public void saveFile(String fileName)throws ConfigurationException
{
saveFile(fileName,"");
}
//保存配置文件,采用原文件名
public void saveFile() throws ConfigurationException {
if(fn.length()==0)
throw new ConfigurationException
("需指定保存的配置文件名");
saveFile(fn);
}
public static String toGb(String uniStr)
{
String gbStr = "";
if(uniStr == null)
{
uniStr = "";
}
try
{
byte[] tempByte = uniStr.getBytes("ISO8859_1");
gbStr = new String(tempByte,"GB2312");
}
catch(Exception ex)
{
}
return gbStr;
}
public static void main(String[]args)
{
}
}
ConfigurationException.java
/**
*
*<p>Blog:http://www.cnweblog.com/nm1504</p>
*<p>E-mail:yyk1504@163.com</p>
*<p>创建时间:2008-3-13-下午03:12:29</p>
*<p>Copyright: (c)2008-3-13</p>
*/
package cn.com.mfsoft.config;
public class ConfigurationException extends Exception
{
public ConfigurationException()
{
}
public ConfigurationException(String msg)
{
super(msg);
}
}
ReadConfig.java
/**
**<p>Blog:http://www.cnweblog.com/nm1504</p>
*<p>E-mail:yyk1504@163.com</p>
*<p>创建时间:2008-3-13-下午03:14:24</p>
*<p>Copyright: (c)2008-3-13</p>
*/
package cn.com.mfsoft.config;
import java.net.URL;
public class ReadConfig
{
public static void main(String[] args)
{
try
{
Configuration config=new Configuration();
//读取指定文件
ReadConfig rc=new ReadConfig();
System.out.println("::::::;"+rc.getPrjRoot());
try{
/*
System.out.println(new String(config.getValue("Max_Users_Count")));//5
System.out.println(new String(config.getValue("Max_Users_Count").getBytes(),"GB2312"));//6
System.out.println(new String(config.getValue("Max_Users_Count").getBytes(),"ISO8859_1"));//7
System.out.println(new String(config.getValue("Max_Users_Count").getBytes("GB2312")));//8
System.out.println(new String(config.getValue("Max_Users_Count").getBytes("GB2312"),"GB2312"));//9
System.out.println(new String(config.getValue("Max_Users_Count").getBytes("GB2312"),"ISO8859_1"));//10
System.out.println(new String(config.getValue("Max_Users_Count").getBytes("UTF-8")));//11
System.out.println(new String(config.getValue("Max_Users_Count").getBytes("UTF-8"),"GB2312"));//12
System.out.println(new String(config.getValue("Max_Users_Count").getBytes("ISO8859_1"),"UTF-8"));//13
System.out.println(toGb(config.getValue("Max_Users_Count")));*/
}
catch(Exception e)
{
e.printStackTrace();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public String getPrjRoot()
{
URL u = this.getClass().getResource(this.getClass().getSimpleName()+".conf");
String str = u.getPath();
if (str.indexOf(":") != -1)
str = str.substring(1);// 在windows下面为/F:/MyPrj/WORK/post
else
str = str.substring(0);// 在Linux下面为/usr/local/apache...
return str;
}
}
SetConfig.java
/**
*
*<p>Blog:http://www.cnweblog.com/nm1504</p>
*<p>E-mail:yyk1504@163.com</p>
*<p>创建时间:2008-3-13-下午03:13:20</p>
*<p>Copyright: (c)2008-3-13</p>
*/
package cn.com.mfsoft.config;
import cn.com.mfsoft.config.ConfigurationException;//包含这个包方能使用配置类
public class SetConfig {
public static void main(String[] args) {
try
{
Configuration config = new Configuration();
//设置一些属性值
config.setValue("language", "chinese");
config.setValue("Max_Users_Count", "50");
config.setValue("Max_OpenedFile_Count", "38");
//保存文件
config.saveFile("d:/system.conf",
"Sytem Global Configuration");
}
catch (ConfigurationException ex) {
//捕获我们自定义的异常
ex.printStackTrace();
}
}
}
分享到:
相关推荐
通常我们的做法是用配置文件来解决。 各种语言都有自己所支持的配置文件类型。比如 python ,他支持 .ini 文件。因为他内部有一个 ConfigParser 类来支持 .ini 文件的读写,根据该类提供的方法程序员可以 自由的...
java项目读取配置文件的实例代码,使用class.getClassLoader().getResourceAsStream方法。
在java类里直接读取配置文件中的信息,用到的方法是this.getClass().getClassLoader().getResourceAsStream
配置文件读取工具类,利用Java语言编写,可以用来读取数据库配置等配置文件,压缩包中包含工具类的使用方法.
这个工程是使用 java 来使用反射的原理读取 *.proproies 资源文件中的一些配置信息。利用此方法可以在没有使用框架的情况下可以让你的程序更加灵活易于修改。如:可以将一些数据库配置信息,重要信息写入到资源文件...
主要介绍了java简单读取properties配置文件的方法,涉及java针对properties配置的载入及文件属性读取相关操作技巧,需要的朋友可以参考下
NULL 博文链接:https://java--hhf.iteye.com/blog/2171034
JNDI配置文件 使用方法 已经配置好了, 直接更换tomcat里面相应的文件
Maven项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理...由于 Maven 的面向项目的方法,许多 Apache Jakarta 项目发文时使用 Maven,而且公司项目采用 Maven 的比例在持续增长。
php+java的配置方法总结!经常使用的不妨看看,对你的配置很有作用啊……
工具类里分读取.yml工具类和.properties工具类,结合博客描述使用,用不到的方法可根据个人情况删除,
使用Java中的Properties类的方法,以及类加载器来读取配置文件中的信息。可以读取.properties文件和.cfg文件。详细代码+注释+测试例子
运行加密后的EJB组件的方式与运行WEB应用程序类似,找到您的EJB容器的启动配置文件,加入-agentlib:的存放目录>\JLoader 即可。 4.Linux 下如何使用: 请将 libJLoader.so 复制到系统的 /usr/lib 目录下,然后...
主要给大家介绍了关于Java中使用Properties配置文件的简单方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
1. 简介 软件功能为:将 Jar 文件转换为 Exe 可执行文件。... 如果运行时还需要其他的 jar 文件,那么可以在环境变量 CLASSPATH 中包含,也可以在配置文件中添加配置: classpath d:\lib1.jar;e:\lib2.jar
。。。
filter过滤器在java的用法,配置文件怎样写,一看就明白
这样,我们就可以在log4j加载配置文件之前,先用System.setProperty ("WORKDIR", WORKDIR);设置好根路径,此操作可通过一初始的servlet进行。 方法二、 可以使用服务器环境变量 log4j的配置文件支持服务器的vm的...
使用反射机制修改加载到内存的Hibernate配置文件,使用该方法可以将数据库的配置信息提取到别的路径下。