Skip to content
本页内容

# Fileutil ​

fileutil 包支持文件基本操作。

## 源码: ​

- https://github.com/duke-git/lancet/blob/main/fileutil/file.go

## 用法: ​

go

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

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

## 目录 ​

- ClearFile
- CreateFile
- CreateDir
- CopyFile
- CurrentPath
- FileMode
- MiMeType
- IsExist
- IsLink
- IsDir
- ListFileNames
- RemoveFile
- ReadFileToString
- ReadFileByLine
- Zip
- ZipAppendEntry
- UnZip
- IsZipFile
- FileSize
- MTime
- Sha
- ReadCsvFile
- WriteCsvFile
- WriteStringToFile
- WriteBytesToFile

## 文档 ​

### ClearFile ​

清空文件内容

函数签名:

go

    func ClearFile(path string) error

    func ClearFile(path string) error

示例:运行

go

    package main

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

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

    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

函数签名:

go

    func CreateFile(path string) bool

    func CreateFile(path string) bool

示例:运行

go

    package main

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

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

    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

函数签名:

go

    func CreateDir(absPath string) error

    func CreateDir(absPath string) error

示例:运行

go

    package main

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

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

    package main

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

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

### CopyFile ​

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

函数签名:

go

    func CopyFile(srcPath string, dstPath string) error

    func CopyFile(srcPath string, dstPath string) error

示例:运行

go

    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)
        }
    }

    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)
        }
    }

### CurrentPath ​

返回当前位置的绝对路径。

函数签名:

go

    func CurrentPath() string

    func CurrentPath() string

示例:运行

go

    package main

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

    func main() {
        absPath := CurrentPath()
        fmt.Println(absPath)
    }

    package main

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

    func main() {
        absPath := CurrentPath()
        fmt.Println(absPath)
    }

### FileMode ​

获取文件mode信息

函数签名:

go

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

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

示例:运行

go

    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)
    }

    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

函数签名:

go

    func MiMeType(file any) string

    func MiMeType(file any) string

示例:运行

go

    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
    }

    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 ​

判断文件或目录是否存在

函数签名:

go

    func IsExist(path string) bool

    func IsExist(path string) bool

示例:运行

go

    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
    }

    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
    }

### IsLink ​

判断文件是否是符号链接

函数签名:

go

    func IsLink(path string) bool

    func IsLink(path string) bool

示例:运行

go

    package main

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

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

    package main

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

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

### IsDir ​

判断参数是否是目录

函数签名:

go

    func IsDir(path string) bool

    func IsDir(path string) bool

示例:运行

go

    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
    }

    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 ​

返回目录下所有文件名

函数签名:

go

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

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

示例:运行

go

    package main

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

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

    package main

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

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

### RemoveFile ​

删除文件

函数签名:

go

    func RemoveFile(path string) error

    func RemoveFile(path string) error

示例:运行

go

    package main

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

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

    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 ​

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

函数签名:

go

    func ReadFileToString(path string) (string, error)

    func ReadFileToString(path string) (string, error)

示例:运行

go

    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
    }

    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 ​

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

函数签名:

go

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

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

示例:运行

go

    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"}
    }

    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参数可以是文件或目录

函数签名:

go

    func Zip(fpath string, destPath string) error

    func Zip(fpath string, destPath string) error

示例:运行

go

    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)
        }
    }

    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)
        }
    }

### ZipAppendEntry ​

通过将单个文件或目录追加到现有的zip文件

函数签名:

go

    func ZipAppendEntry(fpath string, destPath string) error

    func ZipAppendEntry(fpath string, destPath string) error

示例:运行

go

    package main

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

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

    package main

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

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

### UnZip ​

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

函数签名:

go

    func UnZip(zipFile string, destPath string) error

    func UnZip(zipFile string, destPath string) error

示例:运行

go

    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)
        }
    }

    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)
        }
    }

### IsZipFile ​

判断文件是否是zip压缩文件。

函数签名:

go

    func IsZipFile(filepath string) bool

    func IsZipFile(filepath string) bool

示例:运行

go

    package main

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

    func main() {
        isZip := fileutil.IsZipFile("./zipfile.zip")
        fmt.Println(isZip)
    }

    package main

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

    func main() {
        isZip := fileutil.IsZipFile("./zipfile.zip")
        fmt.Println(isZip)
    }

### FileSize ​

返回文件字节大小。

函数签名:

go

    func FileSize(path string) (int64, error)

    func FileSize(path string) (int64, error)

示例:运行

go

    package main

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

    func main() {
        size, err := fileutil.FileSize("./testdata/test.txt")

        fmt.Println(size)
        fmt.Println(err)

        // Output:
        // 20
        // <nil>
    }

    package main

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

    func main() {
        size, err := fileutil.FileSize("./testdata/test.txt")

        fmt.Println(size)
        fmt.Println(err)

        // Output:
        // 20
        // <nil>
    }

### MTime ​

返回文件修改时间(unix timestamp).

函数签名:

go

    func MTime(filepath string) (int64, error)

    func MTime(filepath string) (int64, error)

示例:运行

go

    package main

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

    func main() {
        mtime, err := fileutil.MTime("./testdata/test.txt")

        fmt.Println(mtime)
        fmt.Println(err)

        // Output:
        // 1682391110
        // <nil>
    }

    package main

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

    func main() {
        mtime, err := fileutil.MTime("./testdata/test.txt")

        fmt.Println(mtime)
        fmt.Println(err)

        // Output:
        // 1682391110
        // <nil>
    }

### Sha ​

返回文件sha值，参数`shaType` 应传值为: 1, 256，512.

函数签名:

go

    func Sha(filepath string, shaType ...int) (string, error)

    func Sha(filepath string, shaType ...int) (string, error)

示例:运行

go

    package main

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

    func main() {
        sha1, err := fileutil.Sha("./testdata/test.txt", 1)
        sha256, _ := fileutil.Sha("./testdata/test.txt", 256)
        sha512, _ := fileutil.Sha("./testdata/test.txt", 512)

        fmt.Println(sha1)
        fmt.Println(sha256)
        fmt.Println(sha512)
        fmt.Println(err)

        // Output:
        // dda3cf10c5a6ff6c6659a497bf7261b287af2bc7
        // aa6d0a3fbc3442c228d606da09e0c1dc98c69a1cac3da1909199e0266171df35
        // d22aba2a1b7a2e2f512756255cc1c3708905646920cb1eb95e45b531ba74774dbbb89baebf1f716220eb9cf4908f1cfc5b2a01267704d9a59f59d77cab609870
        // <nil>
    }

    package main

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

    func main() {
        sha1, err := fileutil.Sha("./testdata/test.txt", 1)
        sha256, _ := fileutil.Sha("./testdata/test.txt", 256)
        sha512, _ := fileutil.Sha("./testdata/test.txt", 512)

        fmt.Println(sha1)
        fmt.Println(sha256)
        fmt.Println(sha512)
        fmt.Println(err)

        // Output:
        // dda3cf10c5a6ff6c6659a497bf7261b287af2bc7
        // aa6d0a3fbc3442c228d606da09e0c1dc98c69a1cac3da1909199e0266171df35
        // d22aba2a1b7a2e2f512756255cc1c3708905646920cb1eb95e45b531ba74774dbbb89baebf1f716220eb9cf4908f1cfc5b2a01267704d9a59f59d77cab609870
        // <nil>
    }

### ReadCsvFile ​

读取csv文件内容到切片

函数签名:

go

    func ReadCsvFile(filepath string) ([][]string, error)

    func ReadCsvFile(filepath string) ([][]string, error)

示例:运行

go

    package main

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

    func main() {
        content, err := fileutil.ReadCsvFile("./testdata/test.csv")

        fmt.Println(content)
        fmt.Println(err)

        // Output:
        // [[Bob  12  male] [Duke  14  male] [Lucy  16  female]]
        // <nil>
    }

    package main

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

    func main() {
        content, err := fileutil.ReadCsvFile("./testdata/test.csv")

        fmt.Println(content)
        fmt.Println(err)

        // Output:
        // [[Bob  12  male] [Duke  14  male] [Lucy  16  female]]
        // <nil>
    }

### WriteCsvFile ​

向csv文件写入内容。

函数签名:

go

    func WriteCsvFile(filepath string, records [][]string, append bool) error

    func WriteCsvFile(filepath string, records [][]string, append bool) error

示例:运行

go

    package main

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

    func main() {
        fpath := "./test.csv"
        fileutil.CreateFile(fpath)

        f, _ := os.OpenFile(fpath, os.O_WRONLY|os.O_TRUNC, 0777)
        defer f.Close()

        data := [][]string{
            {"Lili", "22", "female"},
            {"Jim", "21", "male"},
        }
        err := fileutil.WriteCsvFile(fpath, data, false)

        if err != nil {
            return
        }

        content, err := fileutil.ReadCsvFile(fpath)

        if err != nil {
            return
        }
        fmt.Println(content)

        // Output:
        // [[Lili 22 female] [Jim 21 male]]
    }

    package main

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

    func main() {
        fpath := "./test.csv"
        fileutil.CreateFile(fpath)

        f, _ := os.OpenFile(fpath, os.O_WRONLY|os.O_TRUNC, 0777)
        defer f.Close()

        data := [][]string{
            {"Lili", "22", "female"},
            {"Jim", "21", "male"},
        }
        err := fileutil.WriteCsvFile(fpath, data, false)

        if err != nil {
            return
        }

        content, err := fileutil.ReadCsvFile(fpath)

        if err != nil {
            return
        }
        fmt.Println(content)

        // Output:
        // [[Lili 22 female] [Jim 21 male]]
    }

### WriteBytesToFile ​

将bytes写入文件。

函数签名:

go

    func WriteBytesToFile(filepath string, content []byte) error

    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
    }

    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

    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
    }

    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
    }