GO- 文件操作2-实战案例

导入依赖

import (
    "fmt"
    "io"
    "os"
    "io/ioutil"
)

文件拷贝1

func copyFile(srcFilename string,dstFilename string) {
    bytes, _ := ioutil.ReadFile(srcFilename)
    err := ioutil.WriteFile(dstFilename, bytes, 0754)
    if err != nil {
        fmt.Println("拷贝失败,err=", err)
    } else {
        fmt.Println("拷贝成功!")
    }
}
func main51() {
    copyFile("d:/找你妹.txt","d:/找自己.txt")
}

文件拷贝2

func main52() {
    //打开来源文件和目标文件(目标文件可能并不存在)
    srcFile, _ := os.OpenFile("d:/找你妹.txt", os.O_RDONLY, 0666)
    dstFile, _ := os.OpenFile("d:/找我妹.txt", os.O_WRONLY|os.O_CREATE, 0666)

    //程序的最后关闭这两个文件
    defer srcFile.Close()
    defer dstFile.Close()

    //使用标准库提供的API实现文件拷贝,返回的是拷贝的字节数
    written, err := io.Copy(dstFile, srcFile)
    if err!=nil{
        fmt.Println("拷贝失败,err=",err)
    }else{
        fmt.Println("拷贝成功,字节数=",written)
    }
}

实现文件字符数统计

func CountCharsOfFile(path string)map[string]int {
    bytes, _ := ioutil.ReadFile(path)
    str := string(bytes)

    var numberCount,letterCount,spaceCount,othersCount int

    //遍历字符串中的每个字符
    for i,c := range str{
        fmt.Printf("No%d,%c\n",i,c)

        //逐个判断每个字符在字符集中的序号
        switch {
        case c >= '0' && c<='9':
            numberCount++
        case (c >= 'a' && c<='z') || (c >= 'A' && c<='Z'):
            letterCount++
        case c == ' ' || c=='\t':
            spaceCount++
        default:
            othersCount++
        }
    }
    return map[string]int{"数字":numberCount,"字母":letterCount,"空白":spaceCount,"其它":othersCount}
}
func main() {
    retMap := CountCharsOfFile("d:/temp/test.txt")
    fmt.Println(retMap)
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值