-
Notifications
You must be signed in to change notification settings - Fork 2
/
file_util.go
135 lines (115 loc) · 2.63 KB
/
file_util.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package ctk
import (
"bufio"
"fmt"
"io"
"log"
"os"
"strings"
)
// ReadFileLines filter and map
func ReadFileLines(filename string,
filterFunc func(string) bool,
mapFunc func(string) interface{}) []interface{} {
file, err := os.OpenFile(filename, os.O_RDONLY, 0666)
if err != nil {
log.Println("Open file error!", err)
return nil
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
panic(err)
}
if stat.Size() == 0 {
log.Printf("file:%s is empty", filename)
return nil
}
var result []interface{}
buf := bufio.NewReader(file)
for {
line, err := buf.ReadString('\n')
if err != nil {
if err == io.EOF {
break
} else {
log.Println("Read file error!", err)
return nil
}
}
if filterFunc != nil && !filterFunc(line) {
continue
}
if mapFunc != nil {
val := mapFunc(line)
if val == nil {
continue
}
result = append(result, val)
} else {
if line == "" {
continue
}
result = append(result, line)
}
}
return result
}
// ReadFileLinesNoFilter only map
func ReadFileLinesNoFilter(filename string, mapFunc func(string) interface{}) []interface{} {
return ReadFileLines(filename, nil, mapFunc)
}
func ReadCsvFile(filename string) [][]string {
lines := ReadFileLines(filename, nil, func(s string) interface{} {
return strings.Split(strings.ReplaceAll(strings.TrimSpace(s), "\"", ""), ",")
})
var result [][]string
for _, line := range lines {
result = append(result, line.([]string))
}
return result
}
// IsFileExist relative or absolute path
func IsFileExist(path string) bool {
_, err := os.Stat(path)
return err == nil
}
type BufferWriter struct {
file *os.File
writer *bufio.Writer
}
func NewWriterIgnoreError(filePath string, truncate bool) *BufferWriter {
writer, err := NewWriter(filePath, truncate)
if err != nil {
fmt.Println(err)
}
return writer
}
func NewWriter(filePath string, truncate bool) (*BufferWriter, error) {
var fileMod int
if truncate {
fileMod = os.O_WRONLY | os.O_TRUNC | os.O_CREATE
} else {
fileMod = os.O_WRONLY | os.O_APPEND | os.O_CREATE
}
file, err := os.OpenFile(filePath, fileMod, 0666)
if err != nil {
return nil, err
}
fmt.Println(file.Name())
buffer := bufio.NewWriter(file)
return &BufferWriter{file: file, writer: buffer}, nil
}
func (w *BufferWriter) Write(val []byte) (nn int, err error) {
return w.writer.Write(val)
}
func (w *BufferWriter) WriteString(val string) (nn int, err error) {
return w.writer.WriteString(val)
}
func (w *BufferWriter) WriteLine(val string) (nn int, err error) {
return w.writer.WriteString(val + "\n")
}
func (w *BufferWriter) Close() {
w.writer.Flush()
w.file.Close()
}