数据 输入输出 处理
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

53 lines
1008 B

package utils
import (
"fmt"
"log"
"os"
)
func SaveCache2File(cacheStr string, fileName string) error {
// 打开文件,如果文件不存在则创建
file, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
log.Println("Error opening file:", err)
return err
}
defer file.Close()
// 将变量写入文件
_, err = file.WriteString(cacheStr)
if err != nil {
log.Println("Error writing to file:", err)
}
return err
}
func ReadCache2File(fileName string) (string, error) {
// 打开文件
file, err := os.Open(fileName)
if err != nil {
log.Println("Error opening file:", err)
return "", err
}
defer file.Close()
// 读取文件内容
var content string
_, err = fmt.Fscanf(file, "%s", &content)
if err != nil {
log.Println("Error reading from file:", err)
}
return content, err
}
func FileExists(filePath string) bool {
_, err := os.Stat(filePath)
if err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}