Categories: GoFrame 教程

GoFrame gfile-文件/目录操作

文件/目录操作

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, itll 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, itll 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 its 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
}

冒牌SEO

前端开发者,欢迎大家一起沟通和交流。

Share
Published by
冒牌SEO

Recent Posts

vue:页面注入js修改input值

一般会直接这样写: let z…

3 小时 ago

聊聊vue3中的defineProps

在Vue 3中,defineP…

1 周 ago

在 Chrome 中删除、允许和管理 Cookie

您可以选择删除现有 Cooki…

2 周 ago

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

3 周 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

4 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

1 月 ago