在Model里面如何读取c 读取ini配置文件件常量

没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!在 SegmentFault,学习技能、解决问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。
问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
就是我想定义点全局常量,
比如某个文件的单机路径,api 借口等,以后我在控制器,模型 类文件里直接调用这个常量就行了..
请问我该把常量写在哪个文件里呢?
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
在Controller被ThinkPHP调用之前就行,可以写一个const.php,专门定义一些常量,到入口文件引入。
#我喜欢用绝对路径,有时候相对路径嵌套require可能会引起一些错误。
require APP_PATH.'../config/const.php';
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
好像入口文件中定义就行
同步到新浪微博
分享到微博?
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要该,理由是:
在 SegmentFault,学习技能、解决问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。另一种JFinal读取配置文件的高效方案
本文是基于JFinal编写的一种读取配置文件的更加高效的方案。如果您没有使用JFInal,没必要阅读此篇文章,以免浪费您的时间。&使用过JFinal开发项目的朋友都知道,读取properties配置文件其实非常简单,JFinal已经提供了一个PropKit的工具类,使我们非常方便的读取到配置文件中的某个值。在代码初始化的时候,首先初始化下Propkit,代码如下:PropKit.use("jboot.properties");然后在整个项目中,随便使用PropKit的get方法即可读取jboot.properties文件的任何内容,例如:String&wechatAppId&=&PropKit.get("wechat_app_id");非常简洁,读取某个配置文件也就一行代码而已。&但是,但是...&在我们实际开发中,某个组件的配置,往往是由多个配置信息组成的,比如微信开发的相关配置,需要的信息可能如下:wechat.debug&=&falsewechat.appid&=&xxx
wechat.appsecret&=&xxx
wechat.token&=&xxx
wechat.partner&=&xxx
wechat.paternerKey&=&xxx
wechat.cert&=&xxx
wechat.templateId&=&xxx那么,如果我们在需要读取这些信息的时候,比如在拦截器里,我们可能需要编写如下的代码:String&wechatAppId&=&PropKit.get("wechat.id");
String&wechatAppSecret=&PropKit.get("wechat.appsecret");
String&wechatToken&=&PropKit.get("wechat.token");
String&wechatPartner&=&PropKit.get("wechat.partner");
String&wechatPartnerKey&=&PropKit.get("wechat.partnerKey");
String&wechatCert&=&PropKit.get("wechat.cert");
String&wechatTemplateId&=&PropKit.get("wechat.templateId");如果再多个地方有这样的需求,那么通常有几个方法:1、复制这部分代码,到需要的地方。2、把这部分的代码提取到一个独立的类,里面有getAppId(),getAppSercret()等方法,然后调用。3、编写一个model类,然后在这个类进行实例化的时候,填充属性。&在项目中除了微信配置以外,还有非常多的各种配置,比如数据库配置信息、redis配置信息,缓存配置信息,rpc配置信息等等等等。无论您使用3中方法中的任何一种,都不是高效简洁的。&那么,是否可以有更好的办法呢?当然有。方法如下:给每中配置编写对于的一个model,然后通过一个配置文件直接读取到这个model。例如:我们为微信的配置编写了一个WechatConfig类,代码如下:public&class&WechatConfig{&&&
private&String&wechatAppId;&&&
&&&&private&String&wechatAppS&&&
&&&&private&String&wechatT&&&
&&&&private&String&&&&
&&&&private&String&partnerK&&&
&&&&private&String&&&&
&&&&private&String&templateId;&&&
&&&&//&getter&setter&略
}想获取这个配置信息,只需要通过一个配置文件即可。WechatConfig&config&&=&MyPropUtils.getConfig(WechatConfig.class);重点来了,如果编写MyPropUtils这个工具类。先说思路:1、读取到properties的所有配置信息。2、反射读取到WechatConfig的字段。3、把读取到的properties内容赋值到对应的字段去。&看起来也非常简单,直接编写代码。public&static&&T&&T&getConfig(String&propFile,&Class&T&&clazz)&{&&&
&&&&&&&&//不管三七二十一,先初始化这个类。
&&&&&&&&Object&obj&=&ClassNewer.newInstance(clazz);&&&&&&&&
&&&&&&&&//读取到Prop信息
&&&&&&&&Prop&prop&=&PropKit.use(propFile);&&&
&&&&&&&&&&&&
&&&&&&&&//找到这个类的所有set方法,model类默认有get和set的
&&&&&&&&List&Method&&setMethods&=&new&ArrayList&&();
&&&&&&&&Method[]&methods&=&obj.getClass().getMethods();&&&&&&&&
&&&&&&&&if&(ArrayUtils.isNotEmpty(methods))&{&&&&&&&&&&&&
&&&&&&&&&&&&for&(Method&m&:&methods)&{&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&if&(m.getName().startsWith("set")&
&&&&&&&&&&&&&&&&&&&m.getName().length()&&&3&
&&&&&&&&&&&&&&&&&&&m.getParameterCount()&==&1)&{
&&&&&&&&&&&&&&&&&&&&setMethods.add(m);
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&}
&&&&&&&&}&&&&&&&&
&&&&&&&&//遍历所有set方法
&&&&&&&&for&(Method&method&:&setMethods)&{&&&&&&&&&&&&
&&&&&&&&try&{&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&//&得到这个set方法对应的key以及value值
&&&&&&&&&&&&&&&&String&key&=&StrKit.firstCharToLowerCase(method.getName().substring(3));&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&String&&&value&=&prop.get(key);&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&if&(StringUtils.isNotBlank(value))&{&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&//如果这个value值不为空,把value值转化为该set方法需要的数据类型,
&&&&&&&&&&&&&&&&&&&&//可能是boolean,int,datetime等类型
&&&&&&&&&&&&&&&&&&&&Object&val&=&convert(method.getParameterTypes()[0],&value);&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&//执行这个方法并进行赋值
&&&&&&&&&&&&&&&&&&&&method.invoke(obj,&val);
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&}&catch&(Throwable&ex)&{
&&&&&&&&&&&&&&&&log.error(ex.toString(),&ex);
&&&&&&&&&&&&}
&&&&&&&&}&&&&&&&&
&&&&&&&&//把这个object返回
&&&&&&&&return&(T)&
&&&&}&嗯,看起来还不错,但是还是有些问题,这个有些局限性,很多时候在一个配置文件里面可能需要有多个名称一样的配置,比如redis的可能有一个叫password的配置,数据库可能也需要一个叫password的配置,因此,在配置文件里面我们通常会给某个组件添加上前缀,比如redis的相关配置是 redis.password,redis.host这样的,而数据库可能是 mysql.password,mysql.user 等等这样的配置,因此我们需要给这个工具类添加下 可以读取前缀的功能。还有另一个缺点是,每次调用这个getConfig方法,都是重新实例化一个新的实例,配置信息在整个项目中都很少发生变化,为了更加高效,我们可以把这个getConfig读取到的model都是单利的,而不是每次都是一个新的实例。&因此,我们可以优化如下:&&&private&static&ConcurrentHashMap&String,&Object&&configs&=&new&ConcurrentHashMap&&();&&&&
&&&public&static&&T&&T&getConfig(String&prefix,&String&propFile,&Class&T&&clazz)&{&&&&&&&&
&&&&&&&&//检查前缀
&&&&&&&if&(StringUtils.isBlank(prefix))&{&&&&&&&&&&&&
&&&&&&&&&&&throw&new&JbootException("prefix&must&not&be&null");
&&&&&&&&}&&&&&&&&
&&&&&&&&//先尝试从map中获取内容
&&&&&&&&Object&obj&=&configs.get(prefix);&&&&&&&&
&&&&&&&&if&(obj&!=&null)&{&&&&&&&&&&&&
&&&&&&&&&&&&return&(T)&
&&&&&&&&obj&=&ClassNewer.newInstance(clazz);
&&&&&&&&Prop&prop&=&&PropKit.use(propFile);
&&&&&&&&&&&&
&&&&&&&&List&Method&&setMethods&=&new&ArrayList&&();
&&&&&&&&Method[]&methods&=&obj.getClass().getMethods();&&&&&&&&
&&&&&&&&if&(ArrayUtils.isNotEmpty(methods))&{&&&&&&&&&&&&
&&&&&&&&&&&&for&(Method&m&:&methods)&{&&&&&&&&&&&&&&&&
&&&&&&&&&&&&if&(m.getName().startsWith("set")&
&&&&&&&&&&&&&&&&&&&&&&&m.getName().length()&&&3&
&&&&&&&&&&&&&&&&&&&&&&&m.getParameterCount()&==&1)&{
&&&&&&&&&&&&&&&&&&&&setMethods.add(m);
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&}
&&&&&&&&}&&&&&&&&
&&&&&&&&for&(Method&method&:&setMethods)&{&&&&&&&&&&&&
&&&&&&&&try&{&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&String&key&=&StrKit.firstCharToLowerCase(method.getName().substring(3));&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&//添加上前缀的key
&&&&&&&&&&&&&&&&key&=&prefix.trim()&+&"."&+&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&String&value&=&prop.get(key);&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&if&(StringUtils.isNotBlank(value))&{&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&Object&val&=&convert(method.getParameterTypes()[0],&value);
&&&&&&&&&&&&&&&&&&&&method.invoke(obj,&val);
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&}&catch&(Throwable&ex)&{
&&&&&&&&&&&&&&&&log.error(ex.toString(),&ex);
&&&&&&&&&&&&}
&&&&&&&&}&&&&&&&&
&&&&&&&&//设置到map里去
&&&&&&&&configs.put(prefix,&obj);&&&&&&&&
&&&&&&&&return&(T)&
&&&&}&代码写到这里,已经很不错了,但是还有几个小问题:1、调用这个方法需要穿3个参数,太麻烦了。2、通常在我们整个项目中,一般就只有几个配置文件,不会太多,所以没有必要每次都传配置文件的名字。&我们继续优化下,处理办法是添加几个重载的方法。&&&&private&static&ConcurrentHashMap&String,&Object&&configs&=&new&ConcurrentHashMap&&();&&&&
&&&&private&static&final&String&defalutConfigFile&=&"jboot.properties";&
&&&&public&static&&T&&T&get(Class&T&&clazz)&{
&&&&&&&&PropertieConfig&propertieConfig&=&clazz.getAnnotation(PropertieConfig.class);&&&&&&&&
&&&&&&&&if&(propertieConfig&==&null)&{&&&&&&&&&&&&
&&&&&&&&throw&new&JbootException("PropertieConfig&annotation&must&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&not&be&null&in&class&:&"&+&clazz);
&&&&&&&&}&&&&&&&&
&&&&&&&&return&getConfig(propertieConfig.prefix(),&propertieConfig.file(),&clazz);
&&&&public&static&&T&&T&getConfig(String&prefix,&Class&T&&clazz)&{&&&&&&
&&&&&&&&return&getConfig(prefix,defalutConfigFile,clazz);
&&&&public&static&&T&&T&getConfig(String&prefix,&String&propFile,&Class&T&&clazz)&{&&&&&&&&
&&&&&&&&//检查前缀
&&&&&&&if&(StringUtils.isBlank(prefix))&{&&&&&&&&&&&&
&&&&&&&&&&&throw&new&JbootException("prefix&must&not&be&null");
&&&&&&&&}&&&&&&&&
&&&&&&&&//先尝试从map中获取内容
&&&&&&&&Object&obj&=&configs.get(prefix);&&&&&&&&
&&&&&&&&if&(obj&!=&null)&{&&&&&&&&&&&&
&&&&&&&&&&&&return&(T)&
&&&&&&&&obj&=&ClassNewer.newInstance(clazz);
&&&&&&&&Prop&prop&=&&PropKit.use(propFile);&&&&&&&&&&&&
&&&&&&&&List&Method&&setMethods&=&new&ArrayList&&();
&&&&&&&&Method[]&methods&=&obj.getClass().getMethods();&&&&&&&&
&&&&&&&&if&(ArrayUtils.isNotEmpty(methods))&{&&&&&&&&&&&&
&&&&&&&&&&&&for&(Method&m&:&methods)&{&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&if&(m.getName().startsWith("set")&
&&&&&&&&&&&&&&&&&&&&&&&m.getName().length()&&&3&
&&&&&&&&&&&&&&&&&&&&&&&m.getParameterCount()&==&1)&{
&&&&&&&&&&&&&&&&&&&&setMethods.add(m);
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&}
&&&&&&&&}&&&&&&&&
&&&&&&&&for&(Method&method&:&setMethods)&{&&&&&&&&&&&&
&&&&&&&&try&{
&&&&&&&&&&&&&&&&String&key&=&StrKit.firstCharToLowerCase(method.getName().substring(3));&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&//添加上前缀的key
&&&&&&&&&&&&&&&&key&=&prefix.trim()&+&"."&+&
&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&String&value&=&prop.get(key);&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&if&(StringUtils.isNotBlank(value))&{
&&&&&&&&&&&&&&&&&&&&Object&val&=&convert(method.getParameterTypes()[0],&value);
&&&&&&&&&&&&&&&&&&&&method.invoke(obj,&val);
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&}&catch&(Throwable&ex)&{
&&&&&&&&&&&&&&&&log.error(ex.toString(),&ex);
&&&&&&&&&&&&}
&&&&&&&&}&&&&&&&&
&&&&&&&&//设置到map里去
&&&&&&&&configs.put(prefix,&obj);&&&&&&&&
&&&&&&&&return&(T)&
&&&&}&OK,代码编写完了,如何使用呢?配置内容如下:jboot.myconfig.name=aaa
jboot.myconfig.passowrd=bbb
jboot.myconfig.age=10编写model类:@PropertieConfig(prefix="jboot.myconfig")
public&class&MyConfigModel{&&&&
&&&&private&String&&&&&
&&&&private&String&&&&&
&&&&private&int&&&&&
&&&&//getter&setter&略
}读取 model 内容:MyConfigModel&config&=&MyPropUtils.getConfig(MyConfigModel.class);&怎么样?是不是跟简单,有更多时间陪女朋友了?&具体代码在:&,请执行参考。&我的个人微信公众号,经常分享一些技术心得和心里路程,希望也能认识你。
这个功能相当实用,超赞
很受启发,感谢分享!
牛x啊,很有用
将配置分文件存储,不就可以避免这些问题了吗?
虽然还没看懂,但是先收藏一波详解C#如何读写config配置文件
转载 &更新时间:日 11:01:31 & 作者:阿凡卢
这篇文章主要介绍了详解C#如何读写config配置文件,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
配置文件概述:
应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的。它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序。配置文件的根节点是configuration。我们经常访问的是appSettings,它是由.Net预定义的配置节。我们经常使用的配置文件的架构是客诉下面的形式。先大概有个印象,通过后面的实例会有一个比较清楚的认识。下面的“配置节”可以理解为进行配置一个XML的节点。
对于一个config文件:
&?xml version="1.0" encoding="utf-8" ?&
&configuration&
&appSettings&
&add key="ServerIP" value="127.0.0.1"&&/add&
&add key="DataBase" value="WarehouseDB"&&/add&
&add key="user" value="sa"&&/add&
&add key="password" value="sa"&&/add&
&/appSettings&
&/configuration&
对config配置文件的读写类:
using System.Collections.G
using System.L
using System.T
using System.Text.RegularE
using System.C
using System.ServiceM
using System.ServiceModel.C
namespace NetUtilityLib
public static class ConfigHelper
//依据连接串名字connectionName返回数据连接字符串
public static string GetConnectionStringsConfig(string connectionName)
//指定config文件读取
string file = System.Windows.Forms.Application.ExecutableP
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(file);
string connectionString =
config.ConnectionStrings.ConnectionStrings[connectionName].ConnectionString.ToString();
return connectionS
///&summary&
///更新连接字符串
///&/summary&
///&param name="newName"&连接字符串名称&/param&
///&param name="newConString"&连接字符串内容&/param&
///&param name="newProviderName"&数据提供程序名称&/param&
public static void UpdateConnectionStringsConfig(string newName, string newConString, string newProviderName)
//指定config文件读取
string file = System.Windows.Forms.Application.ExecutableP
Configuration config = ConfigurationManager.OpenExeConfiguration(file);
bool exist = //记录该连接串是否已经存在
//如果要更改的连接串已经存在
if (config.ConnectionStrings.ConnectionStrings[newName] != null)
// 如果连接串已存在,首先删除它
if (exist)
config.ConnectionStrings.ConnectionStrings.Remove(newName);
//新建一个连接字符串实例
ConnectionStringSettings mySettings =
new ConnectionStringSettings(newName, newConString, newProviderName);
// 将新的连接串添加到配置文件中.
config.ConnectionStrings.ConnectionStrings.Add(mySettings);
// 保存对配置文件所作的更改
config.Save(ConfigurationSaveMode.Modified);
// 强制重新载入配置文件的ConnectionStrings配置节
ConfigurationManager.RefreshSection("ConnectionStrings");
///&summary&
///返回*.exe.config文件中appSettings配置节的value项
///&/summary&
///&param name="strKey"&&/param&
///&returns&&/returns&
public static string GetAppConfig(string strKey)
string file = System.Windows.Forms.Application.ExecutableP
Configuration config = ConfigurationManager.OpenExeConfiguration(file);
foreach (string key in config.AppSettings.Settings.AllKeys)
if (key == strKey)
return config.AppSettings.Settings[strKey].Value.ToString();
///&summary&
///在*.exe.config文件中appSettings配置节增加一对键值对
///&/summary&
///&param name="newKey"&&/param&
///&param name="newValue"&&/param&
public static void UpdateAppConfig(string newKey, string newValue)
string file = System.Windows.Forms.Application.ExecutableP
Configuration config = ConfigurationManager.OpenExeConfiguration(file);
bool exist =
foreach (string key in config.AppSettings.Settings.AllKeys)
if (key == newKey)
if (exist)
config.AppSettings.Settings.Remove(newKey);
config.AppSettings.Settings.Add(newKey, newValue);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
// 修改system.serviceModel下所有服务终结点的IP地址
public static void UpdateServiceModelConfig(string configPath, string serverIP)
Configuration config = ConfigurationManager.OpenExeConfiguration(configPath);
ConfigurationSectionGroup sec = config.SectionGroups["system.serviceModel"];
ServiceModelSectionGroup serviceModelSectionGroup = sec as ServiceModelSectionG
ClientSection clientSection = serviceModelSectionGroup.C
foreach (ChannelEndpointElement item in clientSection.Endpoints)
string pattern = @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b";
string address = item.Address.ToString();
string replacement = string.Format("{0}", serverIP);
address = Regex.Replace(address, pattern, replacement);
item.Address = new Uri(address);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("system.serviceModel");
// 修改applicationSettings中App.Properties.Settings中服务的IP地址
public static void UpdateConfig(string configPath, string serverIP)
Configuration config = ConfigurationManager.OpenExeConfiguration(configPath);
ConfigurationSectionGroup sec = config.SectionGroups["applicationSettings"];
ConfigurationSection configSection = sec.Sections["DataService.Properties.Settings"];
ClientSettingsSection clientSettingsSection = configSection as ClientSettingsS
if (clientSettingsSection != null)
SettingElement element1 = clientSettingsSection.Settings.Get("DataService_SystemManagerWS_SystemManagerWS");
if (element1 != null)
clientSettingsSection.Settings.Remove(element1);
string oldValue = element1.Value.ValueXml.InnerX
element1.Value.ValueXml.InnerXml = GetNewIP(oldValue, serverIP);
clientSettingsSection.Settings.Add(element1);
SettingElement element2 = clientSettingsSection.Settings.Get("DataService_EquipManagerWS_EquipManagerWS");
if (element2 != null)
clientSettingsSection.Settings.Remove(element2);
string oldValue = element2.Value.ValueXml.InnerX
element2.Value.ValueXml.InnerXml = GetNewIP(oldValue, serverIP);
clientSettingsSection.Settings.Add(element2);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("applicationSettings");
private static string GetNewIP(string oldValue, string serverIP)
string pattern = @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b";
string replacement = string.Format("{0}", serverIP);
string newvalue = Regex.Replace(oldValue, pattern, replacement);
测试代码如下:
class Program
static void Main(string[] args)
//string file = System.Windows.Forms.Application.ExecutablePath + ".config";
//string file1 = AppDomain.CurrentDomain.SetupInformation.ConfigurationF
string serverIP = ConfigHelper.GetAppConfig("ServerIP");
string db = ConfigHelper.GetAppConfig("DataBase");
string user = ConfigHelper.GetAppConfig("user");
string password = ConfigHelper.GetAppConfig("password");
Console.WriteLine(serverIP);
Console.WriteLine(db);
Console.WriteLine(user);
Console.WriteLine(password);
ConfigHelper.UpdateAppConfig("ServerIP", "192.168.1.11");
string newIP = ConfigHelper.GetAppConfig("ServerIP");
Console.WriteLine(newIP);
Console.ReadKey();
catch (Exception ex)
Console.WriteLine(ex.Message);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具Java 读取 INI 配置文件的方法
INI 配置文件是 Windows 以前非常喜欢使用的一种配置文件格式,形如:
[GLOBAL]path = c:\appdata
[JDBC]driver = com.mysql.jdbc.Driverurl = jdbc:mysql://localhost:3306/oschinausername = oschinapassword = 123456
项目可以很方便的读取这个配置,请看下面的测试代码
package net.oschina.
import java.io.F
import java.io.IOE
import org.dtools.ini.*;
public class IniTester {
public static void main(String[] args) throws IOException {
IniFile ini = new BasicIniFile(false);//不使用大小写敏感
IniFileReader reader = new IniFileReader(ini, new File("webapp/WEB-INF/conf/holiday.dat"));
reader.read();
for(int i=0;i&ini.getNumberOfSections();i++){
IniSection sec = ini.getSection(i);
System.out.println("---- " + sec.getName() + " ----");
for(IniItem item : sec.getItems()){
System.out.println(item.getName() + " = " + item.getValue());
但是,经过测试发现该类库对中文不支持,打印出来全部是方块,打开源码一看,原来它默认使用了 ASCII 的编码方式,而且不能修改,只好修改其源码 IniFileWriter 类的 ENCODING 常量,值改为 UTF-8 即可。
还有另外一个开源项目
也可以用来读写 ini 文件,功能很强大,但是体积庞大。
ini文件感觉舒服很多,ini4j功能好丰富~
红薯大侠,我在下面提了小问题~是不是太老了木有看到呀,嘿嘿
红薯大侠又偷偷改人家的包包~
外国人心中只有拉丁文......
一直没听说有ini的读写包,这下好了
麻烦把改的地方给大家晒晒撒,是从new String(String.getBytes("encoding"))改Stream还是用OutputStreamWriter(new FileOutputStream(file), strEncode) 改写?
引用来自#4楼&张林&的帖子
红薯大侠又偷偷改人家的包包~
外国人心中只有拉丁文......
一直没听说有ini的读写包,这下好了
麻烦把改的地方给大家晒晒撒,是从new String(String.getBytes("encoding"))改Stream还是用OutputStreamWriter(new FileOutputStream(file), strEncode) 改写?
打屁股,没注意看帖子里写了:
修改其源码 IniFileWriter 类的 ENCODING 常量,值改为 UTF-8 即可
我早用在项目上了,,那时对比过Java INI Package这个项目最快,最简单,只有十个类不到
这个包ini能加密吗?cnpack上有一个ini加密的方法
红薯大侠,您好。请教一个问题, IniFileWriter 类的 ENCODING 常量,值改为 UTF-8 ,但读取出来的还是乱码。我用的是1.1版本的。为什么呢?

我要回帖

更多关于 c 读取配置文件 的文章

 

随机推荐