当前位置: 移动技术网 > IT编程>脚本编程>Go语言 > Go语言的文件操作代码汇总

Go语言的文件操作代码汇总

2018年11月08日  | 移动技术网IT编程  | 我要评论

# 建立与打开文件

// 新建文件可以通过如下两个方法:

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信息到文件

// 读文件

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中

 // 删除文件

func remove(name string) error

调用该函数就可以删除文件名为name的文件

////关闭文件

func (file *file)close()

写文件

// code_034_os_write_to_file project main.go
package main

import (
  "fmt"
  "os"
)

func main() {

  //新建文件
  fout, err := os.create("./createfile.txt")
  if err != nil {
    fmt.println(err)
    return
  }
  defer fout.close()

  for i := 0; i < 5; i++ {
    //备注:windows环境下,结尾\r\n才能换行,linux下\n就可以
    outstr := fmt.sprintf("%s:%d\r\n", "hello go", i) //sprintf控制台输出,并有返回值string
    // 写入文件
    fout.writestring(outstr)             //string信息
    fout.write([]byte("abcd\r\n"))          //byte类型
  }
}

读文件

// code_035_os_read_from_file project main.go
package main

import (
  "fmt"
  "os"
)

func main() {
  fin, err := os.open("./open_and_read1.txt")
  if err != nil {
    fmt.println(err) //若文件不存在:the system cannot find the file specified.
  }
  defer fin.close()

  buf := make([]byte, 1024) //创建存储slice
  for {
    n, _ := fin.read(buf) //读文件
    if n == 0 {
      break
    }
    fmt.println(string(buf))
  }

}

拷贝文件----》(备注:已经创建并写入内容的local_copy_file.txt)终端切换到当前目录下,执行 go run main.go local_copy_file.txt dst_file.txt

// code_036_os_copy_file project main.go
package main

import (
  "fmt"
  "io"
  "os"
)

func main() {
  // 使用命令行提高拷贝的复用性
  args := os.args
  if args == nil || len(args) != 3 {
    fmt.println("useage : go filename.go src file dstfile")
    return
  }

  srcpath := args[1]
  dstpath := args[2]
  fmt.printf("srcpath = %s, dstpath = %s\r\n", srcpath, dstpath)

  if srcpath == dstpath {
    fmt.println("源文件和目标文件不能重名")
  }

  //执行复制
  srcfile, err1 := os.open(srcpath)
  if err1 != nil {
    fmt.println(err1)
    return
  }
  dstfile, err2 := os.create(dstpath)
  if err2 != nil {
    fmt.println(err2)
    return
  }
  read_buf := make([]byte, 1024)
  for {
    //读取文件
    n, err := srcfile.read(read_buf) //每次文件读取字节的长度
    if err != nil && err != io.eof {
      fmt.println(err)
      break
    }
    if n == 0 {
      fmt.println("文件处理完毕")
      break
    }
    //写入目的文件

    write_buf := read_buf[:n]
    dstfile.write(write_buf)
  }
  // 关闭文件
  srcfile.close()
  dstfile.close()

}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网