Go語言學習、文件操作

今天我們來說一下在go語言中,如何對本地文件進行操作。

一、創建文件

我們在將數據存儲到文件之前,首先要在磁盤中創建文件。

Create()函數在GO語言中,提供我們創建文件使用。我們現在講的都是基於文本文件的存儲。

除了文本文件外,計算機中還有一種存儲格式叫二進制文件,如:音頻、視頻、圖片等等。

創建文件的過程:

1、我們在創建文件之前,首先要導入“os” 包。創建文件的函數都在該包中。

2、指定創建文件存放路徑以及文件名稱。

3、執行Create()函數,進行文件的創建

4、關閉文件操作

package main

import (
	"fmt"
	"os"
)

func main() {
	//路徑又分爲 相對路徑 絕對路徑
	//絕對路徑: windows系統中,從盤符開始到文件的位置如:(D:\go\文件操作)
	// linux 系統中是以根目錄/開始的。
	//絕對路徑 相對某一個文件的位置。以當前文件所在目錄開始。如(./)表示當前該文件所在的目錄../爲上級目錄
	// \\ 是一個轉義字符 [\] 在一般文件操作中都是以 [/]
	file,error:=os.Create("D:/go_test/file/a.txt")
	//error 中如果有值
	if error != nil{
		fmt.Println(error)
		fmt.Println("文件創建失敗")
		return //如果return出現在主函數中,表示程序的結束。
	}
	//defer 延遲調用,關閉文件、
	//文件如果不關閉影響:
	//1、佔用內存和緩衝區
	//2、文件的打開上線爲 65535
	defer file.Close()

	fmt.Println("文件創建成功")


}

結果:

文件創建成功

在相應的目錄位置,創建出a.txt文件

上面代碼中,file 是一個文件指針 相當於 var file * File

在文件操作中,使用so.Create()函數創建文件時,如果文件不存在,會創建一個新文件。

如果文件已經存在,那麼會覆蓋創建,覆蓋掉原來的內容。

二、寫入文件

文件的寫入,WriteString( )方法、

package main

import (
	"fmt"
	"os"
)

func main() {
	file,error:=os.Create("D:/go_test/file/a.txt")
	//error 中如果有值
	if error != nil{
		fmt.Println(error)
		fmt.Println("文件創建失敗")
		return
	}
	defer file.Close()
	//寫入文件
	//在windows 中,寫入文本文件換行爲 \r\n
	//在linux 中,寫入文本文件換行爲 \n
	n,_:=file.WriteString("寫入文件測試\r\n")
	//n返回值,返回寫入個數
	fmt.Println(n)

	m,_=file.WriteString("第二行寫入測試")
	fmt.Println(m)
}

結果:

20
21

在go語言中,一個漢字,是3個字符。

寫入文件的第二種方式:

package main

import (
	"fmt"
	"os"
)

func main() {
	file,error:=os.Create("D:/go_test/file/a.txt")
	//error 中如果有值
	if error != nil{
		fmt.Println(error)
		fmt.Println("文件創建失敗")
		return
	}else{
		fmt.Println("創建文件成功!")
	}
	defer file.Close()
	b:=[]byte{'1','2','3','4'}
	n,_:=file.Write(b)
	fmt.Println(n)
}

結果:

寫入文件成功!
4

上面這種方式,是使用文件對象.write(字符切片) 的方式寫入文件,可以看到這種方式有些麻煩 。

做一下簡單的修改:

package main

import (
	"fmt"
	"os"
)

func main() {
	file,error:=os.Create("D:/go_test/file/a.txt")
	//error 中如果有值
	if error != nil{
		fmt.Println(error)
		fmt.Println("文件創建失敗")
		return
	}else{
		fmt.Println("創建文件成功!")
	}
	defer file.Close()
	//b:=[]byte{'1','2','3','4'}
	str:="1234"
	b:=[]byte(str)
	n,_:=file.Write(b)
	fmt.Println(n)
}

結果:

寫入文件成功!
4

可以看到,我們做了小改動以後,跟上面那種寫入方式一樣。

這裏說明一下,在go語言中,string 字符串和字符切片允許轉換。

第三種文件,寫入方式:

package main

import (
	"fmt"
	"os"
)

func main() {

	//os.OpenFile(文件路徑名稱,打開模式,打開權限)
	//OpenFile 不能創建新文件
	file,error:=os.OpenFile("D:/go_test/file/a.txt", os.O_RDWR,6)
	if error != nil{
		fmt.Println(error)
		fmt.Println("文件創建失敗")
		return
	}else{
		fmt.Println("創建文件成功!")
	}
	defer file.Close()

	b:=[]byte("OpenFile文件測試")
	//當使用WriteAt()進行指定位置,寫入文件時,會覆蓋文件內容。
	file.WriteAt(b,0)
}

 

當我們出入文件時,還可以獲取,文件內容的位置信息:

package main

import (
	"fmt"
	"io"
	"os"
)

func main() {

	//os.OpenFile(文件路徑名稱,打開模式,打開權限)
	//OpenFile 不能創建新文件
	file,error:=os.OpenFile("D:/go_test/file/a.txt", os.O_RDWR,6)
	if error != nil{
		fmt.Println(error)
		fmt.Println("文件創建失敗")
		return
	}else{
		fmt.Println("打開文件成功!")
	}
	defer file.Close()

	//獲取文件字符個數信息,從開始到末尾
	n,_:=file.Seek(0, io.SeekEnd)
	fmt.Println(n)

	//b:=[]byte("OpenFile文件測試")
	//當使用WriteAt()進行指定位置,寫入文件時,會覆蓋文件內容。
	//file.WriteAt(b,0)
}

結果:得到文件中的字符個數信息

20

然後使用wirteAt(插入內容,插入字符位置),來進行文件內容寫入操作。可以寫入在任意位置內。

三、讀取文件

讀取文件過程:

1、打開要讀取的文件

2、對文件進行讀取

3、關閉文件

package main

import (
	"fmt"
	"os"
)

func main() {

	//os.Open(文件路徑名稱),以只讀方式打開文件
	file,error:=os.Open("D:/go_test/file/a.txt")
	if error != nil{
		fmt.Println(error)
		fmt.Println("打開文件失敗")
		return
	}else{
		fmt.Println("打開文件成功!")
	}
	defer file.Close()

	b:=make([]byte,1024)
	//讀取文件
	file.Read(b)
	fmt.Println(b)

}

結果:

[79 112 101 110 70 105 108 101 230 150 135 228 187 182 230 181 139 232 175 149 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

這裏我們設置接收讀取數據大小,爲1024個字節,可以看到,我們讀取出來的數據,都是是二進制數據

如何轉成我們所需要的文本數據呢?

fmt.Println(string(b))

	b:=make([]byte,1024)
	//讀取文件
	file.Read(b)
	fmt.Println(b)
	fmt.Println(string(b))

結果:

[239 187 191 79 112 101 110 70 105 108 101 84 101 115 116 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
OpenFileTest  

OpenFile( )讀取文件這個函數有三個參數,第一個參數表示打開文件的路徑,第二個參數表示模式,常見的模式有

O_RDONLY(只讀模式),O_WRONLY(只寫模式), O_RDWR( 可讀可寫模式),O_APPEND(追加模式)。

第三個參數,表示權限,取值範圍(0-7)

表示如下:

0:沒有任何權限

1:執行權限(如果是可執行文件,是可以運行的)

2:寫權限

3:寫權限與執行權限

4:讀權限

5:讀權限與執行權限

6:讀權限與寫權限

7:讀權限,寫權限,執行權限

讀取文件方式跟上述方式幾乎一樣,只是在openfile時,需要多加幾個參數。參數表示如上訴所示。

讀取文件2:

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {

	//os.Open(文件路徑名稱),以只讀方式打開文件
	file,error:=os.Open("D:/go_test/file/a.txt")
	if error != nil{
		fmt.Println(error)
		fmt.Println("打開文件失敗")
		return
	}else{
		fmt.Println("打開文件成功!")
	}
	defer file.Close()

	//創建切片緩衝區
	r:=bufio.NewReader(file)
	//讀取文件內容
	b,_:=r.ReadBytes('\n')
	fmt.Println(string(b))
}

結果:

OpenFileTest

我們按行讀取數據。需要用到bufio包中的ReadByutes函數。創建緩衝區,也就是我們需要存儲數據的區域。將從文件中讀取的數據,存儲在緩衝區中,然後將緩衝區的數據讀取出來。提供緩衝區的原因:爲了緩和CPU與磁盤之間速度不匹配的矛盾。文件緩衝區是用來暫時存儲讀寫期間文件數據而在內存區域中預留的一定空間。

而我們在使用ReadBytes( )函數時,傳遞的參數是‘\n’,表示遇到’\n’就結束。這裏傳遞的參數,可以不是'\n'。是任意字符都可以。

如果在文件截取中,沒有到我們要找的標誌位,到文件末尾也會自動停止。

這種方式也就是我們可以讀取文件中的行。 

 

發佈了65 篇原創文章 · 獲贊 50 · 訪問量 70萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章