Go語(yǔ)言清除文件中空行的方法
更新時(shí)間:2015年02月27日 14:51:58 作者:秋風(fēng)秋雨
這篇文章主要介紹了Go語(yǔ)言清除文件中空行的方法,實(shí)例分析了Go語(yǔ)言針對(duì)文件的操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了Go語(yǔ)言清除文件中空行的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
這里使用Go語(yǔ)言讀取源文件,去掉空行,并寫到目標(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語(yǔ)言程序設(shè)計(jì)有所幫助。
相關(guān)文章
golang中sync.Mutex的實(shí)現(xiàn)方法
本文主要介紹了golang中sync.Mutex的實(shí)現(xiàn)方法,mutex?主要有兩個(gè)?method:?Lock()?和?Unlock(),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
GO語(yǔ)言文件的創(chuàng)建與打開實(shí)例分析
這篇文章主要介紹了GO語(yǔ)言文件的創(chuàng)建與打開的具體用法,實(shí)例分析了GO語(yǔ)言文件創(chuàng)建與打開操作中所涉及的函數(shù)具體用法,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12
Golang中自定義json序列化時(shí)間格式的示例代碼
Go語(yǔ)言作為一個(gè)由Google開發(fā),號(hào)稱互聯(lián)網(wǎng)的C語(yǔ)言的語(yǔ)言,自然也對(duì)JSON格式支持很好,下面這篇文章主要介紹了關(guān)于Golang中自定義json序列化時(shí)間格式的相關(guān)內(nèi)容,下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧2024-08-08
Go實(shí)現(xiàn)map轉(zhuǎn)json的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用Go語(yǔ)言實(shí)現(xiàn)map轉(zhuǎn)json的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-09-09

