1 Star 0 Fork 0

yjhi / go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
jfile.go 3.09 KB
一键复制 编辑 原始数据 按行查看 历史
yjhi 提交于 2023-09-07 22:53 . v1.1.1
/****************************************************************************
MIT License
Copyright (c) 2022 yjhi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*****************************************************************************/
package jutils
import (
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
)
/*
*
* add by yjh 211124
* check fiel esists
*/
func FileIsExists(filepath string) bool {
_, err := os.Lstat(filepath)
return !os.IsNotExist(err)
}
/*
*
* add by yjh 211124
* read file content
*/
func ReadFileAll(path string) (string, error) {
if !FileIsExists(path) {
return "", errors.New("file not found")
}
f, err := os.Open(path)
if err != nil {
s := fmt.Sprintf("read file err:%s", err.Error())
return "", errors.New(s)
}
defer f.Close()
fd, err := ioutil.ReadAll(f)
if err != nil {
s := fmt.Sprintf("read file content err:%s", err.Error())
return "", errors.New(s)
}
return string(fd), nil
}
/*
*
* add by yjh 211124
* make dir
*/
func MkDir(path string) {
if _, err := os.Stat(path); os.IsNotExist(err) {
os.Mkdir(path, 0777)
os.Chmod(path, 0777)
}
}
func MakeAbsPath(path string, dir string) {
if len(dir) >= 1 {
dirs := strings.TrimPrefix(dir, "/")
dirs = strings.TrimSuffix(dirs, "/")
if !strings.Contains(dirs, "/") {
MkDir(path + dir)
return
}
dirList := strings.Split(dirs, "/")
rootPath := path
for _, item := range dirList {
if len(item) > 0 {
rootPath = rootPath + "/" + item
MkDir(rootPath)
}
}
}
}
//ParseFileName
func ParseFileName(bfile string) (string, string) {
fpath := ""
fname := bfile
if strings.Contains(bfile, "/") {
fs := strings.Split(bfile, "/")
flen := len(fs)
if flen > 0 {
fname = fs[flen-1]
fpath = strings.Join(fs[0:flen-1], "/")
}
}
return fpath, fname
}
func WriteToFile(file, err string) {
f, e := os.OpenFile(file, os.O_CREATE|os.O_WRONLY, 0644)
if e != nil {
fmt.Println(err)
} else {
f.WriteString(err)
f.Close()
}
}
func AppendToFile(file, err string) {
f, e := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if e != nil {
fmt.Println(err)
} else {
f.WriteString(err)
f.Close()
}
}
Go
1
https://gitee.com/yjhi/go.git
git@gitee.com:yjhi/go.git
yjhi
go
go
daa74e60d5f8

搜索帮助

53164aa7 5694891 3bd8fe86 5694891