-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileOutput.go
109 lines (95 loc) · 2.72 KB
/
fileOutput.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
package IO
import (
"bufio"
"os"
)
/*
flag -rwxrwxrwx
os flag info list:
os.O_CREATE: create if none exists 不存在则创建
os.O_RDONLY: read-only 只读
os.O_WRONLY: write-only 只写
os.O_RDWR: read-write 可读可写
os.O_TRUNC: truncate when opened 文件长度截为0:即清空文件
os.O_APPEND: append 追加新数据到文件
*/
func BufferIOOutputByte(path string, flag int, perm os.FileMode, data []byte) error {
file, err := os.OpenFile(path, flag, perm)
defer file.Close()
if nil != err {
return err
}
writer := bufio.NewWriter(file)
_, err = writer.Write(data)
if nil != err {
return err
}
err = writer.Flush()
return err
}
func BufferOutputByte(path string, flag int, perm os.FileMode, data []byte) error {
file, err := os.OpenFile(path, flag, perm)
defer file.Close()
if nil != err {
return err
}
_, err = file.Write(data)
if nil != err {
return err
}
err = file.Sync()
return err
}
func BufferOutputWriteByte(path string, perm os.FileMode, data []byte) error {
return BufferOutputByte(path, os.O_CREATE, perm, data)
}
func BufferOutputAppendByte(path string, perm os.FileMode, data []byte) error {
return BufferOutputByte(path, os.O_APPEND, perm, data)
}
func BufferOutputWriteOnlyByte(path string, perm os.FileMode, data []byte) error {
return BufferOutputByte(path, os.O_WRONLY, perm, data)
}
func BufferOutputClearByte(path string, perm os.FileMode) error {
// return BufferOutputByte(path, os.O_TRUNC, perm, []byte("\n"))
return os.WriteFile(path, []byte(""), perm)
}
func BufferIOOutputString(path string, flag int, perm os.FileMode, data string) error {
file, err := os.OpenFile(path, flag, perm)
defer file.Close()
if nil != err {
return err
}
writer := bufio.NewWriter(file)
_, err = writer.WriteString(data)
if nil != err {
return err
}
err = writer.Flush()
return err
}
func BufferOutputString(path string, flag int, perm os.FileMode, data string) error {
file, err := os.OpenFile(path, flag, perm)
defer file.Close()
if nil != err {
return err
}
_, err = file.WriteString(data)
if nil != err {
return err
}
err = file.Sync()
return err
}
func BufferOutputWriteString(path string, perm os.FileMode, data string) error {
return BufferOutputString(path, os.O_CREATE, perm, data)
}
func BufferOutputAppendString(path string, perm os.FileMode, data string) error {
return BufferOutputString(path, os.O_APPEND, perm, data)
}
func BufferOutputWriteOnlyString(path string, perm os.FileMode, data string) error {
return BufferOutputString(path, os.O_WRONLY, perm, data)
}
func BufferOutputClearString(path string, perm os.FileMode) error {
// return BufferOutputString(path, os.O_TRUNC, perm, "")
return os.WriteFile(path, []byte(""), perm)
}