Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

gfile模块是对文件操作的进一步封装,提供了常用的,简易的API来操作底层文件,隐藏了复杂的底层实现细节。

使用方式

import "github.com/gogf/gf/os/gfile"

接口文档

https://godoc.org/github.com/gogf/gf/os/gfile


内容缓存

GetContentsWithCache

  • 说明:带缓存获取文件内容,可设置缓存超时,文件发生变化自动清除缓存
  • 示例:

    Code Block
    languagego
    func ExampleGetContentsWithCache() {
    	// init
    	var (
    		fileName := "123gflie_example.txt"
    		tempDir  := gfile.TempDir("gfile_example_cache")
    		tempFile := gfile.Join(tempDir, fileName)
    
    	gfile.Mkdir(fileName)
    	gfile.Create(tempFile)
    
    	// write contents
    	gfile.PutContents(tempFile, "testgoframe example contentscontent")
    
    	// read contents
    	content := gfile.GetContentsWithCache(tempFile, time.Minute) It reads the file content with cache duration of one minute,
    	// which means it reads from cache after then without any IO operations within on minute.
    	fmt.Println(content)
    
    	time.Sleep(time.Second * 1gfile.GetContentsWithCache(tempFile, time.Minute))
    
    	// readwrite new contents will clear its cache
    	content1 := gfile.GetContentsWithCache(tempFile)
    	fmt.Println(content1PutContents(tempFile, "new goframe example content")
    
    	// write new contents will clear its cache
    	gfile.PutContents(tempFile, "new test contents" There's some delay for cache clearing after file content change.
    	time.Sleep(time.Second * 1)
    
    	// read contents
    	content2 := fmt.Println(gfile.GetContentsWithCache(tempFile)
    	fmt.Println(content2)
    
    	// Output:
    	// testMay contentsOutput:
    	// goframe testexample contentscontent
    	// new goframe testexample contentscontent
    }



主目录

Home

  • 说明:获取运行用户的主目录
  • 示例:

    Code Block
    languagego
    func ExampleHome() {
    	// user's home directory
    	homePath, _ := gfile.Home()
    	fmt.Println(homePath)
    
    	// May Output:
    	// C:\Users\hailaz
    }



内容替换

ReplaceFile

  • 说明:替换指定文件的指定内容为新内容
  • 示例:

    Code Block
    languagego
    func ExampleReplaceFile() {
    	// init
    	var (
    		fileName := "123gflie_example.txt"
    		tempDir  := gfile.TempDir("gfile_example_replace")
    		tempFile := gfile.Join(tempDir, fileName)
    
    	gfile.Mkdir(tempDir)
    	gfile.Create(tempFile)
    
    	// write contents
    	gfile.PutContents(tempFile, "testgoframe example contentscontent")
    
    	// read contents
    	content := fmt.Println(gfile.GetContents(tempFile))
    
    	fmt.Println(content)
    
    // It replaces content directly by file path.
    	gfile.ReplaceFile("testcontent", "replace word", tempFile)
    
    	content1 := fmt.Println(gfile.GetContents(tempFile)
    	fmt.Println(content1)
    
    	// Output:
    	// goframe testexample contentscontent
    	// goframe example replace word contents
    }


ReplaceFileFunc

  • 说明:使用自定义函数替换指定文件内容
  • 示例:

    Code Block
    languagego
    func ExampleReplaceFileFunc() {
    	// init
    // init
    	var (
    		fileName := "123gflie_example.txt"
    		tempDir  := gfile.TempDir("gfile_example_replace")
    		tempFile := gfile.Join(tempDir, fileName)
    
    	gfile.Mkdir(tempDir)
    	gfile.Create(tempFile)
    
    	// write contents
    	gfile.PutContents(tempFile, "666goframe test contents 888 a1a2a3example 123")
    
    	// read contents
    	content := fmt.Println(gfile.GetContents(tempFile)
    	fmt.Println(content)
    
    	// replace by yourself// It replaces content directly by file path and callback function.
    	gfile.ReplaceFileFunc(func(path, content string) string {
    		// Replace with regular match
    		reg, _ := regexp.Compile(`\d{3}`)
    		return reg.ReplaceAllString(content, "[num]")
    	}, tempFile)
    
    	content1 := fmt.Println(gfile.GetContents(tempFile)
    	fmt.Println(content1)
    
    	// Output:
    	// 666goframe test contents 888 a1a2a3example 123
    	// [num] test contentsgoframe example [num] a1a2a3
    }


ReplaceDir

  • 说明:扫描指定目录,替换符合条件的文件的指定内容为新内容
  • 示例:

    Code Block
    languagego
    func ExampleReplaceDir() {
    	// init
    	var (
    		fileName := "123"gflie_example.txt"
    		tempDir  := gfile.TempDir("gfile_example_replace")
    		tempFile := gfile.Join(tempDir, fileName)
    
    	tempSubDir := gfile.Join(tempDir, "sub_dir")
    	tempSubFile := gfile.Join(tempSubDir, fileName)
    
    	gfile.Mkdir(tempSubDir)
    	gfile.Create(tempFile)
    	gfile.Create(tempSubFile, fileName)
    	)
    
    	// write contents
    	gfile.PutContents(tempFile, "test contents")
    	gfile.PutContents(tempSubFile, "test contents"goframe example content")
    
    	// read contents
    	content := fmt.Println(gfile.GetContents(tempFile))
    
    	fmt.Println(content)
    	contentSub := gfile.GetContents(tempSubFile)
    	fmt.Println(contentSub)
    
    	// It replaces content of all files under specified directory recursively.
    	gfile.ReplaceDir("testcontent", "replace word", tempDir, "123gflie_example.txt", true)
    
    	// read contents
    	content1 := gfile.GetContents(tempFile)
    	fmt.Println(content1)
    	contentSub1 := gfile.GetContents(tempSubFile)
    	fmt.Println(contentSub1)
    
    	// Output:tempFile))
    
    	// test contentsOutput:
    	// goframe testexample contentscontent
    	// replacegoframe word contents
    	// example replace word contents
    }


ReplaceDirFunc

  • 说明:扫描指定目录,使用自定义函数替换符合条件的文件的指定内容为新内容
  • 示例:

    Code Block
    languagego
    func ExampleReplaceDirFunc() {
    	// init
    	fileName := "123.txt"
    	tempDir := gfile.TempDir("gfile_example_replace")
    	tempFile := gfile.Join(tempDir, fileName)
    
    	tempSubDir :var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.JoinTempDir(tempDir, "sub_dir"gfile_example_replace")
    		tempSubFiletempFile := gfile.Join(tempSubDirtempDir, fileName)
    
    	gfile.Mkdir(tempSubDir)
    	gfile.Create(tempFile)
    	gfile.Create(tempSubFile)
    
    	// write contents
    	gfile.PutContents(tempFile, "666 test contents 888 a1a2a3")	// write contents
    	gfile.PutContents(tempSubFiletempFile, "666goframe test contents 888 a1a2a3example 123")
    
    	// read contents
    	fmt.Println(gfile.GetContents(tempFile))
    
    	content := gfile.GetContents(tempFile)
    	fmt.Println(content)
    	contentSub := gfile.GetContents(tempSubFile)
    	fmt.Println(contentSub)
    // It replaces content of all files under specified directory with custom callback function recursively.
    	gfile.ReplaceDirFunc(func(path, content string) string {
    		// Replace with regular match
    		reg, _ := regexp.Compile(`\d{3}`)
    		return reg.ReplaceAllString(content, "[num]")
    	}, tempDir, "123.txt", true)
    
    	// read contents
    	content1 := gfile.GetContents(tempFile)
    , "gflie_example.txt", true)
    
    	fmt.Println(content1)
    	contentSub1 := gfile.GetContents(tempSubFile)
    	fmt.Println(contentSub1tempFile))
    
    	// Output:
    	// 666 test contents 888 a1a2a3
    	// 666 test contents 888 a1a2a3:
    	// [num]goframe test contents [num] a1a2a3example 123
    	// [num] test contentsgoframe example [num] a1a2a3
    
    }



修改时间获取

MTime

  • 说明:获取路径修改时间
  • 示例:

    Code Block
    languagego
    func ExampleMTime() {
    	t := gfile.MTime(gfile.TempDir())
    	fmt.Println(t)
    
    	// May Output:
    	// 2021-11-02 15:18:43.901141 +0800 CST
    }


ExampleMTimestamp

  • 说明:获取路径修改时间戳(秒)
  • 示例:

    Code Block
    languagego
    func ExampleMTimestamp() {
    	t := gfile.MTimestamp(gfile.TempDir())
    	fmt.Println(t)
    
    	// May Output:
    	// 1635838398
    }


ExampleMTimestampMilli

  • 说明:获取路径修改时间戳(毫秒)
  • 示例:

    Code Block
    languagego
    func ExampleMTimestampMilli() {
    	t := gfile.MTimestampMilli(gfile.TempDir())
    	fmt.Println(t)
    
    	// May Output:
    	// 1635838529330
    }



大小获取

Size

  • 说明:获取路径大小
  • 示例:

    Code Block
    languagego
    func ExampleSize() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  := gfile.TempDir("gfile_example_size")
    		tempFile = gfile.MkdirJoin(tempDir, fileName)
    	)
    
    	size// :=write contents
    	gfile.Size(tempDirPutContents(tempFile, "0123456789")
    	fmt.Println(size(gfile.Size(tempFile))
    
    	// Output:
    	// 010
    }


SizeFormat

  • 说明:获取路径大小,并格式化
  • 示例:

    Code Block
    languagego
    func ExampleSizeFormat() {
    	tempDir : {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  = gfile.TempDir("gfile_example_size")
    		tempFile = gfile.MkdirJoin(tempDir, fileName)
    	sizeStr := gfile.SizeFormat(tempDir)
    
    	// write contents
    	gfile.PutContents(tempFile, "0123456789")
    	fmt.Println(sizeStr(gfile.SizeFormat(tempFile))
    
    	// Output:
    	// 010.00B
    }


ReadableSize

  • 说明:获取路径大小,并格式化人类易读
  • 示例:

    Code Block
    languagego
    func ExampleReadableSize() {
    	// init
    	var (
    		fileName = "gflie_example.txt"
    		tempDir  func ExampleReadableSize() {
    	tempDir := gfile.TempDir("gfile_example_size")
    		tempFile = gfile.MkdirJoin(tempDir, fileName)
    	)
    
    	sizeStr// :=write contents
    	gfile.ReadableSize(tempDirPutContents(tempFile, "01234567899876543210")
    	fmt.Println(sizeStrgfile.ReadableSize(tempFile))
    
    	// Output:
    	// 020.00B
    }


StrToSize

  • 说明:大小字符串转换为大小整形
  • 示例:

    Code Block
    languagego
    func ExampleStrToSize() {
    	size := gfile.StrToSize("100MB")
    	fmt.Println(size)
    
    	// Output:
    	// 104857600
    }


FormatSize

  • 说明:大小整形转换为大小字符串
  • 示例:

    Code Block
    languagego
    func ExampleFormatSize() {
    	sizeStr := gfile.FormatSize(104857600)
    	fmt.Println(sizeStr)
    	sizeStr0 := gfile.FormatSize(1024)
    	fmt.Println(sizeStr0)
    	sizeStr1 := gfile.FormatSize(999999999999999999)
    	fmt.Println(sizeStr1)
    
    	// Output:
    	// 100.00M
    	// 1.00K
    	// 888.18P
    }



路径排序

SortFiles

  • 说明:排序多个路径
  • 示例:

    Code Block
    languagego
    func ExampleSortFiles() {
    	files := []string{
    		"/aaa/bbb/ccc.txt",
    		"/aaa/bbb/",
    		"/aaa/",
    		"/aaa",
    		"/aaa/ccc/ddd.txt",
    		"/bbb",
    		"/0123",
    		"/ddd",
    		"/ccc",
    	}
    	sortOut := gfile.SortFiles(files)
    	fmt.Println(sortOut)
    
    	// Output:
    	// [/0123 /aaa /aaa/ /aaa/bbb/ /aaa/bbb/ccc.txt /aaa/ccc/ddd.txt /bbb /ccc /ddd]
    }


搜索

  • 说明:在指定目录(默认包含当前目录、运行目录、主函数目录;不会递归子目录)中搜索文件并返回真实路径
  • 示例:

    Code Block
    languagego
    func ExampleSearch() {
    	// init
    	var (
    		fileName := "123gflie_example.txt"
    		tempDir  := gfile.TempDir("gfile_example_search")
    		tempFile := gfile.Join(tempDir, fileName)
    	)
    	gfile.Mkdir(tempDir)
    	// write contents
    	gfile.CreatePutContents(tempFile, "goframe example content")
    
    	// search file
    	realPath, _ := gfile.Search(fileName, tempDir)
    	fmt.Println(gfile.Basename(realPath))
    
    	// Output:
    	// 123gflie_example.txt
    }



目录扫描

ScanDir

  • 说明:扫描指定目录,可扫描文件或目录,支持递归扫描
  • 示例:

    Code Block
    languagego
    func ExampleScanDir() {
    	// init
    	var (
    		fileName := "123gflie_example.txt"
    		tempDir  := gfile.TempDir("gfile_example_scan_dir")
    		tempFile := gfile.Join(tempDir, fileName)
    
    		tempSubDir  := gfile.Join(tempDir, "sub_dir")
    		tempSubFile := gfile.Join(tempSubDir, fileName)
    	)
    	gfile.Mkdir(tempSubDir)
    	// write contents
    	gfile.CreatePutContents(tempFile, "goframe example content")
    	gfile.CreatePutContents(tempSubFile, "goframe example content")
    
    	// scans directory recursively
    	list, _ := gfile.ScanDir(tempDir, "123.txt,sub_dir*", true)
    	for _, v := range list {
    		fmt.Println(gfile.Basename(v))
    	}
    
    	// Output:
    	// 123gflie_example.txt
    	// sub_dir
    	// 123gflie_example.txt
    }


ScanDirFile

  • 说明:扫描指定目录的文件,支持递归扫描
  • 示例:

    Code Block
    languagego
    func ExampleScanDirFile() {
    	// init
    	var (
    		fileName := "123gflie_example.txt"
    		tempDir  := gfile.TempDir("gfile_example_scan_dir_file")
    		tempFile := gfile.Join(tempDir, fileName)
    
    		tempSubDir : = gfile.Join(tempDir, "sub_dir")
    		tempSubFile := gfile.Join(tempSubDir, fileName)
    	)
    
    	gfile.Mkdir(tempSubDir)// write contents
    	gfile.CreatePutContents(tempFile, "goframe example content")
    	gfile.CreatePutContents(tempSubFile, "goframe example content")
    
    	// scans directory recursively exclusive of directories
    	list, _ := gfile.ScanDirFile(tempDir, "123*.txt,sub_dir", true)
    	for _, v := range list {
    		fmt.Println(gfile.Basename(v))
    	}
    
    	// Output:
    	// 123gflie_example.txt
    	// 123gflie_example.txt
    }


ScanDirFunc

  • 说明:扫描指定目录(自定义过滤方法),可扫描文件或目录,支持递归扫描
  • 示例:

    Code Block
    languagego
    func ExampleScanDirFunc() {
    	// init
    	fileName := "123.txt"
    	fileName1 :var (
    		fileName = "1234gflie_example.txt"
    		tempDir  := gfile.TempDir("gfile_example_scan_dir_1func")
    		tempFile := gfile.Join(tempDir, fileName)
    
    		tempSubDir  := gfile.Join(tempDir, "sub_dir")
    		tempSubFile := gfile.Join(tempSubDir, fileName1fileName)
    	)
    
    	gfile.Mkdir(tempSubDir)// write contents
    	gfile.CreatePutContents(tempFile, "goframe example content")
    	gfile.Create(tempSubFilePutContents(tempSubFile, "goframe example content")
    
    	// scans directory recursively
    	list, _ := gfile.ScanDirFunc(tempDir, "123.txt,1234.txt,sub_dir*", true, func(path string) string {
    		// ignores some files
    		if gfile.Basename(path) == "1234gflie_example.txt" {
    			return ""
    		}
    		return path
    	})
    	for _, v := range list {
    		fmt.Println(gfile.Basename(v))
    	}
    
    	// Output:
    	// 123.txt
    	// sub_dir
    }


ScanDirFileFunc

  • 说明:扫描指定目录的文件(自定义过滤方法),支持递归扫描
  • 示例:

    Code Block
    languagego
    func ExampleScanDirFileFunc() {
    	// init
    	var (
    		fileName := "123gflie_example.txt"
    		fileName1tempDir  := "1234.txt"
    	tempDir :gfile.TempDir("gfile_example_scan_dir_file_func")
    		tempFile = gfile.TempDir("gfileJoin(tempDir, fileName)
    
    		fileName1 = "gflie_example_1ignores.txt")
    	tempFile	tempFile1 := gfile.Join(tempDir, fileNamefileName1)
    
    		tempSubDir  := gfile.Join(tempDir, "sub_dir")
    		tempSubFile := gfile.Join(tempSubDir, fileName1fileName)
    	)
    
    	gfile.Mkdir(tempSubDir// write contents
    	gfile.PutContents(tempFile, "goframe example content")
    	gfile.Create(tempFilePutContents(tempFile1, "goframe example content")
    	gfile.CreatePutContents(tempSubFile, "goframe example content")
    
    	// scans directory recursively exclusive of directories
    	list, _ := gfile.ScanDirFileFunc(tempDir, "123.txt,1234*.txt,sub_dir", true, func(path string) string {
    		// ignores some files
    		if gfile.Basename(path) == "1234gflie_example_ignores.txt" {
    			return ""
    		}
    		return path
    	})
    	for _, v := range list {
    		fmt.Println(gfile.Basename(v))
    	}
    
    	// Output:
    	// 123gflie_example.txt
    	// gflie_example.txt
    }







Panel
titleContent Menu

Table of Contents