1.相关api介绍
1.1建立File内存地址api
func Create(name string) (file *File, err Error)
func NewFile(fd int, name string) *File
1.2打开文件api
func Open(name string)(file *File,err Error)
func OpenFile(name string,flag int,perm unit32)(file *File,err Error)
1.3写文件api
func (file *File)Write(b []byte)(n int,err Error)
func (file *File)WriteAt(b []byte,off int64)(n int,err Error)
func (file *File) WriteString(s string)(ret int,err Error)
1.4读文件api
func (file *File) Read(b []byte)(n int, err Error)
func (file *File) ReadAt(b []byte,off int64)(n int,err Error)
1.5删除文件api
func Remove(name string) Error
2.写文件的实例代码
package main
import (
"fmt"
"os"
)
func main() {
userFile := "d:/test.txt"
fout,err := os.Create(userFile)
defer fout.Close()
if err != nil{
fmt.Println(userFile,err)
return
}
for i:=0;i<10;i++{
fout.WriteString("Hello world!\r\n")
fout.Write([]byte("abcd!\r\n"))
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
输出的文件内容:
Hello world!
abcd!
Hello world!
abcd!
Hello world!
abcd!
Hello world!
abcd!
Hello world!
abcd!
Hello world!
abcd!
Hello world!
abcd!
Hello world!
abcd!
Hello world!
abcd!
Hello world!
abcd!
3.读文件的实例代码
package main
import (
"fmt"
"os"
)
func main() {
userFile := "d:/test.txt"
fin,err := os.Open(userFile)
defer fin.Close()
if err != nil{
fmt.Println(userFile,err)
return
}
buf := make([]byte,1024)
for{
n,_ := fin.Read(buf)
if 0==n{
break
}
os.Stdout.Write(buf[:n])
}
}
- 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
- 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
运行结果:
G:\GoCodeHome\demo\bin>Go build demo4
G:\GoCodeHome\demo\bin>demo4
Hello world!
abcd!
Hello world!
abcd!
Hello world!
abcd!
Hello world!
abcd!
Hello world!
abcd!
Hello world!
abcd!
Hello world!
abcd!
Hello world!
abcd!
Hello world!
abcd!
Hello world!
abcd!
4.同时使用os.Open和os.Create操作文件
package main
import (
"io"
"os"
)
func main() {
fi, err := os.Open("d:/input.txt")
if err != nil { panic(err) }
defer fi.Close()
fo, err := os.Create("d:/output.txt")
if err != nil { panic(err) }
defer fo.Close()
buf := make([]byte, 1024)
for {
n, err := fi.Read(buf)
if err != nil && err != io.EOF { panic(err) }
if n == 0 { break }
if n2, err := fo.Write(buf[:n]); err != nil {
panic(err)
} else if n2 != n {
panic("error in writing")
}
}
}
- 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
- 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
5.使用bufio库
package main
import (
"bufio"
"io"
"os"
)
func main() {
fi, err := os.Open("input.txt")
if err != nil { panic(err) }
defer fi.Close()
r := bufio.NewReader(fi)
fo, err := os.Create("output.txt")
if err != nil { panic(err) }
defer fo.Close()
w := bufio.NewWriter(fo)
buf := make([]byte, 1024)
for {
n, err := r.Read(buf)
if err != nil && err != io.EOF { panic(err) }
if n == 0 { break }
if n2, err := w.Write(buf[:n]); err != nil {
panic(err)
} else if n2 != n {
panic("error in writing")
}
}
if err = w.Flush(); err != nil { panic(err) }
}
- 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
- 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
6.使用ioutil库
package main
import (
"io/ioutil"
)
func main() {
b, err := ioutil.ReadFile("input.txt")
if err != nil { panic(err) }
err = ioutil.WriteFile("output.txt", b, 0644)
if err != nil { panic(err) }
}
7.遍历文件夹
package main
import (
"path/filepath"
"os"
"fmt"
"flag"
)
func getFilelist(path string) {
err := filepath.Walk(path, func(path string, f os.FileInfo, err error) error {
if ( f == nil ) {return err}
if f.IsDir() {return nil}
println(path)
return nil
})
if err != nil {
fmt.Printf("filepath.Walk() returned %v\n", err)
}
}
func main(){
flag.Parse()
root := flag.Arg(0)
getFilelist(root)
}
- 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
- 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
运行结果:
![这里写图片描述](https://img-blog.csdn.net/20160330221416657)
From: http://blog.csdn.net/mchenys/article/details/50993849