分享
Go文件操作
abcijkxyz · · 1382 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
读取文件中的内容
假设你想获取文件中感兴趣的内容,但是,你不希望获取所有内容。假设文件的内容如下所示:
文件内容
1. 网页视觉设计理论之少些质感,多些版式.
http://www.ynetx.com/theory/560-1.html
2. 设计理论:
http://www.ynetx.com/design/theory/
3.Google的新设计理念Material Design
http://www.infoq.com/cn/news/2014/07/google-material-design-android/
你只想获取http开头的行内容。实现代码如下:
line, err := reader.ReadString('\n')
// skip all line starting without line 'http'
// if equal := strings.Index(line, "http"); equal < 0 {
// fmt.Print(line)
// }
//alternatively, only print line starting with 'http'
if equal := strings.Index(line, "http"); equal >= 0 {
fmt.Print(line)
}
完整代码如下所示:
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"strings"
"unicode"
)
func ReadFile(filePath string) {
file, err := os.Open(filePath)
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
reader := bufio.NewReader(file)
for {
line, err := reader.ReadString('\n')
// skip all line starting without line 'http'
// if equal := strings.Index(line, "http"); equal < 0 {
// fmt.Print(line)
// }
//alternatively, only print line starting with 'http'
if equal := strings.Index(line, "http"); equal >= 0 {
fmt.Print(line)
}
if err == io.EOF {
break
}
if err != nil {
fmt.Println(err)
}
}
}
func main(){
//文件路径../util/art.txt
ReadFile("../util/art.txt")
}
输出结果:
http://www.ynetx.com/theory/560-1.html
http://www.ynetx.com/design/theory/
http://www.infoq.com/cn/news/2014/07/google-material-design-android/
欢迎加入我的微信公众号
good!
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信1382 次点击
上一篇:Go 多态功能实现
下一篇:Go语言为什么这么流行?
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
读取文件中的内容
假设你想获取文件中感兴趣的内容,但是,你不希望获取所有内容。假设文件的内容如下所示:
文件内容
1. 网页视觉设计理论之少些质感,多些版式.
http://www.ynetx.com/theory/560-1.html
2. 设计理论:
http://www.ynetx.com/design/theory/
3.Google的新设计理念Material Design
http://www.infoq.com/cn/news/2014/07/google-material-design-android/
你只想获取http开头的行内容。实现代码如下:
line, err := reader.ReadString('\n')
// skip all line starting without line 'http'
// if equal := strings.Index(line, "http"); equal < 0 {
// fmt.Print(line)
// }
//alternatively, only print line starting with 'http'
if equal := strings.Index(line, "http"); equal >= 0 {
fmt.Print(line)
}
完整代码如下所示:
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"strings"
"unicode"
)
func ReadFile(filePath string) {
file, err := os.Open(filePath)
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
reader := bufio.NewReader(file)
for {
line, err := reader.ReadString('\n')
// skip all line starting without line 'http'
// if equal := strings.Index(line, "http"); equal < 0 {
// fmt.Print(line)
// }
//alternatively, only print line starting with 'http'
if equal := strings.Index(line, "http"); equal >= 0 {
fmt.Print(line)
}
if err == io.EOF {
break
}
if err != nil {
fmt.Println(err)
}
}
}
func main(){
//文件路径../util/art.txt
ReadFile("../util/art.txt")
}
输出结果:
http://www.ynetx.com/theory/560-1.html
http://www.ynetx.com/design/theory/
http://www.infoq.com/cn/news/2014/07/google-material-design-android/
欢迎加入我的微信公众号
good!