diff --git a/docs/fileutil.md b/docs/fileutil.md index 5901210..a9cc1c9 100644 --- a/docs/fileutil.md +++ b/docs/fileutil.md @@ -43,6 +43,8 @@ import ( - [MTime](#MTime) - [Sha](#Sha) - [ReadCsvFile](#ReadCsvFile) +- [WriteStringToFile](#WriteStringToFile) +- [WriteBytesToFile](#WriteBytesToFile)
@@ -660,3 +662,101 @@ func main() { // } ``` + +### WriteBytesToFile + +

Writes bytes to target file.

+ +Signature: + +```go +func WriteBytesToFile(filepath string, content []byte) error +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/fileutil" +) + +func main() { + filepath := "./bytes.txt" + + file, err := os.Create(filepath) + if err != nil { + return + } + + defer file.Close() + + err = fileutil.WriteBytesToFile(filepath, []byte("hello")) + if err != nil { + return + } + + content, err := fileutil.ReadFileToString(filepath) + if err != nil { + return + } + + os.Remove(filepath) + + fmt.Println(content) + + // Output: + // hello +} +``` + +### WriteStringToFile + +

Writes string to target file.

+ +Signature: + +```go +func WriteStringToFile(filepath string, content string, append bool) error +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/fileutil" +) + +func main() { + filepath := "./test.txt" + + file, err := os.Create(filepath) + if err != nil { + return + } + + defer file.Close() + + err = fileutil.WriteStringToFile(filepath, "hello", true) + if err != nil { + return + } + + content, err := fileutil.ReadFileToString(filepath) + if err != nil { + return + } + + os.Remove(filepath) + + fmt.Println(content) + + // Output: + // hello +} +``` diff --git a/docs/fileutil_zh-CN.md b/docs/fileutil_zh-CN.md index 091f9b2..d9facdf 100644 --- a/docs/fileutil_zh-CN.md +++ b/docs/fileutil_zh-CN.md @@ -43,6 +43,8 @@ import ( - [MTime](#MTime) - [Sha](#Sha) - [ReadCsvFile](#ReadCsvFile) +- [WriteStringToFile](#WriteStringToFile) +- [WriteBytesToFile](#WriteBytesToFile)
@@ -660,3 +662,103 @@ func main() { // } ``` + + +### WriteBytesToFile + +

将bytes写入文件。

+ +函数签名: + +```go +func WriteBytesToFile(filepath string, content []byte) error +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/fileutil" +) + +func main() { + filepath := "./bytes.txt" + + file, err := os.Create(filepath) + if err != nil { + return + } + + defer file.Close() + + err = fileutil.WriteBytesToFile(filepath, []byte("hello")) + if err != nil { + return + } + + content, err := fileutil.ReadFileToString(filepath) + if err != nil { + return + } + + os.Remove(filepath) + + fmt.Println(content) + + // Output: + // hello +} +``` + + +### WriteStringToFile + +

将字符串写入文件。

+ +函数签名: + +```go +func WriteStringToFile(filepath string, content string, append bool) error +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/fileutil" +) + +func main() { + filepath := "./test.txt" + + file, err := os.Create(filepath) + if err != nil { + return + } + + defer file.Close() + + err = fileutil.WriteStringToFile(filepath, "hello", true) + if err != nil { + return + } + + content, err := fileutil.ReadFileToString(filepath) + if err != nil { + return + } + + os.Remove(filepath) + + fmt.Println(content) + + // Output: + // hello +} +```