直接将你的properties当成普通的文件即可,我写两个函数,没有调试,你自己试试,应该可以,调用GetValueFromPro即可 //去掉每行中无用数据 function CutComma(sData: string): string; var I: Integer; NewData: string; begin Result := ''; NewData := Trim(sData); for I := 1 to Length(NewData) do begin if (NewData[I] in [' ', #9]) then begin Delete(NewData, I, 1); end; end; Result := NewData; end;{================================================================== 输入参数:Param1 文件对应项如 c3p0.minPoolSize 返回值: 修改记录: ===================================================================} function GetValueFromPro(Param1: string): string; var s, sRtn: string; fFileName: string; i: Integer; sList: TStringList; begin sRtn := ''; fFileName := ExtractFilePath(Application.ExeName) + 'properties.txt'; sList := TStringList.Create; sList.LoadFromFile(fFileName); //将文件内容读入内存 s := Param1; for i := 0 to sList.Count - 1 do begin if Pos(s, sList[i]) > 0 then //如果存在对应项 begin sRtn := Copy(CutComma(sList[i]), Pos(s, sList[i])+2, Length(CutComma(sList[i])) - (Pos(s, sList[i])+1) ); break; end; end; Result := sRtn; end;
unit Properties;interfaceuses Classes, SysUtils, Dialogs; type TProperties = class(TObject) private FStringList: TStringList; FFileName: string; public Constructor Create( fileName: string ); Destructor Destroy;override; procedure Load( fileName: string ); function getValues( key: string ):string; procedure setValues( key, value: string ); procedure append( key, value: string ); procedure save; end; implementation{ TProperties }procedure TProperties.append(key, value: string); begin FStringList.Add( key + '='+ value ); end;constructor TProperties.Create(fileName: string); begin FStringList:= TStringList.Create; FFileName:= fileName; Load( fileName ); end;destructor TProperties.Destroy; begin FStringList.Free; inherited; end;function TProperties.getValues(key: string): string; var intI: Integer; begin for intI:=0 to FStringList.Count-1 do begin if ( FStringList.Strings[ intI ] <> '') and ( Copy( FStringList.Strings[ intI ], 0, 1 ) <> '#' ) then begin if Pos( key, FStringList.Strings[ intI ] ) > 0 then Result:= Copy( FStringList.Strings[ intI ], Pos( '=', FStringList.Strings[ intI ] ) + 1 , length( FStringList.Strings[ intI ] ) ); end; end; end;procedure TProperties.Load(fileName: string); begin FStringList.Clear; if FileExists( fileName ) then FStringList.LoadFromFile( fileName ); end;procedure TProperties.save; begin FStringList.SaveToFile( FFileName ); end;procedure TProperties.setValues(key, value: string); var intI: Integer; begin for intI:=0 to FStringList.Count-1 do begin if ( FStringList.Strings[ intI ] <> '') and ( Copy( FStringList.Strings[ intI ], 0, 1 ) <> '#' ) then begin if Pos( key, FStringList.Strings[ intI ] ) > 0 then FStringList.Strings[ intI ] := Copy( FStringList.Strings[ intI ], 0, Pos( '=', FStringList.Strings[ intI ] ) ) + value; end; end; end;end.
datasource.url=jdbc:oracle:thin:@202.96.155.167:1521:rmam
datasource.username=ypzs
datasource.password=ypzs
c3p0.minPoolSize=5
c3p0.maxPoolSize=50
c3p0.initialPoolSize=10
c3p0.maxIdleTime=600
c3p0.acquireIncrement=5
c3p0.maxStatements=100
c3p0.idleConnectionTestPeriod=900
c3p0.numHelperThreads=10
#database.dialect=com.syscanhc.oa.dao.ibatis.ext.MySQLDialect
database.dialect=com.syscanhc.oa.dao.ibatis.ext.OracleDialect
#database.dialect=com.syscanhc.oa.dao.ibatis.ext.DB2Dialect
//去掉每行中无用数据
function CutComma(sData: string): string;
var
I: Integer;
NewData: string;
begin
Result := '';
NewData := Trim(sData);
for I := 1 to Length(NewData) do
begin
if (NewData[I] in [' ', #9]) then
begin
Delete(NewData, I, 1);
end;
end;
Result := NewData;
end;{==================================================================
输入参数:Param1 文件对应项如 c3p0.minPoolSize
返回值:
修改记录:
===================================================================}
function GetValueFromPro(Param1: string): string;
var
s, sRtn: string;
fFileName: string;
i: Integer;
sList: TStringList;
begin
sRtn := '';
fFileName := ExtractFilePath(Application.ExeName) + 'properties.txt';
sList := TStringList.Create;
sList.LoadFromFile(fFileName); //将文件内容读入内存
s := Param1;
for i := 0 to sList.Count - 1 do
begin
if Pos(s, sList[i]) > 0 then //如果存在对应项
begin
sRtn := Copy(CutComma(sList[i]),
Pos(s, sList[i])+2,
Length(CutComma(sList[i])) - (Pos(s, sList[i])+1)
);
break;
end;
end; Result := sRtn;
end;
Classes, SysUtils, Dialogs;
type
TProperties = class(TObject)
private
FStringList: TStringList;
FFileName: string;
public
Constructor Create( fileName: string );
Destructor Destroy;override;
procedure Load( fileName: string );
function getValues( key: string ):string;
procedure setValues( key, value: string );
procedure append( key, value: string );
procedure save;
end;
implementation{ TProperties }procedure TProperties.append(key, value: string);
begin
FStringList.Add( key + '='+ value );
end;constructor TProperties.Create(fileName: string);
begin
FStringList:= TStringList.Create;
FFileName:= fileName;
Load( fileName );
end;destructor TProperties.Destroy;
begin
FStringList.Free;
inherited;
end;function TProperties.getValues(key: string): string;
var
intI: Integer;
begin
for intI:=0 to FStringList.Count-1 do
begin
if ( FStringList.Strings[ intI ] <> '') and ( Copy( FStringList.Strings[ intI ], 0, 1 ) <> '#' ) then
begin
if Pos( key, FStringList.Strings[ intI ] ) > 0 then
Result:= Copy( FStringList.Strings[ intI ], Pos( '=', FStringList.Strings[ intI ] ) + 1 , length( FStringList.Strings[ intI ] ) );
end;
end;
end;procedure TProperties.Load(fileName: string);
begin
FStringList.Clear;
if FileExists( fileName ) then
FStringList.LoadFromFile( fileName );
end;procedure TProperties.save;
begin
FStringList.SaveToFile( FFileName );
end;procedure TProperties.setValues(key, value: string);
var
intI: Integer;
begin
for intI:=0 to FStringList.Count-1 do
begin
if ( FStringList.Strings[ intI ] <> '') and ( Copy( FStringList.Strings[ intI ], 0, 1 ) <> '#' ) then
begin
if Pos( key, FStringList.Strings[ intI ] ) > 0 then
FStringList.Strings[ intI ] := Copy( FStringList.Strings[ intI ], 0, Pos( '=', FStringList.Strings[ intI ] ) ) + value;
end;
end;
end;end.
我觉得用name和value属性更方便
用loadfromfile( filename )从文件中加载,删掉注释和空行
是否存在使用indexofname( propname )>=0
获取值使用stringlist.values[ propname ]
hongqi162的要全面一些
非常感谢两位