fileutil

package
v0.0.0-...-d1e1ff0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 11, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package fileutil implements some basic functions for file operations

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClearFile

func ClearFile(path string) error

ClearFile write empty string to path file. Play: https://go.dev/play/p/NRZ0ZT-G94H

Example
fname := "./test.txt"
CreateFile(fname)

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

_, err := f.WriteString("hello world")
if err != nil {
	return
}

content1, _ := ReadFileToString(fname)

err = ClearFile(fname)
if err != nil {
	return
}
content2, _ := ReadFileToString(fname)

os.Remove(fname)

fmt.Println(content1)
fmt.Println(content2)
Output:

hello world

func CopyFile

func CopyFile(srcFilePath string, dstFilePath string) error

CopyFile copy src file to dest file. Play: https://go.dev/play/p/Jg9AMJMLrJi

func CreateDir

func CreateDir(absPath string) error

CreateDir create directory in absolute path. param `absPath` like /a/, /a/b/. Play: https://go.dev/play/p/qUuCe1OGQnM

Example
pwd, _ := os.Getwd()
dirPath := pwd + "/test_xxx/"

result1 := IsExist(dirPath)

err := CreateDir(dirPath)
if err != nil {
	return
}

result2 := IsExist(dirPath)

os.Remove(dirPath)

fmt.Println(result1)
fmt.Println(result2)
Output:

false
true

func CreateFile

func CreateFile(path string) bool

CreateFile create a file in path. Play: https://go.dev/play/p/lDt8PEsTNKI

Example
fname := "./a.txt"

result1 := IsExist(fname)

CreateFile(fname)

result2 := IsExist(fname)

os.Remove(fname)

fmt.Println(result1)
fmt.Println(result2)
Output:

false
true

func CurrentPath

func CurrentPath() string

CurrentPath return current absolute path. Play: todo

func FileAppend

func FileAppend(filePath string, data []byte, force bool) (int, error)

FileAppend 追加内容到文件中

func FileExt

func FileExt(fpath string) string

Suffix get filename ext. alias of path.Ext() 获取文件的后缀, main.go 获取的后缀是.go

func FileLoopDirs

func FileLoopDirs(pathname string) ([]string, error)

FileLoopDirs 遍历目录下的所有子目录,即返回pathname下面的所有目录,目录为绝对路径

func FileLoopFileNames

func FileLoopFileNames(pathname string) ([]string, error)

FileLoopFileNames 遍历文件夹及子文件夹下的所有文件名,即返回pathname目录下所有的文件,文件名为相对路径

func FileLoopFiles

func FileLoopFiles(pathname string) ([]string, error)

FileLoopFiles 遍历文件夹及子文件夹下的所有文件,即返回pathname目录下所有的文件,文件名为绝对路径

func FileLoopOneDirs

func FileLoopOneDirs(pathname string) ([]string, error)

FileLoopOneDirs 遍历目录下的所有子目录,即返回pathname下面的所有目录,目录为相对路径

func FileMode

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

FileMode return file's mode and permission. Play: https://go.dev/play/p/2l2hI42fA3p

func FileMove

func FileMove(src string, dst string) (err error)

FileMove 移动文件

func FileParentPath

func FileParentPath(filePath string) string

FileParentPath 文件父路径

func FilePathExists

func FilePathExists(path string) bool

FilePathExists 判断路径是否存在

func FileReadFirstLine

func FileReadFirstLine(filePath string) (string, error)

FileReadFirstLine 从文件中读取第一行并返回字符串数组

func FileReadPointLine

func FileReadPointLine(filePath string, line int) (string, error)

FileReadPointLine 从文件中读取指定行并返回字符串数组

func GetFilepaths

func GetFilepaths(dir string) ([]string, error)

GetFilepaths get all filepaths in a directory tree

func GetRootPath

func GetRootPath() string

获取项目路径

func IsAbsPath

func IsAbsPath(aPath string) bool

IsAbsPath is abs path. 是否是绝对路径

func IsDir

func IsDir(path string) bool

IsDir checks if the path is directory or not. Play: https://go.dev/play/p/WkVwEKqtOWk

Example
result1 := IsDir("./")
result2 := IsDir("./xxx.go")

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func IsExist

func IsExist(path string) bool

IsExist checks if a file or directory exists. Play: https://go.dev/play/p/nKKXt8ZQbmh

Example
result1 := IsExist("./")
result2 := IsExist("./xxx.go")

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func IsFile

func IsFile(path string) bool

IsFile reports whether the named file or directory exists. 是否是文件

func IsLink(path string) bool

IsLink checks if a file is symbol link or not. Play: https://go.dev/play/p/TL-b-Kzvf44

func ListFileNames

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

ListFileNames return all file names in the path. Play: https://go.dev/play/p/Tjd7Y07rejl

Example
fileList, _ := ListFileNames("../formatter/")
fmt.Println(fileList)
Output:

[formatter.go formatter_example_test.go formatter_test.go]

func MiMeType

func MiMeType(file any) string

MiMeType return file mime type param `file` should be string(file path) or *os.File. Play: https://go.dev/play/p/bd5sevSUZNu

func MkDir

func MkDir(path string) error

MkDir 创建文件夹,支持x/a/a 多层级

func Name

func Name(fpath string) string

Name get file/dir name 获取路径的文件名

func PathDir

func PathDir(fpath string) string

Dir get dir path, without last name. 获取路径的目录

func Prefix

func Prefix(fpath string) string

Prefix 获取文件名前缀, /tmp/main.go 获取的文件前缀是main

func ReadFile

func ReadFile(filename string) ([]byte, error)

ReadFile 读文件

func ReadFileByLine

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

ReadFileByLine read file line by line. Play: https://go.dev/play/p/svJP_7ZrBrD

Example
fname := "./test.txt"
CreateFile(fname)

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

_, err := f.WriteString("hello\nworld")
if err != nil {
	return
}

content, _ := ReadFileByLine(fname)

os.Remove(fname)

fmt.Println(content)
Output:

[hello world]

func ReadFileToString

func ReadFileToString(path string) (string, error)

ReadFileToString return string of file content. Play: https://go.dev/play/p/cmfwp_5SQTp

Example
fname := "./test.txt"
CreateFile(fname)

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

_, err := f.WriteString("hello world")
if err != nil {
	return
}

content, _ := ReadFileToString(fname)

os.Remove(fname)

fmt.Println(content)
Output:

hello world

func RecreateDir

func RecreateDir(dir string) error

RecreateDir recreate dir

func RemoveFile

func RemoveFile(path string) error

RemoveFile remove the path file. Play: https://go.dev/play/p/P2y0XW8a1SH

Example
srcFile := "./text.txt"
CreateFile(srcFile)

copyFile := "./text_copy.txt"
err := CopyFile(srcFile, copyFile)
if err != nil {
	return
}
file, err := os.Open(copyFile)
if err != nil {
	return
}
result1 := IsExist(copyFile)
result2 := file.Name()

os.Remove(srcFile)
os.Remove(copyFile)

fmt.Println(result1)
fmt.Println(result2)
Output:

true
./text_copy.txt

func Suffix

func Suffix(fpath string) string

Suffix get filename ext. alias of path.Ext() 获取文件的后缀, main.go 获取的后缀是.go

func UnZip

func UnZip(zipFile string, destPath string) error

UnZip unzip the file and save it to destPath. Play: https://go.dev/play/p/g0w34kS7B8m

Example
fname := "./test.txt"
file, _ := os.Create(fname)

_, err := file.WriteString("hello\nworld")
if err != nil {
	return
}

f, _ := os.Open(fname)
defer f.Close()

mimeType := MiMeType(f)
fmt.Println(mimeType)

os.Remove(fname)
Output:

application/octet-stream

func WriteFile

func WriteFile(filename string, data []byte, perm os.FileMode) error

WriteFile 写文件

func WriteStringToFile

func WriteStringToFile(content, path string, mode os.FileMode) (err error)

WriteStringToFile write string to file

func Zip

func Zip(fpath string, destPath string) error

Zip create zip file, fpath could be a single file or a directory. Play: https://go.dev/play/p/j-3sWBp8ik_P

Example
srcFile := "./test.txt"
CreateFile(srcFile)

zipFile := "./test.zip"
err := Zip(srcFile, zipFile)
if err != nil {
	return
}

result := IsExist(zipFile)

os.Remove(srcFile)
os.Remove(zipFile)

fmt.Println(result)
Output:

true

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL