文章
· 七月 18, 2021 阅读大约需 4 分钟

第七章 将文件内容复制到另外文件

第七章 将文件内容复制到另外文件

示例

本例显示了一个使用本文前面介绍的几个%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.

讨论 (0)1
登录或注册以继续