天天看點

将檔案内容複制到另外檔案

本例顯示了一個使用本文前面介紹的幾個

%Library.File

方法的樣例類。

在示例類

Demo.FileDemo

中,

ProcessFile()

方法接受輸入檔案和輸出檔案,并調用

SetUpInputFile()

SetUpOutputFile()

打開檔案,一個用于讀取,另一個用于寫入。然後,它逐行讀取輸入檔案,并調用

ProcessLine()

對每行的内容執行一個或多個替換,将每行的新内容寫入輸出檔案。

/// 設定輸入檔案
/// 1. 建立檔案對象
/// 2. 打開檔案閱讀
/// 3. 傳回檔案對象的句柄
ClassMethod SetUpInputFile(filename As %String) As %File
{
    Set fileObj = ##class(%File).%New(filename)
    Set status = fileObj.Open("RU")
    if $$$ISERR(status) { 
        do $system.Status.DisplayError(status) 
        quit $$$NULLOREF
    }
    quit fileObj
}

/// 設定輸出檔案
/// 1. 為檔案建立目錄結構
/// 2. 建立檔案對象
/// 3. 打開檔案進行寫入
/// 4. 傳回檔案對象的句柄
ClassMethod SetUpOutputFile(filename As %String) As %File
{
    set dir=##class(%File).GetDirectory(filename)
    do ##class(%File).CreateDirectoryChain(dir)
    Set fileObj = ##class(%File).%New(filename)
    Set status = fileObj.Open("WSN")
    If ($SYSTEM.Status.IsError(status)) {
        do $system.Status.DisplayError(status)
        quit $$$NULLOREF
    }
    quit fileObj
}

/// 處理一行,使用$REPLACE對該行執行一系列替換
ClassMethod ProcessLine(line As %String = "") As %String
{
    set newline = line

    set newline = $REPLACE(newline, "Original", "Jamaican-Style")
    set newline = $REPLACE(newline, "traditional", "innovative")
    set newline = $REPLACE(newline, "orange juice", "lime juice")
    set newline = $REPLACE(newline, "orange zest", "ginger")
    set newline = $REPLACE(newline, "white sugar", "light brown sugar")

    quit newline
}

/// 處理輸入檔案,對内容執行一系列替換,并将新内容寫入輸出檔案
ClassMethod ProcessFile(inputfilename As %String = "", outputfilename As %String = "")
{
    // 確定檔案名被傳入
    if (inputfilename="") || (outputfilename="") {
        write !, "ERROR: missing file name"
        quit
    }

    // 打開輸入檔案進行讀取
    set inputfile = ..SetUpInputFile(inputfilename)
    if (inputfile = $$$NULLOREF) quit

    // 打開輸出檔案進行寫入
    set outputfile = ..SetUpOutputFile(outputfilename)
    if (outputfile = $$$NULLOREF) quit

    // 循環輸入檔案中的每一行
    // 雖然不在檔案的末尾:
    // 1. 從檔案中讀出一行
    // 2. 調用ProcessLine()來處理該行
    // 3. 将新的行内容寫入輸出檔案
    while (inputfile.AtEnd = 0) {
        set line = inputfile.ReadLine(,.status)
         if $$$ISERR(status) { 
             do $system.Status.DisplayError(status) 
         }
         else {
             set newline = ..ProcessLine(line)
             do outputfile.WriteLine(newline)
         }
    }
    
    // 關閉輸入和輸出檔案
     do inputfile.Close()
     do outputfile.Close()
}           

複制

調用

ProcessFile()

方法,如下所示:

DHC-APP>do ##class(Demo.FileDemo).ProcessFile("e:\temp\new.txt", "e:\temp\old.txt")           

複制

如果輸入檔案

e:\temp\new.txt

. txt包含以下内容:

Original Whole Berry Cranberry Sauce

This traditional whole berry cranberry sauce gets its distinctive flavor 
from the freshly squeezed orange juice and the freshly grated orange zest.

2 tsp freshly grated orange zest
1 1/4 cups white sugar
1/4 cup freshly squeezed orange juice
3 cups cranberries (12 oz. package)

1. Grate orange zest into a bowl and set aside.
2. Combine the sugar and orange juice in a saucepan. Bring to a boil over medium-low 
heat and stir until sugar is dissolved.
3. Add cranberries and cook over medium-high heat, stirring occasionally, until the 
cranberries have popped.
4. Add the cranberry mixture into the bowl with the orange zest, and stir. Let cool.
5. Cover bowl and chill.           

複制

那麼輸出檔案

e:\temp\old.txt

将包含以下内容:

Jamaican-Style Whole Berry Cranberry Sauce

This innovative whole berry cranberry sauce gets its distinctive flavor 
from the freshly squeezed lime juice and the freshly grated ginger.

2 tsp freshly grated ginger
1 1/4 cups light brown sugar
1/4 cup freshly squeezed lime juice
3 cups cranberries (12 oz. package)

1. Grate ginger into a bowl and set aside.
2. Combine the sugar and lime juice in a saucepan. Bring to a boil over medium-low 
heat and stir until sugar is dissolved.
3. Add cranberries and cook over medium-high heat, stirring occasionally, until the 
cranberries have popped.
4. Add the cranberry mixture into the bowl with the ginger, and stir. Let cool.
5. Cover bowl and chill.           

複制