douchai7891 2014-07-17 19:19
浏览 386
已采纳

如何在Golang中写入文件

i am trying to write to to a file. i read the whole content of the file and now i want to change the content of the file based on some word that i have got from the file. but when i check, the content of the file, it is still the same and it has not change. this is what i used

if strings.Contains(string(read), sam) {
    fmt.Println("this file contain that word")
    temp := strings.ToUpper(sam)
    fmt.Println(temp)
    err := ioutil.WriteFile(fi.Name(), []byte(temp), 0644)
} else {
    fmt.Println(" the word is not in the file")
}
  • 写回答

2条回答 默认 最新

  • douhuan1908 2014-07-17 19:48
    关注

    Considering that your call to ioutil.WriteFile() is consistent with what is used in "Go by Example: Writing Files", this should work.

    But that Go by example article check the err just after the write call.

    You check the err outside the scope of your test:

        if matched {
            read, err := ioutil.ReadFile(path)
            //fmt.Println(string(read))
            fmt.Println(" This is the name of the file", fi.Name())
            if strings.Contains(string(read), sam) {
                fmt.Println("this file contain that word")
                Value := strings.ToUpper(sam)
                fmt.Println(Value)
                err = ioutil.WriteFile(fi.Name(), []byte(Value), 0644)
            } else {
                fmt.Println(" the word is not in the file")
            }
            check(err)   <===== too late
        }
    

    The err you are testing is the one you got when reading the file (ioutil.ReadFile), because of blocks and scope.

    You need to check the error right after the Write call

                err = ioutil.WriteFile(fi.Name(), []byte(Value), 0644)
                check(err)   <===== too late
    

    Since WriteFile overwrite the all file, you could strings.Replace() to replace your word by its upper case equivalent:

    r := string(read)
    r = strings.Replace(r, sam, strings.ToUpper(sam), -1)
    err := ioutil.WriteFile(fi.Name(), []byte(r), 0644)
    

    For a replace which is case insensitive, use a regexp as in "How do I do a case insensitive regular expression in Go?".
    The, use func (*Regexp) ReplaceAllString:

    re := regexp.MustCompile("(?i)\\b"+sam+"\\b")
    r = re.ReplaceAllString(r, strings.ToUpper(sam))
    err := ioutil.WriteFile(fi.Name(), []byte(r), 0644)
    

    Note the \b: word boundary to find the any word starting and ending with sam content (instead of finding substrings containing sam content).
    If you want to replace substrings, simply drop the \b:

    re := regexp.MustCompile("(?i)"+sam)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛