代码拉取完成,页面将自动刷新
/**
* @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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。