1 Star 1 Fork 0

窦雪峰/go-utils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
files.go 1.14 KB
一键复制 编辑 原始数据 按行查看 历史
douxuefeng 提交于 2021-02-09 00:18 . init
/**
* @User: douxuefeng
* @Created:2021/2/6 下午2:39
* @File: files
* @Desc: 文件处理库
*/
package go_utils
import (
"io/ioutil"
"os"
)
type File struct {
}
var NewFile = newFile()
func newFile() *File {
return &File{}
}
// 判断所给路径文件/文件夹是否存在(返回true是存在)
func (f *File)IsExist(path string) bool {
_, err := os.Stat(path) // os.Stat获取文件信息
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}
// 调用os.MkdirAll递归创建文件夹
func (f *File)CreateFile(filePath string) error {
if !f.IsExist(filePath) {
err := os.MkdirAll(filePath, os.ModePerm)
return err
}
return nil
}
// 将数据写入文件中,如果文件已存在,覆盖
func (f *File) WriteFile(filename string, data []byte, perm os.FileMode) error {
return ioutil.WriteFile(filename, data, perm)
}
// 向文件追加内容
func (f *File) AppendFile(filename string, data []byte) error {
file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer file.Close()
if _, err = file.Write(data); err != nil {
return err
}
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/douxuefeng/go-utils.git
git@gitee.com:douxuefeng/go-utils.git
douxuefeng
go-utils
go-utils
v0.1.3

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385