Go語言清除文件中空行的方法
更新時(shí)間:2015年02月27日 14:51:58 作者:秋風(fēng)秋雨
這篇文章主要介紹了Go語言清除文件中空行的方法,實(shí)例分析了Go語言針對(duì)文件的操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了Go語言清除文件中空行的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
這里使用Go語言讀取源文件,去掉空行,并寫到目標(biāo)文件
復(fù)制代碼 代碼如下:
/**
* Created with IntelliJ IDEA.
* User: hyper-carrot
* Date: 12-8-31
* Time: 下午4:04
* To change this template use File | Settings | File Templates.
*/
package main
import (
"os"
"bufio"
"fmt"
)
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
}
var destContent string
for {
str, _ := srcReader.ReadString('\n')
if err != nil {
if err == io.EOF {
fmt.Print("The file end is touched.")
break
} else {
return err
}
}
if 0 == len(str) || str == "\r\n" {
continue
}
fmt.Print(str)
destFile.WriteString(str)
}
return nil
}
func main() {
DeleteBlankFile("e:\\src.txt", "e:\\dest.txt")
}
* Created with IntelliJ IDEA.
* User: hyper-carrot
* Date: 12-8-31
* Time: 下午4:04
* To change this template use File | Settings | File Templates.
*/
package main
import (
"os"
"bufio"
"fmt"
)
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
}
var destContent string
for {
str, _ := srcReader.ReadString('\n')
if err != nil {
if err == io.EOF {
fmt.Print("The file end is touched.")
break
} else {
return err
}
}
if 0 == len(str) || str == "\r\n" {
continue
}
fmt.Print(str)
destFile.WriteString(str)
}
return nil
}
func main() {
DeleteBlankFile("e:\\src.txt", "e:\\dest.txt")
}
希望本文所述對(duì)大家的Go語言程序設(shè)計(jì)有所幫助。
相關(guān)文章
淺談Golang中創(chuàng)建一個(gè)簡單的服務(wù)器的方法
這篇文章主要介紹了淺談Golang中創(chuàng)建一個(gè)簡單的服務(wù)器的方法,golang中的net/http包對(duì)網(wǎng)絡(luò)的支持非常好,這樣會(huì)讓我們比較容易的建立起一個(gè)相對(duì)簡單的服務(wù)器,有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06基于go手動(dòng)寫個(gè)轉(zhuǎn)發(fā)代理服務(wù)的代碼實(shí)現(xiàn)
這篇文章主要介紹了基于go手動(dòng)寫個(gè)轉(zhuǎn)發(fā)代理服務(wù)的代碼實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02Golang RSA生成密鑰、加密、解密、簽名與驗(yàn)簽的實(shí)現(xiàn)
RSA 是最常用的非對(duì)稱加密算法,本文主要介紹了Golang RSA生成密鑰、加密、解密、簽名與驗(yàn)簽的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11