1.打开文件和关闭文件
file, err := os.Open("file/wordtest.txt")
fmt.Println("open file err=", err)
fmt.Printf("file=%v\n", file)
fmt.Println("close file err=", err)
file=&{0xc000068a00}
2.带缓冲的Reader读取文件
file, err := os.Open("file/wordtest.txt")
fmt.Println("open file err=", err)
reader := bufio.NewReader(file)
str, err := reader.ReadString('\n')
3.一次性读取文件
file := "file/wordtest.txt"
content, err := os.ReadFile(file)
fmt.Printf("read file err=%v", err)
fmt.Printf("%v\n", content)
fmt.Printf("%s\n", string(content))
[104 101 108 108 111 44 119 111 114 108 100 13 10 231 143 160 230 181 183 239 188 129 97 98 99 33 71 111 108 97 110 103
33 13 10 71 111 44 104 101 108 108 111 44 119 111 114 108 100 13 10]
4.创建文件写入内容
filePath := "./file/wordtest2.txt"
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
fmt.Printf("open file err=%v\n", err)
str := "hello jinitaimei\n"
writer := bufio.NewWriter(file)
5.打开一个存在的文件,把原来的内容覆盖新的内容
filePath := "./file/wordtest2.txt"
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_TRUNC, 0666)
fmt.Printf("open file err=%v\n", err)
str := "hello Xiamen\r\n"
writer := bufio.NewWriter(file)
for i := 0; i < 10; i++ {
6.对已经存在的文件,追加内容
filePath := "./file/wordtest2.txt"
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_APPEND, 0666)
fmt.Println("open file err=%v\n", err)
str := "Humans, I want the clear,pollution-free water!\r\n"
writer := bufio.NewWriter(file)
for i := 0; i < 10; i++ {
7.把原来的内容读取出来,再往文件追加几句话
filePath := "./file/wordtest2.txt"
file, err := os.OpenFile(filePath, os.O_RDWR|os.O_APPEND, 0666)
fmt.Println("open file err=%v\n", err)
reader := bufio.NewReader(file)
str2, err := reader.ReadString('\n')
str := "Sweet dolphins, I wish you all peace.\r\n"
writer := bufio.NewWriter(file)
Humans, I want the clear,pollution-free water!
Humans, I want the clear,pollution-free water!
Humans, I want the clear,pollution-free water!
Humans, I want the clear,pollution-free water!
Humans, I want the clear,pollution-free water!
Humans, I want the clear,pollution-free water!
Humans, I want the clear,pollution-free water!
Humans, I want the clear,pollution-free water!
Humans, I want the clear,pollution-free water!
Humans, I want the clear,pollution-free water!
Humans, I want the clear,pollution-free water!
Humans, I want the clear,pollution-free water!
Humans, I want the clear,pollution-free water!
Humans, I want the clear,pollution-free water!
Humans, I want the clear,pollution-free water!
Humans, I want the clear,pollution-free water!
Humans, I want the clear,pollution-free water!
Humans, I want the clear,pollution-free water!
Sweet dolphins, I wish you all peace.
Sweet dolphins, I wish you all peace.
Sweet dolphins, I wish you all peace.
Sweet dolphins, I wish you all peace.
Sweet dolphins, I wish you all peace.
8.把一个文件的内容追加到另一个文件中
filePath1 := "./file/word1.txt"
filePath2 := "./file/word2.txt"
content, err := os.ReadFile(filePath1)
fmt.Printf("read file err=%v\n", err)
word2_file, err := os.OpenFile(filePath2, os.O_RDWR|os.O_APPEND, 0666)
writer := bufio.NewWriter(word2_file)
writer.WriteString(string(content))
When spring comes, all the seeds begin to burgeon.
People wait in the lounge for boarding.
People wait in the lounge for boarding.When spring comes, all the seeds begin to burgeon.
9.拷贝文件到另一个目录中
func CopyFile(dstFileName string, srcFileName string) (written int64, err error) {
srcFile, err := os.Open(srcFileName)
fmt.Printf("open file err=%v\n", err)
reader := bufio.NewReader(srcFile)
dstFile, err := os.OpenFile(dstFileName, os.O_WRONLY|os.O_CREATE, 0666)
fmt.Printf("open file err=%v\n", err)
writer := bufio.NewWriter(dstFile)
return io.Copy(writer, reader)
srcFile := "d:/goldpig.jpg"
dstFile := "e:/goldpig.jpg"
_, err := CopyFile(dstFile, srcFile)
fmt.Printf("拷贝错误 err=%v\n", err)
10.统计文件中不同类型的字符的数量
fileName := "./file/abc.txt"
file, err := os.Open(fileName)
fmt.Printf("open file err=%v\n", err)
reader := bufio.NewReader(file)
str, err := reader.ReadString('\n')
case v >= ('a') && v <= 'z':
case v >= 'A' && v <= 'Z':
case v == ' ' || v == '\t':
case v >= '0' && v <= '9':
fmt.Printf("字符的个数=%v 数字的个数=%v 空格的个数=%v 其他字符的个数=%v\n",
count.ChCount, count.NumCount, count.SpaceCount, count.OtherCount)
字符的个数=13 数字的个数=7 空格的个数=2 其他字符的个数=4