# Fileutil

fileutil 包支持文件基本操作。

# 源码:

# 用法:

import (
    "github.com/duke-git/lancet/v2/fileutil"
)

# 文档

# ClearFile

清空文件内容

函数签名:

func ClearFile(path string) error

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/fileutil"
)

func main() {
    err := fileutil.ClearFile("./test.txt")
    if err != nil {
        fmt.Println(err)
    }
}

# CreateFile

创建文件,创建成功返回true, 否则返回false

函数签名:

func CreateFile(path string) bool

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/fileutil"
)

func main() {
    isCreatedSucceed := fileutil.CreateFile("./test.txt")
    fmt.Println(isCreatedSucceed)
}

# CreateDir

使用绝对路径创建嵌套目录,例如/a/, /a/b/

函数签名:

func CreateDir(absPath string) error

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/fileutil"
)

func main() {
    err := fileutil.CreateDir("/a/")
    fmt.Println(err)
}

# CopyFile

拷贝文件,会覆盖原有的拷贝文件

函数签名:

func CopyFile(srcFilePath string, dstFilePath string) error

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/fileutil"
)

func main() {
    err := fileutil.CopyFile("./test.txt", "./test_copy.txt")
    if err != nil {
        fmt.Println(err)
    }
}

# FileMode

获取文件mode信息

函数签名:

func FileMode(path string) (fs.FileMode, error)

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/fileutil"
)

func main() {
    mode, err := fileutil.FileMode("./test.txt")
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(mode)
}

# MiMeType

获取文件mime类型, 'file'参数的类型必须是string或者*os.File

函数签名:

func MiMeType(file any) string

例子:

package main

import (
    "fmt"
    "os"
    "github.com/duke-git/lancet/v2/fileutil"
)

func main() {
    type1 := fileutil.MiMeType("./test.txt")
    fmt.Println(type1) //text/plain; charset=utf-8

    f, _ := os.Open("./file.go")
    type2 := fileutil.MiMeType(f)
    fmt.Println(type2) //text/plain; charset=utf-8
}

# IsExist

判断文件或目录是否存在

函数签名:

func IsExist(path string) bool

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/fileutil"
)

func main() {
    fileutil.CreateFile("./test.txt")
    isFileExist := fileutil.IsExist("./test.txt")
    fmt.Println(isFileExist) //true
}

判断文件是否是符号链接

函数签名:

func IsLink(path string) bool

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/fileutil"
)

func main() {
    isLinkFile := fileutil.IsLink("./test.txt")
    fmt.Println(isLinkFile) //false
}

# IsDir

判断目录是否存在

函数签名:

func IsDir(path string) bool

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/fileutil"
)

func main() {
    isDir := fileutil.IsDir("./")
    fmt.Println(isDir) //true

    isDir = fileutil.IsDir("./test.txt")
    fmt.Println(isDir) //false
}

# ListFileNames

返回目录下所有文件名

函数签名:

func ListFileNames(path string) ([]string, error)

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/fileutil"
)

func main() {
    fileNames, _ := fileutil.ListFileNames("./")
    fmt.Println(fileNames)
}

# RemoveFile

删除文件

函数签名:

func RemoveFile(path string) error

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/fileutil"
)

func main() {
    err := fileutil.RemoveFile("./test.txt")
    if err != nil {
        fmt.Println(err)
    }
}

# ReadFileToString

读取文件内容并返回字符串

函数签名:

func ReadFileToString(path string) (string, error)

例子:

package main

import (
    "fmt"
    "os"
    "github.com/duke-git/lancet/v2/fileutil"
)

func main() {
    path := "./test.txt"
    fileutil.CreateFile(path)

    f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
    f.WriteString("hello world")

    content, _ := fileutil.ReadFileToString(path)
    fmt.Println(content) //hello world
}

# ReadFileByLine

按行读取文件内容,返回字符串切片包含每一行

函数签名:

func ReadFileByLine(path string)([]string, error)

例子:

package main

import (
    "fmt"
    "os"
    "github.com/duke-git/lancet/v2/fileutil"
)

func main() {
    path := "./text.txt"
    fileutil.CreateFile(path)

    f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
    defer f.Close()
    f.WriteString("hello\nworld")

    contents, _ := fileutil.ReadFileByLine(path)
    fmt.Println(contents) //[]string{"hello", "world"}
}

# Zip

zip压缩文件, fpath参数可以是文件或目录

函数签名:

func Zip(fpath string, destPath string) error

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/fileutil"
)

func main() {
    err := fileutil.Zip("./test.txt", "./test.zip")
    if err != nil {
        fmt.Println(err)
    }
}

# UnZip

zip解压缩文件并保存在目录中

Signature:

func UnZip(zipFile string, destPath string) error

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/fileutil"
)

func main() {
    err := fileutil.Zip("./test.zip", "./unzip/test.txt")
    if err != nil {
        fmt.Println(err)
    }
}
最后更新时间: 2022/9/29 下午1:53:14