-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfileTools.go
107 lines (98 loc) · 2.08 KB
/
fileTools.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package go_utils
import (
"bufio"
"encoding/csv"
"encoding/hex"
"fmt"
xj "github.com/hktalent/goxml2json"
"io"
"log"
"os"
"path/filepath"
"strings"
)
func Xml2Map(i io.Reader) *map[string]interface{} {
if j1, err := xj.Convert(i); nil == err {
var m1 = map[string]interface{}{}
if err := Json.Unmarshal(j1.Bytes(), &m1); nil == err {
return &m1
}
}
return nil
}
// 处理目录遍历
func DoDirs(szDir string, doFile func(s string)) {
filepath.WalkDir(szDir, func(s string, d os.DirEntry, e error) error {
doFile(s)
return nil
})
}
// 读取多个文件,按行返回
func ReadFile4Line(a ...string) chan *string {
var out = make(chan *string)
go func() {
defer close(out)
for _, x := range a {
if FileExists(x) {
if fs, err := os.OpenFile(x, os.O_RDONLY, os.ModePerm); nil == err {
scanner := bufio.NewScanner(fs)
scanner.Buffer(make([]byte, MacLineSize), MacLineSize)
for scanner.Scan() {
value := strings.TrimSpace(scanner.Text())
out <- &value
}
}
}
}
}()
return out
}
// 文件转 16进制字符串
func File2HexStr(s string) string {
if data, err := os.ReadFile(s); nil == err {
return fmt.Sprintf("%x", data)
}
return ""
}
// 16进制 字符串 转byte
func HexStr2Bytes(s string) []byte {
if data, err := hex.DecodeString(s); nil == err {
return data
}
return nil
}
// 追加到文件中
func AppendCsvFile(szFile string, a []string, f1 *os.File) *os.File {
if nil == f1 {
f, err := os.OpenFile(szFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
return f
}
f1 = f
}
//defer f.Close()
w := csv.NewWriter(f1)
if err := w.Write(a); nil != err {
log.Println(err)
}
w.Flush()
return f1
}
func AppendFile(szFile string, data []byte, f1 *os.File) *os.File {
if nil == f1 {
f, err := os.OpenFile(szFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
return f
}
f1 = f
}
//defer f.Close()
buf := bufio.NewWriter(f1)
if n, err := buf.Write(data); nil != err || n != len(data) {
log.Println(err)
}
buf.Flush()
return f1
}