用Java创建固定长度的平面文件

时间:2010-03-08 17:27:59

标签: java web-applications export flat-file

我有一个当前在我编写的Delphi应用程序中运行的进程,我需要将其转换为将在我们的Web应用程序上运行的Java进程。基本上我们的State Financial(遗留)系统需要在特定输出中使用此文件。在Delphi中就是这样:

procedure CreateSHAREJournalFile(AppDate : string; ClassCode : string; BudgetRef : String; AccountNumber : string; FYEStep : integer);
var
GLFileInfo : TStrings;
MPayFormat, HPayFormat, TPayFormat : string;
const
//this is the fixed length format for each item in the file
HeaderFormat = '%-1s%-5s%-10s%-8s%-12s%-10s%-21s%-3s%-71s%-3s%-20s%-1s';
DetailFormat = '%-1s%-5s%-9s%-10s%-10s%-10s%-10s%-8s%-6s%-5s%-5s%-5s%-8s%-25s%-10s%-60s%-28s%-66s%-28s';
begin
  try
//get the data from the query
    with dmJMS.qryShare do
    begin
      SQL.Clear;
      SQL.Add('SELECT SUM(TOTHRPAY) As HourPay, SUM(TOTMLPAY) As MilePay, SUM(TOTALPAY) AS TotalPay FROM JMPCHECK INNER JOIN JMPMAIN ON JMPCHECK.JURNUM = JMPMAIN.JURNUM WHERE PANELID LIKE ''' + Copy(AppDate, 3, 6) + '%'' ');
      if FYEStep > -1 then
        SQL.Add('AND WARRANTNO = ' + QUotedStr(IntToStr(FYEStep)));
      Active := True;
//assign totals to variables so they can be padded with leading zeros
    MPayFormat := FieldByName('MilePay').AsString;
    while length(MPayFormat) < 28 do <br>MPayFormat := '0' + MPayFormat;
    HPayFormat := FieldByName('HourPay').AsString;
    while length(HPayFormat) < 28 do <br>HPayFormat := '0' + HPayFormat;
    TPayFormat := Format('%f' ,[(FieldByName('TotalPay').AsCurrency)]);
    while length(TPayFormat) < 27 do
    TPayFormat := '0' + TPayFormat;
    TPayFormat := '-' + TPayFormat;
//create a TStringlist to put each line item into
    GLFileInfo := TStringList.Create;
//add header info using HeaderFormat defined above
    GLFileInfo.Add(Format(HeaderFormat, ['H', '21801', 'NEXT', FormatDateTime('MMDDYYYY', Today), '', 'ACTUALS', '', 'EXT', '', 'EXT', '', 'N']));
//add detail info using DetailFormat defined above
    GLFileInfo.Add(Format(DetailFormat, ['L', '21801', '1', 'ACTUALS', AccountNumber, '', '1414000000', '111500', '', '01200', ClassCode, '', BudgetRef, '', AccountNumber + '0300', '', MPayFormat, '', MPayFormat]));
    GLFileInfo.Add(Format(DetailFormat, ['L', '21801', '2', 'ACTUALS', AccountNumber, '', '1414000000', '111500', '', '01200', ClassCode, '', BudgetRef, '', AccountNumber + '0100', '', HPayFormat, '', HPayFormat]));
    GLFileInfo.Add(Format(DetailFormat, ['L', '21801', '3', 'ACTUALS', '101900', '', '1414000000', '111500', '', '01200', ClassCode, '', BudgetRef, '', '', '', TPayFormat, '', TPayFormat]));
//save TStringList to text file
    GLFileINfo.SaveToFile(ExtractFilePath(Application.ExeName) + 'FileTransfer\GL_' + formatdateTime('mmddyy', Today) + SequenceID + '24400' + '.txt');
    end;
  finally
    GLFileINfo.Free;
  end;
end;

Java格式选项中是否存在等价物?还是保存到文本文件的TStringList?

感谢您提供任何信息....还没有做过很多Java编程!

莱斯利

1 个答案:

答案 0 :(得分:1)

这些都是Java中相对简单的操作(或许多其他语言,就此而言)。 对于Java中的用例,最简单的方法是直接通过JDBC访问数据库,如here所示。检索数据后,您可以使用String.format(...)以您需要的方式格式化数据,然后您可以将其写入文件(如here所述)。