3

#yyds干货盘点#golang生成vcf格式文件详解

 2 years ago
source link: https://blog.51cto.com/u_12040959/5115124
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

​3.源码​

package number


/*
@Time : 2021/12/22 17:44
@Author :dengfeng_hu
@File : phone
@Software: GoLand
*/

import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
)

const (
inputFilePath = "D:/phone.txt"
fileName = "洛阳"
last = "36"
fileSize = 5
outputPath = "D://phoneNumber"
)

func getNum(fileName string) int {
var num = 1
for {
if _,err:=os.Stat("D://phoneNumber/" + fileName + strconv.Itoa(num)+".vcf");
os.IsNotExist(err){
return num
}
num++
}
}


func Exe() {
mid := read()
for i:=0;i<len(mid);i=i+5{
var subList []string
if i+fileSize<len(mid){
subList = mid[i:i+fileSize:len(mid)]
}else{
subList = mid[i:len(mid):len(mid)]
}
fmt.Println(subList)
MyTest(subList,fileName,last)
}
}

func MyTest(mid []string,fileName string,last string){
num := getNum(fileName)
file, err := os.OpenFile(outputPath+"/" + fileName +strconv.Itoa(num) +".vcf", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
fmt.Println("文件打开失败", err)
return
}
//及时关闭file句柄
defer file.Close()
write := bufio.NewWriter(file)
count:=1
for i := 0;i<len(mid);i++{
for j := 0;j<100;j++{
phoneNumber := strings.ReplaceAll(mid[i],"\r\n","") + fmt.Sprintf("%02d", j) + last
makeNumber(phoneNumber,write,strconv.Itoa(count))
count++
}
}
makeNumber("15804299051",write,strconv.Itoa(count))
write.Flush()
fmt.Println("文件创建成功,时间:"+time.Now().Format("2006-01-02 15:04:05"))
}


func makeNumber(num string,w *bufio.Writer,index string){
w.WriteString("BEGIN:VCARD" + "\r\n")
w.WriteString("VERSION:3.0" + "\r\n")
w.WriteString("FN;CHARSET=UTF-8:1" + "\r\n")
w.WriteString("N;CHARSET=UTF-8:;;" + "A"+index +";;" + "\r\n")
w.WriteString("TEL;TYPE=CELL:" + num + "\r\n")
w.WriteString("END:VCARD" + "\r\n")

}

func read() []string{
var mid []string
f, err := os.Open(inputFilePath)
if err != nil {
panic(err)
}
defer f.Close()
rd := bufio.NewReader(f)
for {
line, err := rd.ReadString('\n') //以'\n'为结束符读入一行
if err != nil || io.EOF == err {
break
}
mid = append(mid,line)
}
fmt.Println("mid size:" + strconv.Itoa(len(mid)))
fmt.Println("mid " )
fmt.Println(mid)
return mid
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK