加入收藏 | 设为首页 | 会员中心 | 我要投稿 三明站长网 (https://www.0598zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

Go语言清除文件中空行的方法

发布时间:2016-12-04 13:05:43 所属栏目:教程 来源:站长网
导读:本文实例讲述了Go语言清除文件中空行的方法。分享给大家供大家参考。具体实现方法如下: 这里使用Go语言读取源文件,去掉空行,并写到目标文件 复制代码 代码如下:/** nbsp;* Created with IntelliJ IDEA. nbsp;* User: hyper-carrot nbsp;* Date: 12-8-31

本文实例讲述了Go语言清除文件中空行的方法。分享给大家供大家参考。具体实现方法如下:

这里使用Go语言读取源文件,去掉空行,并写到目标文件
复制代码 代码如下:/**
nbsp;* Created with IntelliJ IDEA.
nbsp;* User: hyper-carrot
nbsp;* Date: 12-8-31
nbsp;* Time: 下午4:04
nbsp;* To change this template use File | Settings | File Templates.
nbsp;*/
package main
import (
nbsp;"os"
nbsp;"bufio"
nbsp;"fmt"
)
func DeleteBlankFile(srcFilePah string, destFilePath string) error {
nbsp;srcFile, err := os.OpenFile(srcFilePah, os.O_RDONLY, 0666)
nbsp;defer srcFile.Close()
nbsp;if err != nil {
nbsp;nbsp;return err
nbsp;}
nbsp;srcReader := bufio.NewReader(srcFile)
nbsp;destFile, err := os.OpenFile(destFilePath, os.O_WRONLY|os.O_CREATE, 0666)
nbsp;defer destFile.Close()
nbsp;if err != nil {
nbsp;nbsp;return err
nbsp;}
nbsp;var destContent string
nbsp;for {
nbsp;nbsp;str, _ := srcReader.ReadString('n')
nbsp;nbsp;if err != nil {
nbsp;nbsp;nbsp;if err == io.EOF {
nbsp;nbsp;nbsp;nbsp;fmt.Print("The file end is touched.")
nbsp;nbsp;nbsp;nbsp;break
nbsp;nbsp;nbsp;} else {
nbsp;nbsp;nbsp;nbsp;return err
nbsp;nbsp;nbsp;}
nbsp;nbsp;}
nbsp;nbsp;if 0 == len(str) || str == "rn" {
nbsp;nbsp;nbsp;continue
nbsp;nbsp;}
nbsp;nbsp;fmt.Print(str)
nbsp;nbsp;destFile.WriteString(str)
nbsp;}
nbsp;return nil
}
func main() {
nbsp;DeleteBlankFile("e:src.txt", "e:dest.txt")
}

希望本文所述对大家的Go语言程序设计有所帮助。

(编辑:三明站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!