Goland 统计文本文件行数

180it 2021-12-09 PM 606℃ 0条
package main

import (
  "bufio"
  "bytes"
  "fmt"
  "golang.org/x/text/encoding/simplifiedchinese"
  "golang.org/x/text/transform"
  "io"
  "io/ioutil"
  "os"
  "path"
  "path/filepath"
  "runtime"
  "strings"
  "time"
)

const AppName = "取得文本行数"
const AppVersion = "3.30.01"
//const AppCode ="LineCount"

var doFileList []string
var subCount int

func DoOneFile(number int,SourceFile string) {
  ConsoleWriteLnGBK("%d: Counting: %s...",number,path.Base(filepath.ToSlash(SourceFile)))
  fi, err := os.Open(SourceFile)
  if err != nil {
    panic(err)
  }
  defer func(f *os.File) {
    err := f.Close()
    if err != nil {

    }
  }(fi)
  reader := bufio.NewReader(fi)
  var aCount =0
  for {
    _,_, err := reader.ReadLine()
    if err == io.EOF {
      break
    }
    if err != nil {
      panic(err)
    }
    aCount++

  }
  ConsoleWriteLnGBK("%d: Done: %s: %d line(s).", number,path.Base(filepath.ToSlash(SourceFile)),aCount)
  subCount+=aCount
}

func DoOnePath(SourcePath string) {
  fileInfoList, err := ioutil.ReadDir(SourcePath)
  if err != nil {
    panic(err)
  }
  for i := range fileInfoList {
    if !fileInfoList[i].IsDir(){
      doFileList = append(doFileList,filepath.Join(SourcePath, fileInfoList[i].Name()))
    }
  }
}

func ConsoleWriteLnGBK(aContent string, a ...interface{}) {
  if strings.ToLower(runtime.GOOS[0:3])=="win" {
    b, _ := ioutil.ReadAll(transform.NewReader(bytes.NewReader([]byte(fmt.Sprintf(aContent, a...))), simplifiedchinese.GBK.NewEncoder()))
    _, err := os.Stdout.Write(b)
    if err != nil {
      fmt.Printf("Error: %s\n", aContent)
      fmt.Printf("Error: %s\n", err)
      os.Exit(1)
    }
    fmt.Printf("\n")
  }else{
    fmt.Printf(aContent+"\n", a...)
  }
}

func main() {
  defer func() {
    if err := recover(); err != nil {
      ConsoleWriteLnGBK("Error: %s", err)
      os.Exit(1)
    }
  }()
  t1:=time.Now()

  //Golang only support UTF8,but our workflow platform only support GBK。。。
  //fmt.Fprintf(os.Stdout, "%s %s\n",AppName, AppVersion)
  ConsoleWriteLnGBK("%s %s", AppName, AppVersion)

  if cap(os.Args) < 7 {
    panic("Program Params not enough!")
  }

  InputPath := os.Args[1]
  //WorkPath := os.Args[2]
  //TaskID:= os.Args[3]
  //InstanceID:= os.Args[4]
  //ImplementID:= os.Args[5]
  fRuleName := os.Args[6]
  fRuleVersion := os.Args[7]

  if fRuleName != AppName {
    panic(fmt.Sprintf("Name not match! db:%s app:%s !", fRuleName, AppName))
  }
  if fRuleVersion != AppVersion {
    panic(fmt.Sprintf("Version not match! db:%s app:%s !", fRuleVersion, AppVersion))
  }

  fmt.Println(runtime.GOOS+" "+runtime.GOARCH+" "+runtime.Version())

  paths := strings.Split(InputPath, "|")
  for _, pathOne := range paths {
    if len(pathOne) > 0 {
      DoOnePath(pathOne)
    }
  }

  for i := range doFileList {
    DoOneFile(i,doFileList[i])
  }

  ConsoleWriteLnGBK("Total lines: %d.", subCount)
  ConsoleWriteLnGBK("Time elapsed: %s", time.Now().Sub(t1))

  fmt.Printf("Finish:\n")
}
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

Goland 统计文本文件行数