前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >将文件内容复制到另外文件

将文件内容复制到另外文件

作者头像
用户7741497
发布2022-07-06 09:36:40
9520
发布2022-07-06 09:36:40
举报
文章被收录于专栏:hml_知识记录

本例显示了一个使用本文前面介绍的几个%Library.File方法的样例类。

在示例类Demo.FileDemo中,ProcessFile()方法接受输入文件和输出文件,并调用SetUpInputFile()SetUpOutputFile()打开文件,一个用于读取,另一个用于写入。然后,它逐行读取输入文件,并调用ProcessLine()对每行的内容执行一个或多个替换,将每行的新内容写入输出文件。

代码语言:javascript
复制
/// 设置输入文件
/// 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()方法,如下所示:

代码语言:javascript
复制
DHC-APP>do ##class(Demo.FileDemo).ProcessFile("e:\temp\new.txt", "e:\temp\old.txt")

如果输入文件e:\temp\new.txt. txt包含以下内容:

代码语言:javascript
复制
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将包含以下内容:

代码语言:javascript
复制
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.

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档