美文网首页
golang删除文件中空行

golang删除文件中空行

作者: jojo1313 | 来源:发表于2019-10-02 00:52 被阅读0次

“ ” = 32 空格
“\r” = 13 回车符
"\n" = 10 换行符
“\t” = 9 tab 键

删除文件中空行

func DeleteBlankFile(srcFilePah string, destFilePath string) error {
        srcFile, err := os.OpenFile(srcFilePah, os.O_RDONLY, 0666)
        defer srcFile.Close()
        if err != nil {
            return err
        }
        srcReader := bufio.NewReader(srcFile)
        destFile, err := os.OpenFile(destFilePath, os.O_WRONLY|os.O_CREATE, 0666)
        defer destFile.Close()
        if err != nil {
            return err
        }

        for {
            str, err := srcReader.ReadString('\n')
            if err != nil {
                if err == io.EOF {
                    fmt.Print("The file end is touched.")
                    break
                } else {
                    return err
                }
            }

            if strings.HasSuffix(str," \r\n"){
            continue
            }
            if strings.HasPrefix(str,"\r\n"){
            continue
            }
            fmt.Println(len(str))
            fmt.Print(str)
            destFile.WriteString(str)
        }
        return nil
}

相关文章

网友评论

      本文标题:golang删除文件中空行

      本文链接:https://www.haomeiwen.com/subject/wpelpctx.html