GoFrame gfile-文件/目錄操作

2022-04-09 13:44 更新

文件/目錄操作

Mkdir

  • 說明:創(chuàng)建文件夾,支持遞歸創(chuàng)建(建議采用絕對路徑),創(chuàng)建后的文件夾權(quán)限為:?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

  • 說明:創(chuàng)建文件/文件夾,如果傳入的路徑中的文件夾不存在,則會(huì)自動(dòng)創(chuàng)建文件夾以及文件,其中創(chuàng)建的文件權(quán)限為?-rw-r–r–?。
  • 注意:如果需要?jiǎng)?chuàng)建文件的已存在,則會(huì)清空該文件的內(nèi)容!
  • 格式: 

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

  • 說明:支持復(fù)制文件或目錄
  • 格式: 

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

  • 說明:復(fù)制文件
  • 格式: 

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

  • 說明:支持復(fù)制文件或目錄
  • 格式: 

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?已經(jīng)存在并且是文件,將會(huì)被替換造成數(shù)據(jù)丟失!
  • 格式: 

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?已經(jīng)存在并且是文件,將會(huì)被替換造成數(shù)據(jù)丟失!
  • 格式: 

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

  • 說明:獲取給定路徑下的文件列表,返回的是一個(gè)切片。
  • 格式: 

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

  • 說明:模糊搜索給定路徑下的文件列表,支持正則,第二個(gè)參數(shù)控制返回的結(jié)果是否帶上絕對路徑。
  • 格式: 

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

  • 說明:使用給定的路徑,更改當(dāng)前的工作路徑。
  • 格式: 

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
}


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號