13518219792

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

创新互联GoFrame教程:GoFramegfile-文件/目录操作

文件/目录操作

Mkdir

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

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

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

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

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

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

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
}

网页标题:创新互联GoFrame教程:GoFramegfile-文件/目录操作
转载来源:http://cdbrznjsb.com/article/djojcpd.html

其他资讯

让你的专属顾问为你服务