Golang初學:文件操作,標準庫

go version go1.22.1 windows/amd64

Windows 11 + amd64

x86_64 x86_64 GNU/Linux

---

 

序章

讀取文件內容,寫入新文件(可能存在、也可能不存在)。

 

相關標準庫

  • io
  • fs
  • os
  • path
  • filepath

 

Show Code

func CopyFile() {
	// 測試文件拷貝

	var fsrc, fdst string
	var start time.Time

	fsrc = "D:/test/fs01.go"
	fdst = "D:/test/fs01.go.bak"

	start = time.Now()
	copyFromTo(fsrc, fdst)
	fmt.Println(fsrc, "耗時:", time.Now().Sub(start))

	// 第二次測試時,文件已存在
	copyFromTo(fsrc, fdst)

	fsrc = "D:/test/VTS_01_1.VOB"
	fdst = "D:/test/VTS_01_1.VOB.bak"
	start = time.Now()
	copyFromTo(fsrc, fdst)
	fmt.Println(fsrc, "耗時:", time.Now().Sub(start))

	copyFromTo(fsrc, fdst)
}


// 拷貝文件 fsrc 到 fdst
func copyFromTo(fsrc, fdst string) {
	fsrcp := openFileSrc(fsrc)
	fdstp := openFileDstForWrite(fdst)

	copyFileData(fsrcp, fdstp)

}

// 打開文件:只讀 os.Open(.)
// 錯誤時退出程序
func openFileSrc(path string) (fr *os.File) {
	fr, err := os.Open(path)
	if err != nil {
		log.Fatal(err)
	}

	return
}

// 打開文件:可讀寫
// 不存在,先新建
// 已存在,先清空
// 返回 fw
func openFileDstForWrite(path string) (fw *os.File) {
	// 這裏的 參數2、參數3 還有不同的配置
	fw, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0644)
	if err != nil {
		log.Fatal(err)
	}

	return
}

// 拷貝文件數據
func copyFileData(fsrcp, fdstp *os.File) {
	// 緩衝區:字節分片
	var buf = make([]byte, 1024)

	var rc int
	var rerr error
	var testTotal int
	for {
		if rc, rerr = fsrcp.Read(buf); rerr == nil {
			// 已讀取 rc 字節到 buf
			// 寫入 fdstp
			fdstp.Write(buf[:rc])
			testTotal += rc
		} else if rerr == io.EOF {
			log.Println("寫入文件數據成功...結束。", rerr, "testTotal=", testTotal)
			break
		} else {
			log.Fatal("寫入文件數據失敗...", rerr)
		}
	}
}

 

官文資料

std: os 文件打開 函數

// https://pkg.go.dev/[email protected]

/*
Open opens the named file for reading. 
If successful, methods on the returned file can be used for reading; 
the associated file descriptor has mode O_RDONLY. 
If there is an error, it will be of type *PathError.
*/
func Open(name string) (*File, error)

/*
OpenFile is the generalized open call; most users will use Open or Create instead.
示例:
f, err := os.OpenFile("notes.txt", os.O_RDWR|os.O_CREATE, 0644)
*/
func OpenFile(name string, flag int, perm FileMode) (*File, error)

注意,這裏的 flag、perm 參數設置很關鍵。

 

std:os 文件讀取、寫入 方法——*File 的方法

/*
Read reads up to len(b) bytes from the File and stores them in b. 
It returns the number of bytes read and any error encountered. 
At end of file, Read returns 0, io.EOF.
*/
func (f *File) Read(b []byte) (n int, err error)
// 注意,返回的 err 爲 io.EOF 也是成功,表示結束,需要 退出讀取

/*
Write writes len(b) bytes from b to the File. 
It returns the number of bytes written and an error, if any. 
Write returns a non-nil error when n != len(b).
*/
func (f *File) Write(b []byte) (n int, err error)
// 注意,參數 b 不一定是讀取 全部字節,因此,代碼裏使用了 buf[:rc]

 

測試

結果:成功。

2024/05/15 21:01:51 寫入文件數據成功...結束。 EOF testTotal= 299
D:/test/fs01.go 耗時: 20.9464ms
2024/05/15 21:01:51 寫入文件數據成功...結束。 EOF testTotal= 299
2024/05/15 21:01:52 寫入文件數據成功...結束。 EOF testTotal= 24037376
D:/test/VTS_01_1.VOB 耗時: 143.7081ms
2024/05/15 21:01:52 寫入文件數據成功...結束。 EOF testTotal= 24037376

截圖:

 

END.

ben發佈於博客園

本文鏈接:

https://www.cnblogs.com/luo630/p/18194645

ben發佈於博客園

參考資料

1、golang 相關 std

https://pkg.go.dev/std

2、

 

ben發佈於博客園

ben發佈於博客園

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章