mirror of
https://gitee.com/johng/gf
synced 2026-06-25 01:05:41 +08:00
gfile改进中
This commit is contained in:
@ -61,7 +61,7 @@ func Create(path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 打开文件
|
||||
// 打开文件(os.O_RDWR|os.O_CREATE, 0666)
|
||||
func Open(path string) (*os.File, error) {
|
||||
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
@ -70,7 +70,7 @@ func Open(path string) (*os.File, error) {
|
||||
return f, nil
|
||||
}
|
||||
|
||||
// 打开文件
|
||||
// 打开文件(带flag)
|
||||
func OpenWithFlag(path string, flag int) (*os.File, error) {
|
||||
f, err := os.OpenFile(path, flag, 0666)
|
||||
if err != nil {
|
||||
@ -79,6 +79,15 @@ func OpenWithFlag(path string, flag int) (*os.File, error) {
|
||||
return f, nil
|
||||
}
|
||||
|
||||
// 打开文件(带flag&perm)
|
||||
func OpenWithFlagPerm(path string, flag int, perm int) (*os.File, error) {
|
||||
f, err := os.OpenFile(path, flag, os.FileMode(perm))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
|
||||
// 判断所给路径文件/文件夹是否存在
|
||||
func Exists(path string) bool {
|
||||
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
||||
|
||||
@ -13,6 +13,11 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
// 方法中涉及到读取的时候的缓冲大小
|
||||
gREAD_BUFFER = 1024
|
||||
)
|
||||
|
||||
// (文本)读取文件内容
|
||||
func GetContents(path string) string {
|
||||
return string(GetBinContents(path))
|
||||
@ -76,25 +81,34 @@ func PutBinContentsAppend(path string, content []byte) error {
|
||||
}
|
||||
|
||||
// 获得文件内容下一个指定字节的位置
|
||||
func GetNextCharOffset(file *os.File, char string, start int64) int64 {
|
||||
c := []byte(char)[0]
|
||||
b := make([]byte, 1)
|
||||
o := start
|
||||
func GetNextCharOffset(file *os.File, char byte, start int64) int64 {
|
||||
buffer := make([]byte, gREAD_BUFFER)
|
||||
offset := start
|
||||
for {
|
||||
_, err := file.ReadAt(b, o)
|
||||
if err != nil {
|
||||
return 0
|
||||
if n, err := file.ReadAt(buffer, offset); n > 0 {
|
||||
for i := 0; i < n; i++ {
|
||||
if buffer[i] == char {
|
||||
return int64(i)
|
||||
}
|
||||
}
|
||||
offset++
|
||||
} else if err != nil {
|
||||
break
|
||||
}
|
||||
if b[0] == c {
|
||||
return o
|
||||
}
|
||||
o++
|
||||
}
|
||||
return 0
|
||||
return -1
|
||||
}
|
||||
|
||||
// 获得文件内容直到下一个指定字节的位置(返回值不包含该位置字符内容)
|
||||
func GetBinContentsTilChar(file *os.File, char byte, start int64) ([]byte, int64) {
|
||||
if offset := GetNextCharOffset(file, char, start); offset != -1 {
|
||||
return GetBinContentsByTwoOffsets(file, start, offset), offset
|
||||
}
|
||||
return nil, -1
|
||||
}
|
||||
|
||||
// 获得文件内容中两个offset之间的内容 [start, end)
|
||||
func GetBinContentByTwoOffsets(file *os.File, start int64, end int64) []byte {
|
||||
func GetBinContentsByTwoOffsets(file *os.File, start int64, end int64) []byte {
|
||||
buffer := make([]byte, end - start)
|
||||
if _, err := file.ReadAt(buffer, start); err != nil {
|
||||
return nil
|
||||
|
||||
19
geg/os/gfile/gfile_contents.go
Normal file
19
geg/os/gfile/gfile_contents.go
Normal file
@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gitee.com/johng/gf/g/os/gfile"
|
||||
)
|
||||
|
||||
func main() {
|
||||
path := "/tmp/temp"
|
||||
content := `123
|
||||
456
|
||||
789`
|
||||
gfile.PutContents(path, content)
|
||||
f, err := gfile.Open(path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(gfile.GetBinContentsTilChar(f, '\n', 0))
|
||||
}
|
||||
Reference in New Issue
Block a user