Merging other languages
This commit is contained in:
312
en/07.5.md
312
en/07.5.md
@@ -1,153 +1,159 @@
|
||||
# 7.5 文件操作
|
||||
在任何计算机设备中,文件是都是必须的对象,而在Web编程中,文件的操作一直是Web程序员经常遇到的问题,文件操作在Web应用中是必须的,非常有用的,我们经常遇到生成文件目录,文件(夹)编辑等操作,现在我把Go中的这些操作做一详细总结并实例示范如何使用。
|
||||
## 目录操作
|
||||
文件操作的大多数函数都是在os包里面,下面列举了几个目录操作的:
|
||||
|
||||
- func Mkdir(name string, perm FileMode) error
|
||||
|
||||
创建名称为name的目录,权限设置是perm,例如0777
|
||||
|
||||
- func MkdirAll(path string, perm FileMode) error
|
||||
|
||||
根据path创建多级子目录,例如astaxie/test1/test2。
|
||||
|
||||
- func Remove(name string) error
|
||||
|
||||
删除名称为name的目录,当目录下有文件或者其他目录是会出错
|
||||
|
||||
- func RemoveAll(path string) error
|
||||
|
||||
根据path删除多级子目录,如果path是单个名称,那么该目录下的子目录全部删除。
|
||||
|
||||
|
||||
下面是演示代码:
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
os.Mkdir("astaxie", 0777)
|
||||
os.MkdirAll("astaxie/test1/test2", 0777)
|
||||
err := os.Remove("astaxie")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
os.RemoveAll("astaxie")
|
||||
}
|
||||
|
||||
|
||||
## 文件操作
|
||||
|
||||
### 建立与打开文件
|
||||
新建文件可以通过如下两个方法
|
||||
|
||||
- func Create(name string) (file *File, err Error)
|
||||
|
||||
根据提供的文件名创建新的文件,返回一个文件对象,默认权限是0666的文件,返回的文件对象是可读写的。
|
||||
|
||||
- func NewFile(fd uintptr, name string) *File
|
||||
|
||||
根据文件描述符创建相应的文件,返回一个文件对象
|
||||
|
||||
|
||||
通过如下两个方法来打开文件:
|
||||
|
||||
- func Open(name string) (file *File, err Error)
|
||||
|
||||
该方法打开一个名称为name的文件,但是是只读方式,内部实现其实调用了OpenFile。
|
||||
|
||||
- func OpenFile(name string, flag int, perm uint32) (file *File, err Error)
|
||||
|
||||
打开名称为name的文件,flag是打开的方式,只读、读写等,perm是权限
|
||||
|
||||
### 写文件
|
||||
写文件函数:
|
||||
|
||||
- func (file *File) Write(b []byte) (n int, err Error)
|
||||
|
||||
写入byte类型的信息到文件
|
||||
|
||||
- func (file *File) WriteAt(b []byte, off int64) (n int, err Error)
|
||||
|
||||
在指定位置开始写入byte类型的信息
|
||||
|
||||
- func (file *File) WriteString(s string) (ret int, err Error)
|
||||
|
||||
写入string信息到文件
|
||||
|
||||
写文件的示例代码
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
userFile := "astaxie.txt"
|
||||
fout, err := os.Create(userFile)
|
||||
if err != nil {
|
||||
fmt.Println(userFile, err)
|
||||
return
|
||||
}
|
||||
defer fout.Close()
|
||||
for i := 0; i < 10; i++ {
|
||||
fout.WriteString("Just a test!\r\n")
|
||||
fout.Write([]byte("Just a test!\r\n"))
|
||||
}
|
||||
}
|
||||
|
||||
### 读文件
|
||||
读文件函数:
|
||||
|
||||
- func (file *File) Read(b []byte) (n int, err Error)
|
||||
|
||||
读取数据到b中
|
||||
|
||||
- func (file *File) ReadAt(b []byte, off int64) (n int, err Error)
|
||||
|
||||
从off开始读取数据到b中
|
||||
|
||||
读文件的示例代码:
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
userFile := "asatxie.txt"
|
||||
fl, err := os.Open(userFile)
|
||||
if err != nil {
|
||||
fmt.Println(userFile, err)
|
||||
return
|
||||
}
|
||||
defer fl.Close()
|
||||
buf := make([]byte, 1024)
|
||||
for {
|
||||
n, _ := fl.Read(buf)
|
||||
if 0 == n {
|
||||
break
|
||||
}
|
||||
os.Stdout.Write(buf[:n])
|
||||
}
|
||||
}
|
||||
|
||||
### 删除文件
|
||||
Go语言里面删除文件和删除文件夹是同一个函数
|
||||
|
||||
- func Remove(name string) Error
|
||||
|
||||
调用该函数就可以删除文件名为name的文件
|
||||
|
||||
## links
|
||||
* [目录](<preface.md>)
|
||||
* 上一节: [模板处理](<07.4.md>)
|
||||
* 下一节: [字符串处理](<07.6.md>)
|
||||
# 7.5 Files
|
||||
|
||||
Files are must-have objects on every single computer device. It won't come as any surprise to you that web applications also make heavy use of them. In this section, we're going to learn how to operate on files in Go.
|
||||
|
||||
## Directories
|
||||
|
||||
In Go, most of the file operation functions are located in the `os` package. Here are some directory functions:
|
||||
|
||||
- func Mkdir(name string, perm FileMode) error
|
||||
|
||||
Create a directory with `name`. `perm` is the directory permissions, i.e 0777.
|
||||
|
||||
- func MkdirAll(path string, perm FileMode) error
|
||||
|
||||
Create multiple directories according to `path`, like `astaxie/test1/test2`.
|
||||
|
||||
- func Remove(name string) error
|
||||
|
||||
Removes directory with `name`. Returns error if it's not a directory or not empty.
|
||||
|
||||
- func RemoveAll(path string) error
|
||||
|
||||
Removes multiple directories according to `path`. Directories will not be deleted if `path` is a single path.
|
||||
|
||||
Code sample:
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
os.Mkdir("astaxie", 0777)
|
||||
os.MkdirAll("astaxie/test1/test2", 0777)
|
||||
err := os.Remove("astaxie")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
os.RemoveAll("astaxie")
|
||||
}
|
||||
|
||||
## Files
|
||||
|
||||
### Create and open files
|
||||
|
||||
There are two functions for creating files:
|
||||
|
||||
- func Create(name string) (file *File, err Error)
|
||||
|
||||
Create a file with `name` and return a read-writable file object with permission 0666.
|
||||
|
||||
- func NewFile(fd uintptr, name string) *File
|
||||
|
||||
Create a file and return a file object.
|
||||
|
||||
|
||||
There are also two functions to open files:
|
||||
|
||||
- func Open(name string) (file *File, err Error)
|
||||
|
||||
Opens a file called `name` with read-only access, calling `OpenFile` under the covers.
|
||||
|
||||
- func OpenFile(name string, flag int, perm uint32) (file *File, err Error)
|
||||
|
||||
Opens a file called `name`. `flag` is open mode like read-only, read-write, etc. `perm` are the file permissions.
|
||||
|
||||
### Write files
|
||||
|
||||
Functions for writing files:
|
||||
|
||||
- func (file *File) Write(b []byte) (n int, err Error)
|
||||
|
||||
Write byte type content to a file.
|
||||
|
||||
- func (file *File) WriteAt(b []byte, off int64) (n int, err Error)
|
||||
|
||||
Write byte type content to a specific position of a file.
|
||||
|
||||
- func (file *File) WriteString(s string) (ret int, err Error)
|
||||
|
||||
Write a string to a file.
|
||||
|
||||
Code sample:
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
userFile := "astaxie.txt"
|
||||
fout, err := os.Create(userFile)
|
||||
if err != nil {
|
||||
fmt.Println(userFile, err)
|
||||
return
|
||||
}
|
||||
defer fout.Close()
|
||||
for i := 0; i < 10; i++ {
|
||||
fout.WriteString("Just a test!\r\n")
|
||||
fout.Write([]byte("Just a test!\r\n"))
|
||||
}
|
||||
}
|
||||
|
||||
### Read files
|
||||
|
||||
Functions for reading files:
|
||||
|
||||
- func (file *File) Read(b []byte) (n int, err Error)
|
||||
|
||||
Read data to `b`.
|
||||
|
||||
- func (file *File) ReadAt(b []byte, off int64) (n int, err Error)
|
||||
|
||||
Read data from position `off` to `b`.
|
||||
|
||||
Code sample:
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
userFile := "asatxie.txt"
|
||||
fl, err := os.Open(userFile)
|
||||
if err != nil {
|
||||
fmt.Println(userFile, err)
|
||||
return
|
||||
}
|
||||
defer fl.Close()
|
||||
buf := make([]byte, 1024)
|
||||
for {
|
||||
n, _ := fl.Read(buf)
|
||||
if 0 == n {
|
||||
break
|
||||
}
|
||||
os.Stdout.Write(buf[:n])
|
||||
}
|
||||
}
|
||||
|
||||
### Delete files
|
||||
|
||||
Go uses the same function for removing files and directories:
|
||||
|
||||
- func Remove(name string) Error
|
||||
|
||||
Remove a file or directory called `name`.( *** a `name` ending with `/` signifies that it's a directory*** )
|
||||
|
||||
## Links
|
||||
|
||||
- [Directory](preface.md)
|
||||
- Previous section: [Templates](07.4.md)
|
||||
- Next section: [Strings](07.6.md)
|
||||
|
||||
Reference in New Issue
Block a user