Skip to content

Instantly share code, notes, and snippets.

@horsley
Created August 14, 2013 05:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save horsley/6228232 to your computer and use it in GitHub Desktop.
Save horsley/6228232 to your computer and use it in GitHub Desktop.
文件分割器 for jokerdeng
// lineSpliter project main.go
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
)
func main() {
var path, newFile, line string
var size int
var err error
var inputFile, outputFile *os.File
fmt.Println("*******************************************")
fmt.Println("* 文件分割器 for jokerdeng *")
fmt.Println("* by horsleyli *")
fmt.Println("*******************************************\n")
reader := bufio.NewReader(os.Stdin)
fmt.Print("第一步、拖放要分割的文件到本窗口,然后按回车\n\t")
line, _ = reader.ReadString('\n') //bug of golang
path = strings.TrimSpace(line)
fmt.Print("第二步、输入分割的行数大小,然后按回车\n\t")
fmt.Scan(&size)
fmt.Println(size)
if inputFile, err = os.Open(path); err != nil {
fmt.Println("输入文件打开失败!" + err.Error())
os.Exit(1)
}
defer inputFile.Close()
scanner := bufio.NewScanner(inputFile)
ext := filepath.Ext(path)
rs := []rune(path)
totalCount := 1
thisCount := 1
for scanner.Scan() {
if newFile == "" {
newFile = string(rs[0:len(rs)-len(ext)]) + "_" + strconv.Itoa(totalCount/size+1) + ext
//fmt.Println(newFile)
if outputFile, err = os.Create(newFile); err != nil {
fmt.Println("输出文件打开失败!" + err.Error())
os.Exit(1)
}
}
outputFile.WriteString(scanner.Text() + "\r\n")
thisCount += 1
totalCount += 1
if thisCount > size {
outputFile.Close()
fmt.Println("写出文件:" + newFile)
newFile = ""
thisCount = 1
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment