华域联盟 Golang Go语言清除文件中空行的方法

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

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

这里使用Go语言读取源文件,去掉空行,并写到目标文件

复制代码 代码如下: /**

 * Created with IntelliJ IDEA.

 * User: hyper-carrot

 * Date: 12-8-31

 * Time: 下午4:04

 * To change this template use File | Settings | File Templates.

 */

package main

import (

 "os"

 "bufio"

 "fmt"

)

func DeleteBlankFile(srcFilePah string, destFilePath string) error {

 srcFile, err := os.OpenFile(srcFilePah, os.O_RDONLY, 0666)

 defer srcFile.Close()

 if err != nil {

  return err

 }

 srcReader := bufio.NewReader(srcFile)

 destFile, err := os.OpenFile(destFilePath, os.O_WRONLY|os.O_CREATE, 0666)

 defer destFile.Close()

 if err != nil {

  return err

 }

 var destContent string

 for {

  str, _ := srcReader.ReadString('\n')

  if err != nil {

   if err == io.EOF {

    fmt.Print("The file end is touched.")

    break

   } else {

    return err

   }

  }

  if 0 == len(str) || str == "\r\n" {

   continue

  }

  fmt.Print(str)

  destFile.WriteString(str)

 }

 return nil

}

func main() {

 DeleteBlankFile("e:\\src.txt", "e:\\dest.txt")

}

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

本文由 华域联盟 原创撰写:华域联盟 » Go语言清除文件中空行的方法

转载请保留出处和原文链接:https://www.cnhackhy.com/109981.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们