“ ” = 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
}
有疑问加站长微信联系(非本文作者)