-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileOper.go
40 lines (36 loc) · 900 Bytes
/
fileOper.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
package rushtool
import (
"io/ioutil"
"os"
)
func ReadFile(fileName string) string {
var resultReturn string
//fileName := "./logconfig.json"
if !checkFileIsExist(fileName) {
WriteFile(fileName, "0")
}
if fileObj, err := os.Open(fileName); err == nil {
defer fileObj.Close()
if contents, err := ioutil.ReadAll(fileObj); err == nil {
resultReturn = string(contents)
//fmt.Println("Use os.Open family functions and ioutil.ReadAll to read a file contents:",result)
}
}
return resultReturn
}
func WriteFile(fileName string, content string) string {
statusStr := "OK"
var d1 = []byte(content)
err2 := ioutil.WriteFile(fileName, d1, 0666) //写入文件(字节数组)
if err2 != nil {
panic(err2)
}
return statusStr
}
func checkFileIsExist(filename string) bool {
var exist = true
if _, err := os.Stat(filename); os.IsNotExist(err) {
exist = false
}
return exist
}