-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
67 lines (57 loc) · 1.22 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
package file
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
)
// SelfPath gets compiled executable file absolute path
func SelfPath() string {
path, _ := filepath.Abs(os.Args[0])
return path
}
// SelfDir gets compiled executable file directory
func SelfDir() string {
return filepath.Dir(SelfPath())
}
// FileExists reports whether the named file or directory exists.
func IsExists(name string) bool {
_, err := os.Stat(name)
return err == nil || os.IsExist(err)
}
func WriteStringsToFile(data []string, fileName string) error {
f, err := os.Create(fileName)
if err != nil {
fmt.Printf("create map file error: %v\n", err)
return err
}
defer f.Close()
w := bufio.NewWriter(f)
for _, v := range data {
_, _ = fmt.Fprintln(w, v)
}
return w.Flush()
}
func ReadByteFromFile(filename string) ([]byte, error) {
return ioutil.ReadFile(filename)
}
func ReadLinesFromFile(filename string) ([]string, error) {
var output []string
fi, err := os.Open(filename)
if err != nil {
fmt.Printf("Error: %s\n", err)
return output, err
}
defer fi.Close()
br := bufio.NewReader(fi)
for {
a, _, c := br.ReadLine()
if c == io.EOF {
break
}
output = append(output, string(a))
}
return output, nil
}