-
Notifications
You must be signed in to change notification settings - Fork 3
/
operationReadFile.go
61 lines (51 loc) · 1.21 KB
/
operationReadFile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package fileKit
import (
"bufio"
"github.com/gogf/gf/v2/os/gfile"
"os"
)
var (
ReadLines func(file string, callback func(line string) error) error = gfile.ReadLines
ReadLinesBytes func(file string, callback func(bytes []byte) error) error = gfile.ReadLinesBytes
)
// ReadFile 读取文件的数据.
/*
PS:
(1) ioutil.ReadFile() 比 ioutil.ReadAll() 性能好,特别是大文件;
(2) 编码必须为"UTF-8"!!!
(3) 读取大文件也很快.
@param path 文件的路径(不能是目录的路径)
*/
func ReadFile(filePath string) ([]byte, error) {
if err := AssertExistAndIsFile(filePath); err != nil {
return nil, err
}
return os.ReadFile(filePath)
}
func ReadFileToString(filePath string) (string, error) {
data, err := ReadFile(filePath)
return string(data), err
}
// ReadFileByLine
/*
@param f 调用scan.Bytes() || scan.Text()
*/
func ReadFileByLine(filePath string, f func(scan *bufio.Scanner)) error {
if err := AssertExistAndIsFile(filePath); err != nil {
return err
}
file, err := os.Open(filePath)
defer file.Close()
if err != nil {
return err
}
scan := bufio.NewScanner(file)
for scan.Scan() {
// 自定义
f(scan)
}
if err := scan.Err(); err != nil {
return err
}
return nil
}