2021-10-25 11:29:46 +08:00
|
|
|
|
# Golang 文件读写
|
2020-10-13 14:08:29 +08:00
|
|
|
|
|
2020-11-23 17:52:38 +08:00
|
|
|
|
os.File 封装了所有文件相关的操作,File 是一个结构体,有很多方法。
|
2020-10-13 14:08:29 +08:00
|
|
|
|
|
2020-11-23 17:52:38 +08:00
|
|
|
|
## 1.os 包
|
|
|
|
|
|
|
|
|
|
### 1.1.创建文件
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
func Create(name string) (file *File, err error)
|
|
|
|
|
func NewFile(fd uintptr, name string) *File
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Create 方法示例如下:
|
2020-10-13 14:08:29 +08:00
|
|
|
|
|
|
|
|
|
```go
|
2021-09-02 13:25:19 +08:00
|
|
|
|
f, err := os.Create(fileName)
|
2020-10-13 14:08:29 +08:00
|
|
|
|
defer f.Close()
|
|
|
|
|
if err !=nil {
|
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
|
} else {
|
|
|
|
|
_,err=f.Write([]byte("要写入的文本内容"))
|
|
|
|
|
checkErr(err)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2020-11-23 17:52:38 +08:00
|
|
|
|
### 1.2.打开文件
|
2020-10-13 14:08:29 +08:00
|
|
|
|
|
|
|
|
|
```go
|
2020-11-23 17:52:38 +08:00
|
|
|
|
func Open(name string) (*File, error)
|
|
|
|
|
func OpenFile(name string, flag int, perm FileMode) (*File, error)
|
2020-10-13 14:08:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-07-17 11:32:46 +08:00
|
|
|
|
flag 如下:
|
2020-10-13 14:08:29 +08:00
|
|
|
|
|
|
|
|
|
```go
|
2021-09-22 20:10:00 +08:00
|
|
|
|
// 打开方式
|
2020-10-13 14:08:29 +08:00
|
|
|
|
const (
|
2021-09-22 20:10:00 +08:00
|
|
|
|
// 只读模式
|
2020-10-13 14:08:29 +08:00
|
|
|
|
O_RDONLY int = syscall.O_RDONLY // open the file read-only.
|
2021-09-22 20:10:00 +08:00
|
|
|
|
// 只写模式
|
2020-10-13 14:08:29 +08:00
|
|
|
|
O_WRONLY int = syscall.O_WRONLY // open the file write-only.
|
2021-09-22 20:10:00 +08:00
|
|
|
|
// 可读可写
|
2020-10-13 14:08:29 +08:00
|
|
|
|
O_RDWR int = syscall.O_RDWR // open the file read-write.
|
2021-09-22 20:10:00 +08:00
|
|
|
|
// 追加内容
|
2020-10-13 14:08:29 +08:00
|
|
|
|
O_APPEND int = syscall.O_APPEND // append data to the file when writing.
|
2021-09-22 20:10:00 +08:00
|
|
|
|
// 创建文件,如果文件不存在
|
2020-10-13 14:08:29 +08:00
|
|
|
|
O_CREATE int = syscall.O_CREAT // create a new file if none exists.
|
2021-12-01 20:34:25 +08:00
|
|
|
|
// 与创建文件一同使用,文件必须不存在
|
2020-10-13 14:08:29 +08:00
|
|
|
|
O_EXCL int = syscall.O_EXCL // used with O_CREATE, file must not exist
|
2021-09-22 20:10:00 +08:00
|
|
|
|
// 打开一个同步的文件流
|
2020-10-13 14:08:29 +08:00
|
|
|
|
O_SYNC int = syscall.O_SYNC // open for synchronous I/O.
|
2021-09-22 20:10:00 +08:00
|
|
|
|
// 如果可能,打开时缩短文件
|
2020-10-13 14:08:29 +08:00
|
|
|
|
O_TRUNC int = syscall.O_TRUNC // if possible, truncate file when opened.
|
|
|
|
|
)
|
|
|
|
|
```
|
2020-11-23 17:52:38 +08:00
|
|
|
|
|
2021-07-17 11:32:46 +08:00
|
|
|
|
perm 模式如下:
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
const (
|
|
|
|
|
// The single letters are the abbreviations
|
|
|
|
|
// used by the String method's formatting.
|
|
|
|
|
// 文件夹模式
|
|
|
|
|
ModeDir FileMode = 1 << (32 - 1 - iota) // d: is a directory
|
|
|
|
|
// 追加模式
|
|
|
|
|
ModeAppend // a: append-only
|
|
|
|
|
// 单独使用
|
|
|
|
|
ModeExclusive // l: exclusive use
|
|
|
|
|
// 临时文件
|
|
|
|
|
ModeTemporary // T: temporary file; Plan 9 only
|
|
|
|
|
// 象征性的关联
|
|
|
|
|
ModeSymlink // L: symbolic link
|
|
|
|
|
// 设备文件
|
|
|
|
|
ModeDevice // D: device file
|
|
|
|
|
// 命名管道
|
|
|
|
|
ModeNamedPipe // p: named pipe (FIFO)
|
|
|
|
|
// Unix 主机 socket
|
|
|
|
|
ModeSocket // S: Unix domain socket
|
|
|
|
|
// 设置uid
|
|
|
|
|
ModeSetuid // u: setuid
|
|
|
|
|
// 设置gid
|
|
|
|
|
ModeSetgid // g: setgid
|
|
|
|
|
// UNIX 字符串设备,当设备模式是设置unix
|
|
|
|
|
ModeCharDevice // c: Unix character device, when ModeDevice is set
|
|
|
|
|
// 粘性的
|
|
|
|
|
ModeSticky // t: sticky
|
|
|
|
|
// 非常规文件;对该文件一无所知
|
|
|
|
|
ModeIrregular // ?: non-regular file; nothing else is known about this file
|
|
|
|
|
|
|
|
|
|
// bit位遮盖,不变的文件设置为none
|
|
|
|
|
// Mask for the type bits. For regular files, none will be set.
|
|
|
|
|
ModeType = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice | ModeCharDevice | ModeIrregular
|
|
|
|
|
// 权限位
|
|
|
|
|
ModePerm FileMode = 0777 // Unix permission bits
|
|
|
|
|
)
|
|
|
|
|
```
|
|
|
|
|
|
2020-11-23 17:52:38 +08:00
|
|
|
|
### 1.3.获取文件状态
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
func (f *File) Stat() (fi FileInfo, err error)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 1.4.读取文件
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
func (f *File) Read(b []byte) (n int, err error)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 1.5.写入文件
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
func (f *File) Write(b []byte) (n int, err error)
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-11 11:11:13 +08:00
|
|
|
|
### 1.6.设置读写位置
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
/**
|
|
|
|
|
* @param whence
|
2022-01-12 15:23:19 +08:00
|
|
|
|
* io.SeekStart/os.SEEK_SET: seek relative to the origin of the file
|
|
|
|
|
* io.SeekCurrent/os.SEEK_CUR: seek relative to the current offset
|
|
|
|
|
* io.SeekEnd/os.SEEK_END: seek relative to the end
|
2021-03-11 11:11:13 +08:00
|
|
|
|
*/
|
|
|
|
|
func (f *File) Seek(offset int64, whence int) (ret int64, err error)
|
|
|
|
|
```
|
|
|
|
|
|
2022-01-13 18:11:46 +08:00
|
|
|
|
### 1.7.读取全部
|
2020-11-23 17:52:38 +08:00
|
|
|
|
|
|
|
|
|
示例如下:
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import(
|
2022-01-13 18:11:46 +08:00
|
|
|
|
"os"
|
2020-11-23 17:52:38 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main(){
|
2022-01-13 18:11:46 +08:00
|
|
|
|
b, err := os.ReadFile("C:/example.txt")
|
2020-11-23 17:52:38 +08:00
|
|
|
|
|
|
|
|
|
if err != nil{
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println(b)
|
|
|
|
|
fmt.Println(string(b))
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-01-13 18:11:46 +08:00
|
|
|
|
### 1.8.写入全部
|
2020-11-23 17:52:38 +08:00
|
|
|
|
|
|
|
|
|
示例如下:
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import(
|
2022-01-13 18:11:46 +08:00
|
|
|
|
"os"
|
2020-11-23 17:52:38 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main(){
|
|
|
|
|
str := []byte("Hello,Go!")
|
2022-01-13 18:11:46 +08:00
|
|
|
|
err := os.WriteFile("C:/example.txt", str, 0644)
|
2020-11-23 17:52:38 +08:00
|
|
|
|
if err != nil{
|
|
|
|
|
panic(err)
|
|
|
|
|
}else{
|
|
|
|
|
fmt.Println("WriteFile Success!")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-01-13 18:11:46 +08:00
|
|
|
|
## 2.bufio 包
|
2020-11-23 17:52:38 +08:00
|
|
|
|
|
2022-01-13 18:11:46 +08:00
|
|
|
|
### 2.1.读文件
|
2020-11-23 17:52:38 +08:00
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
func NewReader(rd io.Reader) *Reader
|
|
|
|
|
func (b *Reader) Read(p []byte) (n int, err error)
|
|
|
|
|
func (b *Reader) ReadLine() (line []byte, isPrefix bool, err error)
|
|
|
|
|
```
|
|
|
|
|
|
2022-01-13 18:11:46 +08:00
|
|
|
|
### 2.2.写文件
|
2020-11-23 17:52:38 +08:00
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
func NewWriter(w io.Writer) *Writer
|
|
|
|
|
func (b *Writer) Write(p []byte) (nn int, err error)
|
|
|
|
|
```
|