标签:写入 util read open 覆写 only ack break write
1.
package main import ( "fmt" "os" ) func main(){ //file指针 file,err:=os.Open("d:/test.txt") if err!=nil{ fmt.Println("open file err=",err) } //输出文件 fmt.Printf("file=%v",file) err=file.Close() if err!=nil{ fmt.Println("close file err=",err) } }
2.逐行读取
package main import ( "bufio" "fmt" "io" "os" ) //逐行读取 func main() { file, err := os.Open("d:/test.txt") if err != nil { fmt.Println("open file err=", err) } defer file.Close() const ( defaultltBufSize = 4096 //默认缓冲区为4096 ) reader := bufio.NewReader(file) //循环读取文件的内容 for { str, err := reader.ReadString(‘\n‘) //读取到一个换行就结束 fmt.Print(str) if err == io.EOF { break } } }
3.一次性读取所有,不适用大文件
package main import ( "fmt" "io/ioutil" ) func main(){ //使用ioutil.ReadFile,一次性将文件读取到位 file:="d:/test.txt" content,err:=ioutil.ReadFile(file) if err!=nil{ fmt.Printf("read file err=%v",err) } fmt.Printf("%v",string(content)) }
4.创建文件并写入
package main import ( "bufio" "fmt" "os" ) func main(){ //创建文件并写入 filePath:="d:/abc.txt" file,err:=os.OpenFile(filePath,os.O_WRONLY|os.O_CREATE,0666) if err!=nil{ fmt.Printf("open file err=%v\n",err) return } defer file.Close() str:="hello gardon\n" writer:=bufio.NewWriter(file) for i:=0;i<5;i++{ writer.WriteString(str) } writer.Flush() }
5.对文件覆写
package main import ( "bufio" "fmt" "os" ) func main(){ //覆盖文件内容 filePath:="d:/abc.txt" file,err:=os.OpenFile(filePath,os.O_WRONLY|os.O_TRUNC,0666) if err!=nil{ fmt.Printf("open file err=%v\n",err) return } defer file.Close() str:="hello kitty\n" writer:=bufio.NewWriter(file) for i:=0;i<5;i++{ writer.WriteString(str) } writer.Flush() }
6.追加内容
package main import ( "bufio" "fmt" "os" ) func main(){ //追加文件内容 filePath:="d:/abc.txt" file,err:=os.OpenFile(filePath,os.O_WRONLY|os.O_APPEND,0666) if err!=nil{ fmt.Printf("open file err=%v\n",err) return } defer file.Close() str:="我是追加内容\n" writer:=bufio.NewWriter(file) for i:=0;i<5;i++{ writer.WriteString(str) } writer.Flush() }
7.读取后追加内容
package main import ( "bufio" "fmt" "os" ) func main(){ //先读取后追加文件内容 filePath:="d:/abc.txt" file,err:=os.OpenFile(filePath,os.O_RDWR|os.O_APPEND,0666) if err!=nil{ fmt.Printf("open file err=%v\n",err) return } defer file.Close() str:="我是读取后追加内容\n" writer:=bufio.NewWriter(file) for i:=0;i<5;i++{ writer.WriteString(str) } writer.Flush() }
8.将一个文件的内容写入到另一个文件
package main import ( "fmt" "io/ioutil" ) func main(){ //将一个文件的内容写入另外一个文件 filepath:="d:/abc.txt" filepath2:="d:/kkk.txt" data,err:=ioutil.ReadFile(filepath) if err!=nil{ fmt.Printf("read file err=%v",err) return } err=ioutil.WriteFile(filepath2,data,0666) if err!=nil{ fmt.Printf("write file err=%v\n",err) } }
9.判断文件,文件夹是否存在
package main import ( "fmt" "os" ) func main() { fmt.Println(PathExists("d:/")) } //判断文件.或文件夹是否存在 func PathExists(path string) (bool, error) { _, err := os.Stat(path) if err == nil { return true, nil } if os.IsNotExist(err) { return false, nil } return false, nil }
10.文件拷贝
package main import ( "bufio" "fmt" "io" "os" ) //文件拷贝 func CopyFile(dstFileName string, srcFileName string) (written int64, err error) { srcFile, err := os.Open(srcFileName) if err != nil { fmt.Printf("open file err=%v\n", err) } defer srcFile.Close() reader := bufio.NewReader(srcFile) dstFile, err := os.OpenFile(dstFileName, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { fmt.Printf("open file err=%v\n", err) return } writer := bufio.NewWriter(dstFile) defer dstFile.Close() return io.Copy(writer, reader) } func main() { srcFile:="d:/flower.jpg" dstFile:="e:/abc.jpg" _,err:=CopyFile(dstFile,srcFile) if err==nil{ fmt.Print("拷贝完成\n") }else{ fmt.Printf("拷贝错误err=%v",err) } }
11.统计字符个数
package main import ( "fmt" "os" "io" "bufio" ) //定义一个结构体,用于保存统计结果 type CharCount struct { ChCount int // 记录英文个数 NumCount int // 记录数字的个数 SpaceCount int // 记录空格的个数 OtherCount int // 记录其它字符的个数 } func main() { //思路: 打开一个文件, 创一个Reader //每读取一行,就去统计该行有多少个 英文、数字、空格和其他字符 //然后将结果保存到一个结构体 fileName := "d:/abc.txt" file, err := os.Open(fileName) if err != nil { fmt.Printf("open file err=%v\n", err) return } defer file.Close() //定义个CharCount 实例 var count CharCount //创建一个Reader reader := bufio.NewReader(file) //开始循环的读取fileName的内容 for { str, err := reader.ReadString(‘\n‘) if err == io.EOF { //读到文件末尾就退出 break } //遍历 str ,进行统计 for _, v := range str { switch { case v >= ‘a‘ && v <= ‘z‘: fallthrough //穿透 case v >= ‘A‘ && v <= ‘Z‘: count.ChCount++ case v == ‘ ‘ || v == ‘\t‘: count.SpaceCount++ case v >= ‘0‘ && v <= ‘9‘: count.NumCount++ default : count.OtherCount++ } } } //输出统计的结果看看是否正确 fmt.Printf("字符的个数为=%v 数字的个数为=%v 空格的个数为=%v 其它字符个数=%v", count.ChCount, count.NumCount, count.SpaceCount, count.OtherCount) }
12.
13.
14.
15.
标签:写入 util read open 覆写 only ack break write
原文地址:https://www.cnblogs.com/huay/p/12165720.html