woj-server/pkg/utils/file.go

40 lines
763 B
Go
Raw Normal View History

2022-09-07 23:34:37 +08:00
package utils
import (
"io"
"os"
"path/filepath"
2022-09-07 23:34:37 +08:00
)
func FileRead(filePath string) ([]byte, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
return io.ReadAll(f)
}
func FileWrite(filePath string, content []byte) error {
return os.WriteFile(filePath, content, 0644)
}
func FileExist(filePath string) bool {
_, err := os.Stat(filePath)
2022-10-30 22:40:59 +08:00
return If(err == nil || os.IsExist(err), true, false)
2022-09-07 23:34:37 +08:00
}
func FileEmpty(filePath string) bool {
stat, err := os.Stat(filePath)
if err != nil {
return true
}
return stat.Size() == 0
}
2022-09-07 23:34:37 +08:00
func FileTouch(filePath string) bool {
base := filepath.Dir(filePath)
_ = os.MkdirAll(base, 0755)
2022-09-07 23:34:37 +08:00
_, err := os.OpenFile(filePath, os.O_RDONLY|os.O_CREATE, 0644)
2022-10-30 22:40:59 +08:00
return If(err == nil, true, false)
2022-09-07 23:34:37 +08:00
}