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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化