Files
engine/util/util.go
2020-02-27 20:48:04 +08:00

41 lines
687 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package util
import (
"bufio"
"io"
"os"
)
// 检查文件或目录是否存在
// 如果由 filename 指定的文件或目录存在则返回 true否则返回 false
func Exist(filename string) bool {
_, err := os.Stat(filename)
return err == nil || os.IsExist(err)
}
func ReadFileLines(filename string) (lines []string, err error) {
file, err := os.OpenFile(filename, os.O_RDONLY, 0644)
if err != nil {
return
}
defer file.Close()
bio := bufio.NewReader(file)
for {
var line []byte
line, _, err = bio.ReadLine()
if err != nil {
if err == io.EOF {
file.Close()
return lines, nil
}
return
}
lines = append(lines, string(line))
}
return
}