文章目录

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

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

示例

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

第七章 将文件内容复制到另外文件相关推荐

  1. Vue第七章:项目环境配置及单文件组件 vue脚手

    第七章:项目环境配置及单文件组件 vue脚手架 回顾: 组件之间的通信 父传子:正向传递 vue允许 自动触发 ​ props ​ 1.先在子组件中定义期待的属性名和类型 ​ 2.在父组件中调用子组件 ...

  2. 知网CAJ文件内容复制小助手

    知网CAJ文件内容复制小助手,仅限可复制文献使用. 主要功能 (1)一键去空,去换行,去特殊字符 (2)去空的时候,检测英语,遍历相邻两位字符,判断是否字母+空,如果是字母+空,则不去空.否则去空 ...

  3. python同时对文件进行读写操作-Python实现的读取文件内容并写入其他文件操作示例...

    本文实例讲述了Python实现的读取文件内容并写入其他文件操作.分享给大家供大家参考,具体如下: 文件目录结构,如图: read_file.py是工作文件,file_test.py是读取文件源,wri ...

  4. python打开文件并读取内容-Python实现的读取文件内容并写入其他文件操作示例

    本文实例讲述了Python实现的读取文件内容并写入其他文件操作.分享给大家供大家参考,具体如下: 文件目录结构,如图: read_file.py是工作文件,file_test.py是读取文件源,wri ...

  5. python文件读取输出-python分批定量读取文件内容,输出到不同文件中的方法

    一.文件内容的分发 应用场景:分批读取共有358086行内容的txt文件,每取1000条输出到一个文件当中 # coding=utf-8 # 分批读取共有358086行内容的txt文件,每取1000条 ...

  6. python连续写入文件操作_Python实现的读取文件内容并写入其他文件操作示例

    本文实例讲述了Python实现的读取文件内容并写入其他文件操作.分享给大家供大家参考,具体如下: 文件目录结构,如图: read_file.py是工作文件,file_test.py是读取文件源,wri ...

  7. linux下qt浏览word文件内容,Qt获取office文件内容

    Qt获取office文件内容 需要获取word文件的文件内容.网上找了好久,大部分都是excel的.而word的很少.所以在这里记录一下,方便大家查阅和自己使用. 使用的Qt版本是5.4.2 . 下面 ...

  8. python读取文件内容并操作_Python实现的读取文件内容并写入其他文件操作示例

    本文实例讲述了Python实现的读取文件内容并写入其他文件操作.分享给大家供大家参考,具体如下: 文件目录结构,如图: read_file.py是工作文件,file_test.py是读取文件源,wri ...

  9. Linux下文件内容更新了,文件夹时间戳却没变?

    在日常多人协作时,最开始习惯看文件夹更新时间来查看是否有更新,但发现总是不能如实反映情况, 文件夹日期有时变,有时不变.很是困惑,就来探究下. Desk with stationary. Studio ...

最新文章

  1. [华清远见]FPGA公益培训
  2. macOS Big Sur 11当前存在的一些问题(更新中)
  3. rest-framework解析器,url控制,分页,响应器,渲染器,版本控制
  4. php jpeg不支持,php jpeg不支持怎么办
  5. python类方法在类外定义_第7.15节 Python中classmethod定义的类方法详解
  6. 信息安全工程师考试大纲(含pdf)
  7. MobileNet-SSD网络解析
  8. tar打包命令的用法
  9. GMM-HMM 详解
  10. android 手机内存分配,【扫盲贴』关于android手机中RAM(也就是 运行内存)的分配...
  11. MLO/uboot-spl.bin和uboot.img/uboot.bin
  12. 王阳明没法帮你造出光刻机
  13. linux 命令总结大全
  14. Github上的开源项目2
  15. vue-cli中使用高德地图及其插件
  16. uni-app 相机相册选择图片转base64
  17. 丁益祥c语言答案pdf,丁益祥|
  18. This primary key of “id“ is primitive !不建议如此请使用包装类 in Class
  19. android换苹果,苹果换手机怎么转移数据?苹果、安卓都可以一键转移数据
  20. MyBatis-Plus 条件构造器之实体(Entity)查询

热门文章

  1. MySQL ODBC驱动安装和配置数据源
  2. 1 ,storm 框架介绍
  3. 浅析PC机串口通讯流控制
  4. 住宅园区光纤布线案例
  5. uefi启动 多硬盘gtp_关于UEFI启动+GPT分区的一些经验
  6. JAVA考试多选题判断得分
  7. php和html关于读取文件的小项目
  8. 使用AI技术获取图片文字与识别图像内容
  9. 负载均衡过程中的一台机器当掉了
  10. 什么是Promise?Promise的优点