forked from swxctx/gutil
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile.go
183 lines (164 loc) · 3.17 KB
/
file.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package gutil
import (
"bufio"
"encoding/csv"
"fmt"
"io"
"os"
"strings"
)
// WriteCsv 写入数据到csv文件
func WriteCsv(allData [][]string, file string) error {
// 需要写入的文件
csvWriteFile, err := os.Create(file)
if err != nil {
return err
}
defer csvWriteFile.Close()
// 防止乱码
csvWriteFile.WriteString("\xEF\xBB\xBF")
csvWriter := csv.NewWriter(csvWriteFile)
err = csvWriter.WriteAll(allData)
if err != nil {
return err
}
csvWriter.Flush()
if err := csvWriter.Error(); err != nil {
return err
}
return nil
}
// ReadCsv 读取csv文件
func ReadCsv(file string) ([][]string, error) {
var (
rows [][]string
err error
)
csvFile, err := os.Open(file)
if err != nil {
return rows, err
}
defer csvFile.Close()
csvReader := csv.NewReader(csvFile)
rows, err = csvReader.ReadAll()
if err != nil {
return rows, err
}
return rows, nil
}
// WriteMaptoFile
func WriteMaptoFile(m map[string]string, filePath string) error {
f, err := os.Create(filePath)
if err != nil {
return err
}
defer f.Close()
w := bufio.NewWriter(f)
for _, v := range m {
lineStr := fmt.Sprintf("%s", v)
fmt.Fprintln(w, lineStr)
}
return w.Flush()
}
// WriteSliceToFile
func WriteSliceToFile(arrs []string, filePath string) error {
f, err := os.Create(filePath)
if err != nil {
return err
}
defer f.Close()
w := bufio.NewWriter(f)
for _, lineStr := range arrs {
fmt.Fprintln(w, lineStr)
}
return w.Flush()
}
// ReadMapToFile
func ReadMapToFile(fileName string) map[string]string {
file, err := os.Open(fileName)
if err != nil {
panic(err)
}
defer file.Close()
var (
result map[string]string
)
result = make(map[string]string)
rd := bufio.NewReader(file)
for {
line, err := rd.ReadString('\n')
if err != nil || io.EOF == err {
break
}
line = strings.Replace(line, "\n", "", -1)
result[line] = line
}
return result
}
// DewightDat .dat文件去重处理
func DewightDat(file1, file2 string, resfile string) error {
// 样本(需要过滤掉的数据)
f1, err := os.Open(file1)
if err != nil {
return err
}
defer f1.Close()
var (
equs map[string]string
result map[string]string
)
equs = make(map[string]string)
rd := bufio.NewReader(f1)
for {
line, err := rd.ReadString('\n')
if err != nil || io.EOF == err {
break
}
line = strings.Replace(line, "\n", "", -1)
equs[line] = line
}
// 所有数据
f, err := os.Open(file2)
if err != nil {
panic(err)
}
defer f.Close()
// 所有数据
result = make(map[string]string)
rd1 := bufio.NewReader(f)
for {
line, err := rd1.ReadString('\n')
if err != nil || io.EOF == err {
break
}
line = strings.Replace(line, "\n", "", -1)
// 没有在样本里面
if _, ok := equs[line]; !ok {
result[line] = line
}
}
err = WriteMaptoFile(result, resfile)
if err != nil {
return err
}
return nil
}
// CreateFile
func CreateFile(fileName string) error {
_, err := os.Create(fileName)
return err
}
// FileIsExist 判断文件或文件夹是否存在
func FileIsExist(path string) bool {
_, err := os.Stat(path)
if err != nil {
if os.IsExist(err) {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}
return true
}