|
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/
package main
import (
"bufio"
"fmt"
"io"
"os"
)
var (
FileName string = "E:\\Code\\Golang\\Golang_Program\\文件处理\\我有一只小毛驴.txt" //这是我们需要打开的文件,当然你也可以把它定义到从某个配置文件来获取变量。
InputFile *os.File //变量 InputFile 是 *os.File 类型的。该类型是一个结构,表示一个打开文件的描述符(文件句柄)。
InputError error //我们使用 os 包里的 Open 函数来打开一个文件。如果文件不存在或者程序没有足够的权限打开这个文件,Open函数会返回一个错误,InputError变量就是用来接收这个错误的。
Count int //这个变量是我们用来统计行号的,默认值为0.
)
func main() {
//InputFile,InputError = os.OpenFile(FileName,os.O_CREATE|os.O_RDWR,0644) //打开FileName文件,如果不存在就创建新文件,打开的权限是可读可写,权限是644。这种打开方式相对下面的打开方式权限会更大一些。
InputFile, InputError = os.Open(FileName) //使用 os 包里的 Open 函数来打开一个文件。该函数的参数是文件名,类型为 string 。我们以只读模式打开"FileName"文件。
if InputError != nil { //如果打开文件出错,那么我们可以给用户一些提示,然后在推出函数。
fmt.Printf("An error occurred on opening the inputfile\n" +
"Does the file exist?\n" +
"Have you got acces to it?\n")
return // exit the function on error
}
defer InputFile.Close() //defer关键字是用在程序即将结束时执行的代码,确保在程序退出前关闭该文件。
inputReader := bufio.NewReader(InputFile) //我们使用 bufio.NewReader()函数来获得一个读取器变量(读取器)。我们可以很方便的操作相对高层的 string 对象,而避免了去操作比较底层的字节。
for {
Count += 1
inputString, readerError := inputReader.ReadString('\n') //我们将inputReader里面的字符串按行进行读取。
if readerError == io.EOF {
return //如果遇到错误就终止循环。
}
fmt.Printf("The %d line is: %s",Count, inputString) //将文件的内容逐行(行结束符'\n')读取出来。
}
}
#以上代码执行结果如下:
The 1 line is: 歌曲:我有一只小毛驴
The 2 line is: 歌手:碧瑶
The 3 line is: 专辑:《瑶谣摇》
The 4 line is: 发行时间:2014-03-18
The 5 line is: 词:付林
The 6 line is: 曲:付林
The 7 line is: 歌词:
The 8 line is: 我有一只小毛驴
The 9 line is: 我从来也不骑
The 10 line is: 有一天我心血来潮骑着去赶集
The 11 line is: 我手里拿着小皮鞭
The 12 line is: 我心里很得意
The 13 line is: 不知怎么哗啦啦啦啦
The 14 line is: 摔了一身泥
The 15 line is: 我有一只小毛驴
The 16 line is: 我从来也不骑
The 17 line is: 有一天我心血来潮骑着去赶集
The 18 line is: 我手里拿着小皮鞭
The 19 line is: 我心里很得意
The 20 line is: 不知怎么哗啦啦啦啦
The 21 line is: 摔了一身泥
The 22 line is: 我有一只小毛驴 我从来也不骑
The 23 line is: 有一天我心血来潮 骑着去赶集
The 24 line is: 我手里拿着小皮鞭 我心里很得意
The 25 line is: 不知怎么哗啦啦啦 摔了一身泥
The 26 line is: 我有一只小毛驴 我从来也不骑
The 27 line is: 有一天我心血来潮 骑着去赶集
The 28 line is: 我手里拿着小皮鞭 我心里很得意
The 29 line is: 不知怎么哗啦啦啦 摔了一身泥
The 30 line is: 我手里拿着小皮鞭 我心里很得意
|
|
|