Skip to content

Instantly share code, notes, and snippets.

@jemygraw
Created May 16, 2014 13:32
Show Gist options
  • Save jemygraw/7b39389068ab271d1d33 to your computer and use it in GitHub Desktop.
Save jemygraw/7b39389068ab271d1d33 to your computer and use it in GitHub Desktop.
Merge File Content
package main
import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
func merge(rootPath string) {
outFileName := "d:\\merge_result.txt"
outFile, openErr := os.OpenFile(outFileName, os.O_CREATE|os.O_WRONLY, 0600)
if openErr != nil {
fmt.Printf("Can not open file %s", outFileName)
}
bWriter := bufio.NewWriter(outFile)
filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
fmt.Println("Processing:", path)
//这里是文件过滤器,表示我仅仅处理txt文件
if strings.HasSuffix(path, ".txt") {
fp, fpOpenErr := os.Open(path)
if fpOpenErr != nil {
fmt.Printf("Can not open file %v", fpOpenErr)
return fpOpenErr
}
bReader := bufio.NewReader(fp)
for {
buffer := make([]byte, 1024)
readCount, readErr := bReader.Read(buffer)
if readErr == io.EOF {
break
} else {
bWriter.Write(buffer[:readCount])
}
}
}
return err
})
bWriter.Flush()
}
func main() {
var rootPath = "D:\\JemyGraw\\GoProjects\\folder"
merge(rootPath)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment