2010-03-08 133 views
0

我有一個當前運行在我編寫的Delphi應用程序中的進程,我需要將其轉換爲將在我們的Web應用程序上運行的Java進程。基本上我們的州財政(遺產)系統需要這個文件在一個特定的輸出。在德爾福它是這樣的:使用Java創建固定長度的平面文件

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編程!

張國榮

+0

我發現這個鏈接,但沒有看到你如何格式化輸出: http://www.rgagnon.com/javadetails/java-0461.html – Leslie 2010-03-08 17:29:50

+1

爲了將來的參考,這不是你如何編碼在Stackoverflow的代碼塊。只需將所有代碼縮進4個空格。 – Pointy 2010-03-08 17:30:20

+0

感謝您的信息!我嘗試了各種各樣的東西,讓它看起來很正確!感謝編輯! – Leslie 2010-03-08 17:44:13

回答

1

這些都是在Java中所有相對簡單的操作(或者很多其他語言的,對於這個問題)。 對於Java中的用例,通過JDBC直接訪問數據庫可能是最容易的,如here所示。檢索數據後,可以使用String.format(...)按照需要的方式格式化數據,然後將其寫入文件(如here所述)。

+0

是的,我知道他們是簡單的操作,但是當你不確定要搜索什麼時,很難找到答案!感謝您的鏈接,特別是String.format! – Leslie 2010-03-08 17:44:28

+0

很高興有幫助。 :) – 2010-03-08 18:08:04