文件/目录操作
Mkdir
- 说明:创建文件夹,支持递归创建(建议采用绝对路径),创建后的文件夹权限为:
drwxr-xr-x
。 - 格式:
func Mkdir(path string) error
func ExampleMkdir() {
// init
var (
path = gfile.TempDir("gfile_example_basic_dir")
)
// Creates directory
gfile.Mkdir(path)
// Check if directory exists
fmt.Println(gfile.IsDir(path))
// Output:
// true
}
Create
- 说明:创建文件/文件夹,如果传入的路径中的文件夹不存在,则会自动创建文件夹以及文件,其中创建的文件权限为
-rw-r–r–
。 - 注意:如果需要创建文件的已存在,则会清空该文件的内容!
- 格式:
func Create(path string) (*os.File, error)
func ExampleCreate() {
// init
var (
path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
dataByte = make([]byte, 50)
)
// Check whether the file exists
isFile := gfile.IsFile(path)
fmt.Println(isFile)
// Creates file with given `path` recursively
fileHandle, _ := gfile.Create(path)
defer fileHandle.Close()
// Write some content to file
n, _ := fileHandle.WriteString("hello goframe")
// Check whether the file exists
isFile = gfile.IsFile(path)
fmt.Println(isFile)
// Reset file uintptr
unix.Seek(int(fileHandle.Fd()), 0, 0)
// Reads len(b) bytes from the File
fileHandle.Read(dataByte)
fmt.Println(string(dataByte[:n]))
// Output:
// false
// true
// hello goframe
}
Open
func Open(path string) (*os.File, error)
func ExampleOpen() {
// init
var (
path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
dataByte = make([]byte, 4096)
)
// Open file or directory with READONLY model
file, _ := gfile.Open(path)
defer file.Close()
// Read data
n, _ := file.Read(dataByte)
fmt.Println(string(dataByte[:n]))
// Output:
// hello goframe
}
OpenFile
- 说明:以指定`flag`以及`perm`的方式打开文件/文件夹。
- 格式:
func OpenFile(path string, flag int, perm os.FileMode) (*os.File, error)
func ExampleOpenFile() {
// init
var (
path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
dataByte = make([]byte, 4096)
)
// Opens file/directory with custom `flag` and `perm`
// Create if file does not exist,it is created in a readable and writable mode,prem 0777
openFile, _ := gfile.OpenFile(path, os.O_CREATE|os.O_RDWR, gfile.DefaultPermCopy)
defer openFile.Close()
// Write some content to file
writeLength, _ := openFile.WriteString("hello goframe test open file")
fmt.Println(writeLength)
// Read data
unix.Seek(int(openFile.Fd()), 0, 0)
n, _ := openFile.Read(dataByte)
fmt.Println(string(dataByte[:n]))
// Output:
// 28
// hello goframe test open file
}
OpenWithFalg
- 说明:以指定`flag`的方式打开文件/文件夹。
- 格式:
func OpenWithFlag(path string, flag int) (*os.File, error)
func ExampleOpenWithFlag() {
// init
var (
path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
dataByte = make([]byte, 4096)
)
// Opens file/directory with custom `flag`
// Create if file does not exist,it is created in a readable and writable mode with default `perm` is 0666
openFile, _ := gfile.OpenWithFlag(path, os.O_CREATE|os.O_RDWR)
defer openFile.Close()
// Write some content to file
writeLength, _ := openFile.WriteString("hello goframe test open file with flag")
fmt.Println(writeLength)
// Read data
unix.Seek(int(openFile.Fd()), 0, 0)
n, _ := openFile.Read(dataByte)
fmt.Println(string(dataByte[:n]))
// Output:
// 38
// hello goframe test open file with flag
}
OpenWithFalgPerm
- 说明:以指定`flag`以及`perm`的方式打开文件/文件夹。
- 格式:
func OpenWithFlagPerm(path string, flag int, perm os.FileMode) (*os.File, error)
func ExampleOpenWithFlagPerm() {
// init
var (
path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
dataByte = make([]byte, 4096)
)
// Opens file/directory with custom `flag` and `perm`
// Create if file does not exist,it is created in a readable and writable mode with `perm` is 0777
openFile, _ := gfile.OpenWithFlagPerm(path, os.O_CREATE|os.O_RDWR, gfile.DefaultPermCopy)
defer openFile.Close()
// Write some content to file
writeLength, _ := openFile.WriteString("hello goframe test open file with flag and perm")
fmt.Println(writeLength)
// Read data
unix.Seek(int(openFile.Fd()), 0, 0)
n, _ := openFile.Read(dataByte)
fmt.Println(string(dataByte[:n]))
// Output:
// 38
// hello goframe test open file with flag
}
Stat
func Stat(path string) (os.FileInfo, error)
func ExampleStat() {
// init
var (
path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
)
// Get a FileInfo describing the named file.
stat, _ := gfile.Stat(path)
fmt.Println(stat.Name())
fmt.Println(stat.IsDir())
fmt.Println(stat.Mode())
fmt.Println(stat.ModTime())
fmt.Println(stat.Size())
fmt.Println(stat.Sys())
// May Output:
// file1
// false
// -rwxr-xr-x
// 2021-12-02 11:01:27.261441694 +0800 CST
// &{16777220 33261 1 8597857090 501 20 0 [0 0 0 0] {1638414088 192363490} {1638414087 261441694} {1638414087 261441694} {1638413480 485068275} 38 8 4096 0 0 0 [0 0]}
}
Copy
func Copy(src string, dst string) error
func ExampleCopy() {
// init
var (
srcFileName = "gflie_example.txt"
srcTempDir = gfile.TempDir("gfile_example_copy_src")
srcTempFile = gfile.Join(srcTempDir, srcFileName)
// copy file
dstFileName = "gflie_example_copy.txt"
dstTempFile = gfile.Join(srcTempDir, dstFileName)
// copy dir
dstTempDir = gfile.TempDir("gfile_example_copy_dst")
)
// write contents
gfile.PutContents(srcTempFile, "goframe example copy")
// copy file
gfile.Copy(srcTempFile, dstTempFile)
// read contents after copy file
fmt.Println(gfile.GetContents(dstTempFile))
// copy dir
gfile.Copy(srcTempDir, dstTempDir)
// list copy dir file
fList, _ := gfile.ScanDir(dstTempDir, "*", false)
for _, v := range fList {
fmt.Println(gfile.Basename(v))
}
// Output:
// goframe example copy
// gflie_example.txt
// gflie_example_copy.txt
}
CopyFile
func CopyFile(src, dst string) (err error)
func ExampleCopyFile() {
// init
var (
srcFileName = "gflie_example.txt"
srcTempDir = gfile.TempDir("gfile_example_copy_src")
srcTempFile = gfile.Join(srcTempDir, srcFileName)
// copy file
dstFileName = "gflie_example_copy.txt"
dstTempFile = gfile.Join(srcTempDir, dstFileName)
)
// write contents
gfile.PutContents(srcTempFile, "goframe example copy")
// copy file
gfile.CopyFile(srcTempFile, dstTempFile)
// read contents after copy file
fmt.Println(gfile.GetContents(dstTempFile))
// Output:
// goframe example copy
}
CopyDir
func CopyDir(src string, dst string) error
func ExampleCopyDir() {
// init
var (
srcTempDir = gfile.TempDir("gfile_example_copy_src")
// copy file
dstFileName = "gflie_example_copy.txt"
dstTempFile = gfile.Join(srcTempDir, dstFileName)
// copy dir
dstTempDir = gfile.TempDir("gfile_example_copy_dst")
)
// read contents after copy file
fmt.Println(gfile.GetContents(dstTempFile))
// copy dir
gfile.CopyDir(srcTempDir, dstTempDir)
// list copy dir file
fList, _ := gfile.ScanDir(dstTempDir, "*", false)
for _, v := range fList {
fmt.Println(gfile.Basename(v))
}
// Output:
// gflie_example.txt
// gflie_example_copy.txt
}
Move
- 说明:将
src
重命名为dst
。 - 注意:如果
dst
已经存在并且是文件,将会被替换造成数据丢失! - 格式:
func Move(src string, dst string) error
func ExampleMove() {
// init
var (
srcPath = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
dstPath = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file2")
)
// Check is file
fmt.Println(gfile.IsFile(dstPath))
// Moves `src` to `dst` path.
// If `dst` already exists and is not a directory, it'll be replaced.
gfile.Move(srcPath, dstPath)
fmt.Println(gfile.IsFile(srcPath))
fmt.Println(gfile.IsFile(dstPath))
// Output:
// false
// false
// true
}
Rename
- 说明:
Move
的别名,将src
重命名为dst
。 - 注意:如果
dst
已经存在并且是文件,将会被替换造成数据丢失! - 格式:
func Rename(src string, dst string) error
func ExampleRename() {
// init
var (
srcPath = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file2")
dstPath = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
)
// Check is file
fmt.Println(gfile.IsFile(dstPath))
// renames (moves) `src` to `dst` path.
// If `dst` already exists and is not a directory, it'll be replaced.
gfile.Rename(srcPath, dstPath)
fmt.Println(gfile.IsFile(srcPath))
fmt.Println(gfile.IsFile(dstPath))
// Output:
// false
// false
// true
}
Remove
func Remove(path string) error
func ExampleRemove() {
// init
var (
path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
)
// Checks whether given `path` a file, which means it's not a directory.
fmt.Println(gfile.IsFile(path))
// deletes all file/directory with `path` parameter.
gfile.Remove(path)
// Check again
fmt.Println(gfile.IsFile(path))
// Output:
// true
// false
}
IsEmpty
- 说明:检查给定的路径,如果是文件夹则检查是否包含文件,如果是文件则检查文件大小是否为空。
- 格式:
func IsEmpty(path string) bool
func ExampleIsEmpty() {
// init
var (
path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
)
// Check whether the `path` is empty
fmt.Println(gfile.IsEmpty(path))
// Truncate file
gfile.Truncate(path, 0)
// Check whether the `path` is empty
fmt.Println(gfile.IsEmpty(path))
// Output:
// false
// true
}
DirNames
- 说明:获取给定路径下的文件列表,返回的是一个切片。
- 格式:
func DirNames(path string) ([]string, error)
func ExampleDirNames() {
// init
var (
path = gfile.TempDir("gfile_example_basic_dir")
)
// Get sub-file names of given directory `path`.
dirNames, _ := gfile.DirNames(path)
fmt.Println(dirNames)
// May Output:
// [file1]
}
Glob
- 说明:模糊搜索给定路径下的文件列表,支持正则,第二个参数控制返回的结果是否带上绝对路径。
- 格式:
func Glob(pattern string, onlyNames ...bool) ([]string, error)
func ExampleGlob() {
// init
var (
path = gfile.Pwd() + gfile.Separator + "*_example_basic_test.go"
)
// Get sub-file names of given directory `path`.
// Only show file name
matchNames, _ := gfile.Glob(path, true)
fmt.Println(matchNames)
// Show full path of the file
matchNames, _ = gfile.Glob(path, false)
fmt.Println(matchNames)
// May Output:
// [gfile_z_example_basic_test.go]
// [xxx/gf/os/gfile/gfile_z_example_basic_test.go]
}
Exists
func Exists(path string) bool
func ExampleExists() {
// init
var (
path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
)
// Checks whether given `path` exist.
fmt.Println(gfile.Exists(path))
// Output:
// true
}
Chdir
- 说明:使用给定的路径,更改当前的工作路径。
- 格式:
func Chdir(dir string) error
func ExampleChdir() {
// init
var (
path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
)
// Get current working directory
fmt.Println(gfile.Pwd())
// Changes the current working directory to the named directory.
gfile.Chdir(path)
// Get current working directory
fmt.Println(gfile.Pwd())
// May Output:
// xxx/gf/os/gfile
// /tmp/gfile_example_basic_dir/file1
}