fileKit

package
v2.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// PathSeparator 路径分隔符,Mac("/")
	PathSeparator          = string(os.PathSeparator)
	PathSeparatorRune rune = os.PathSeparator

	// PathListSeparator 路径列表分隔符,Mac(":")
	PathListSeparator          = string(os.PathListSeparator)
	PathListSeparatorRune rune = os.PathListSeparator
)

Variables

This section is empty.

Functions

func AssertExist

func AssertExist(path string) error

AssertExist

@param path 文件(或目录)的路径

func AssertExistAndIsDir

func AssertExistAndIsDir(path string) error

AssertExistAndIsDir

@return 如果path存在且是个目录,返回nil

func AssertExistAndIsFile

func AssertExistAndIsFile(path string) error

AssertExistAndIsFile

@return 如果path存在且是个文件,返回nil

func AssertNotExistOrIsDir

func AssertNotExistOrIsDir(path string) error

AssertNotExistOrIsDir

通过的情况: 不存在 || 存在但是个目录 不通过的情况: 存在但是个文件

func AssertNotExistOrIsFile

func AssertNotExistOrIsFile(path string) error

AssertNotExistOrIsFile

通过的情况: 不存在 || 存在但是个文件 不通过的情况: 存在但是个目录

func CloseOnExec

func CloseOnExec(f *os.File)

CloseOnExec makes sure closing the file on process forking.

参考: go-zero中 fs.CloseOnExec.

func CopyDir

func CopyDir(src, dest string) error

CopyDir 将 src目录的所有内容 复制到 dest目录 中.

PS: (1) src目录下如果还有目录,会递归(空目录也会复制过去); (2) 类似于Linux的 cp -r 命令.

@param src 一个已经存在的目录 @param dest 一个已经存在的目录 || 一个不存在的目录

func CopyFile

func CopyFile(src, dest string) (int64, error)

CopyFile 复制单个文件.

@param src 一个已经存在的文件 @param dest 一个已经存在的文件(会覆盖) || 一个不存在的文件 @return 第一个返回值: the number of bytes copied(单位为byte)

func CreateHardLink(src, dest string) error

CreateHardLink 创建软链接

Golang 之 文件硬连接 与 软连接: https://blog.csdn.net/icebergliu1234/article/details/109208030

@param src 源文件 @param dest 生成链接的位置

func CreateSoftLink(src, dest string) error

CreateSoftLink 创建软链接

Golang 之 文件硬连接 与 软连接: https://blog.csdn.net/icebergliu1234/article/details/109208030

@param src 源文件 @param dest 生成链接的位置

func Delete

func Delete(path string) error

Delete 删除 文件 或 目录(内部有文件或目录,也会一并删除).

@param path 文件(或目录)的路径(绝对 || 相对),可以不存在,此时将返回nil

PS: (1) 传参path可以为"": 正常执行,返回nil; (2) path对应的文件或目录不存在: 正常执行,返回nil; (3) Windows系统,如果 传参path 对应的是 一个被锁定的文件 或者 一个目录(内部有文件被锁定),将返回error(remove xxx(path): The process cannot access the file because it is being used by another process.).

func EmptyDir

func EmptyDir(dirPath string) error

EmptyDir 清空目录:删掉目录中的文件和子目录(递归),但该目录本身不会被删掉.

@param dirPath 可以不存在(此时将返回nil)

func Exist

func Exist(path string) bool

Exist 判断文件(或目录)是否存在.

@param path 绝对路径 || 相对路径

e.g. ("") => false (" ") => false

func GetDirSize

func GetDirSize(dirPath string) (int64, error)

GetDirSize 获取目录的大小(包含其内文件和目录).

参考: golang获取文件/目录(包含下面的文件)的大小: https://blog.csdn.net/n_fly/article/details/117080173

func GetFileInfo

func GetFileInfo(path string) (os.FileInfo, error)

GetFileInfo 获取文件(或目录)信息

func GetFileSize

func GetFileSize(filePath string) (int64, error)

GetFileSize 获取文件的大小.

func GetLuaContent

func GetLuaContent(path string) (string, error)

GetLuaContent

  • 读取.lua文件的内容.
  • @param path lua文件的绝对路径

func GetModificationTime

func GetModificationTime(path string) (time.Time, error)

GetModificationTime 获取文件(或目录)的修改时间

@param path 传参""将返回err(Stat : The system cannot find the path specified.)

func GetName

func GetName(nameOrPath string) string

GetName 获取文件(或目录)的名称(带后缀).

PS: (1) 文件可以不存在; (2) 不要使用 path.Base(),不然在Windows环境下有问题.

@param pathOfFile 文件名 或 路径(相对||绝对)

e.g. ("") => "." (" ") => " "(1个空格) (" ") => " "(2个空格) (".") => "." ("./") => "." ("../") => ".."

e.g.1 ("test.log") => "test.log" (""/Users/richelieu/Downloads"") => "Downloads" ("c:/a/c/aaa.doc") => "aaa.doc"

e.g.2 ("/Users/richelieu/START") => "START" ("/Users/richelieu/START/") => "START"

func GetPrefix

func GetPrefix(nameOrPath string) string

GetPrefix 文件名前缀 * PS: 文件可以不存在. 参考:https://zhuanlan.zhihu.com/p/80403583

@param pathOfFile 文件名 或 文件路径(相对||绝对)

e.g. "d:/t/test.log" => "test" "d:/t/test" => "test"

func GetSize

func GetSize(path string) (int64, error)

GetSize 获取文件(或目录)的大小.

func GetSuffix

func GetSuffix(nameOrPath string) string

GetSuffix 返回文件名后缀(带"."; 小写)

PS: 文件可以不存在.

@param pathOfFile 文件名 或 文件路径(相对||绝对)

e.g. ("d:/t/test.LOG") => ".log" ("d:/t/test") => ""

func GetSuffixWithoutDot

func GetSuffixWithoutDot(nameOrPath string) string

GetSuffixWithoutDot

@return 文件名后缀(小写;不带".")

func IsDir

func IsDir(path string) bool

IsDir

@return 如果传参path对应的文件或目录不存在,将返回false

func IsFile

func IsFile(path string) bool

IsFile

@return 如果传参path对应的文件或目录不存在,将返回false

func IsHidden

func IsHidden(path string) (bool, error)

IsHidden 文件(或目录)是否隐藏?

如何在 Go 中检测文件夹中的隐藏文件 - 跨平台方法

https://www.likecs.com/ask-919454.html#sc=1368.5

流程: (1) 获取文件名(以防传参为路径) (2) 判断文件名是否以"."开头

PS: (1) 传参path 对应的文件或目录必须存在,否则返回error.

@param 文件(或目录)的 path 或 name

func MkDirs

func MkDirs(dirPaths ...string) (err error)

MkDirs 为目录路径,创建(一级或多级)目录.

PS: (1) 如果目录已经存在,将返回nil; (2) 如果 传参dirPath 对应的是个已存在的文件,将返回error("mkdir {xxx}: not a directory").

@param dirPaths 目录路径s(相对路径 || 绝对路径)

e.g. ("i:/test/test.exe") => 路径没问题且目录不存在的情况下,会在i盘创建"test"、"test.exe"两个目录 ("i:/test1/test2/") => 路径没问题且目录不存在的情况下,会在i盘创建"test1"、"test2"两个目录

e.g.1 Mac ("") => nil(什么都不会做) ("/") => nil(什么都不会做) (".") => nil(什么都不会做) ("./") => nil(什么都不会做)

func MkParentDirs

func MkParentDirs(paths ...string) (err error)

MkParentDirs 为父路径,创建(一级或多级)目录.

@param filePaths (文件 || 目录)路径s(相对路径 || 绝对路径)

e.g. ("") => nil (".") => nil

func NewFile

func NewFile(filePath string) (*os.File, error)

NewFile 创建文件.

PS: 如果文件已经存在,会覆盖掉它.

func NewTemporaryFile

func NewTemporaryFile(dirPath, pattern string) (*os.File, error)

NewTemporaryFile 在指定目录下,生成临时文件.

@param dirPath 如果为"",临时文件将生成在 系统临时目录 内;如果为".",临时文件将生成在 当前目录 内.

e.g. pattern: "tempfile_test" => 临时文件的文件名: "tempfile_test2594316144" pattern: "tempfile_test*" => 临时文件的文件名: "tempfile_test827818253" pattern: "tempfile_test*.xyz" => 临时文件的文件名: "tempfile_test3617672388.xyz"

func NotExist

func NotExist(path string) bool

func Open

func Open(path string) (*os.File, error)

Open 以"只读权限"打开文件(或目录).

@param path 文件(或目录)的路径

PS: (1) 对于os.Open(),如果传参对应的文件不存在,将返回error. (2) os.Open() 是以"只读"权限打开.

func ReadFile

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

ReadFile 读取文件的数据.

PS: (1) ioutil.ReadFile() 比 ioutil.ReadAll() 性能好,特别是大文件; (2) 编码必须为"UTF-8"!!!

@param path 文件的路径(不能是目录的路径)

func Rename

func Rename(oldPath, newPath string) error

Rename 重命名文件(或目录)

PS: (1) 重命名的同时,也能 将该文件移动到别的目录下; (2) 重命名目录,该目录下 有没有文件或目录 不会有影响,正常能成功; (3) 重命名文件,如果 newPath 对应的是一个已经存在的文件,将覆盖那个文件(并不是加到最后面); (4) 不管 oldPath,如果 newPath 对应的是一个已经存在的目录,将返回error(e.g. rename /Users/richelieu/Downloads/1 /Users/richelieu/Downloads/2: file exists).

func SetModificationTime

func SetModificationTime(path string, t time.Time) error

SetModificationTime 修改文件(或目录)的修改时间

PS: (1) 也会同时修改文件(或目录)的访问时间; (2) 修改目录的修改时间,将不会影响该目录下的文件或目录; (3) 传参t可以晚于当前时间.

@param path 传参""将返回error(chtimes : The system cannot find the path specified.)

func WriteToFile

func WriteToFile(data []byte, filePath string) error

WriteToFile 将数据(字节流)写到文件中.

@param filePath 目标文件的路径(不存在的话,会创建一个新的文件;存在且是个文件的话,会覆盖掉旧的(并不会加到该文件的最后面))

Types

This section is empty.

Jump to

Keyboard shortcuts

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