就是希望能像读取ini文件那样直接读取properties文件中对应项“=”后的内容

解决方案 »

  1.   

    JAVA读取都是利用了自身封装的类,Delphi读取难度比较大,个人有个想法,可以把它作为一个流文件读入,然后比对等号,或者分析properties文件的格式,然后通过数据结构进行读取
      

  2.   

    谢谢你的解答,能否给段实现的代码,下面是properties文件的部分内容datasource.driverClassName=oracle.jdbc.driver.OracleDriver
    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
      

  3.   

    直接将你的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;
      

  4.   

    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.
      

  5.   

    大家的思路都是用stringlist
    我觉得用name和value属性更方便
    用loadfromfile( filename )从文件中加载,删掉注释和空行
    是否存在使用indexofname( propname )>=0
    获取值使用stringlist.values[ propname ]
      

  6.   

    kampan与hongqi162给出的方法都可以解决问题kampan的稍微改动一下就可
    hongqi162的要全面一些
    非常感谢两位