猿码记

V1

2023/02/14阅读:13主题:橙心

Go学习(二十四):文件操作

1.介绍

Go语言中,文件操作的大多数函数都在os包里面,文件的信息包括文件名、文件大小、修改权限、修改时间等。

1.1 文件接口属性(FileInfo)

type FileInfo interface {
 Name() string       // 文件名
 Size() int64        // 文件大小
 Mode() FileMode     // 修改权限
 ModTime() time.Time // 最后修改时间
 IsDir() bool        // 判断是否是目录
 Sys() interface{}   // 文件的详细信息,获取的值是*syscall.Stat_t类型 
}

1.2 文件信息结构体(fileStat)

type fileStat struct {
 name    string
 size    int64
 mode    FileMode
 modTime time.Time
 sys     syscall.Stat_t
}
1.fileStat结构体的常用方法汇总
方法名 描述
Name() string 返回文件名
IsDir() bool 判断是否是目录
Size() int64 返回文件大小
Mode() FileMode 返回文件权限
ModTime() time.Time 文件的最后修改时间
Sys() interface{} 文件的详细信息,获取的值是*syscall.Stat_t类型
2.使用示例
package main
import (
 "fmt"
 "os"
)
func main(){
 fp := "./public/test1.txt"
 fileInfo, err := os.Stat(fp)
 if err != nil {
  fmt.Println(err.Error())
  return
 }
 fmt.Printf("类型: %T\n",fileInfo)
 fmt.Printf("文件名: %v\n",fileInfo.Name())
 fmt.Printf("文件大小: %v\n",fileInfo.Size())
 fmt.Printf("是否为目录: %v\n",fileInfo.IsDir())
 fmt.Printf("文件权限: %v\n",fileInfo.Mode())
 fmt.Printf("文件最后修改的时间: %v\n",fileInfo.ModTime())
}
/**输出
类型: *os.fileStat
文件名: test1.txt
文件大小: 11
是否为目录: false
文件权限: -rw-r--r--
文件最后修改的时间: 2021-01-15 16:29:53.816034553 +0800 CST
sys: &{Dev:16777220 Mode:33188 Nlink:1 Ino:15440327 Uid:501 Gid:20 Rdev:0 Pad_cgo_0:[0 0 0 0] Atimespec:{Sec:1610699394 Nsec:591237565} Mtimespec:{Sec:1610699393 Nsec:816034553} Ctimespec:{Sec:1610699393 Nsec:816034553} Birthtimespec:{Sec:1610611801 Nsec:544367649} Size:11 Blocks:8 Blksize:4096 Flags:0 Gen:0 Lspare:0 Qspare:[0 0]}
*/

2.求路径方法

2.1 方法列表

方法的所属包: path/filepath

方法名 描述
IsAbs(path string) bool 判断是否是绝对路径
Rel(basepath, targpath string) (string, error) 返回一个相对于basepath的路径
Abs(path string) (string, error) 返回绝对路径
Join(elem ...string) string 拼接路径

2.2 使用示例

package main
import (
 "fmt"
 "path"
 "path/filepath"
)
func main(){
 basePath := "/Users/hui/Project/Go/src/study"
 fp1 := "./public/test1.txt"
 fp2 := "/Users/hui/Project/Go/src/study/public/static/test2.txt"
 fmt.Printf("是绝对路径?: %t\n",filepath.IsAbs(fp1))
 // 相对路径
 rel, err := filepath.Rel(basePath, fp2)
 if err != nil {
  fmt.Println(err.Error())
 }
 fmt.Printf("fp1相对fp2的路径?:  %v\n",rel)
 // 绝对路径
 abs, err := filepath.Abs(fp1)
 if err != nil {
  fmt.Println(err.Error())
 }
 fmt.Printf("fp1的绝对路径?:  %s\n",abs)
 // 拼接路径
 fmt.Printf("拼接路径1: %s \n",path.Join("/home/Item","go","study"))
 fmt.Printf("拼接路径2: %s \n",path.Join("/home/图片","美女","beauty.jpg"))
}
/** 输出
是绝对路径?: false
fp1相对fp2的路径?:  public/static/test2.txt
fp1的绝对路径?: /Users/hui/Project/Go/src/study/public/test1.txt
拼接路径1: /home/Item/go/study 
拼接路径2: /home/图片/美女/beauty.jpg 
*/

3.目录操作

3.1 创建目录

1.方法列表
方法 描述
os.Mkdir(name string, perm FileMode) error 创建名称为name的目录,权限设置是perm
os.MkdirAll(path string, perm FileMode) error 根据path创建多级子目录
2.使用示例
package main
import (
 "fmt"
 "os"
)
func main(){
 dirName := "./img"
 // 创建目录,并分配权限
 err := os.Mkdir(dirName, os.ModePerm)
 if err != nil {
  fmt.Println(err.Error())
 }
 fmt.Println( dirName+ "  创建成功!")
 // 创建多级目录
 dirName2 := "./public/static/css"
 err = os.MkdirAll(dirName2,os.ModePerm)
 if err != nil {
  fmt.Println(err.Error())
 }
 fmt.Println( dirName2+ "  创建成功!")
}

3.2 判断目录是否存在

// 判断文件或者目录是否存在
func pathIsExist(pathName string) (bool,error) {
 // 获取文件或目录信息
 _, err := os.Stat(pathName)
 // 如果文件或目录存在,则err== nil
 if err == nil {
  return truenil
 }
 // 使用os.IsNotExist()判断返回的错误类型是否为true,true:说明文件或文件夹不存在
 if os.IsNotExist(err) {
  return false,nil
 }
 // 如果错误类型为其它,则不确定是否在存在
 return false,err
}

3.3 删除目录

1.方法列表
方法 描述
os.Remove(name string) error 删除名称为name的目录,当目录下有文件或者其他目录会出错
os.RemoveAll(path string) error 根据path删除多级子目录
2.使用示例
package main
import (
 "fmt"
 "os"
)
func main(){
 // 删除目录
 err := os.Remove("./file")
 if err != nil {
  fmt.Println("Remove-> 删除失败: " + err.Error())
 } else {
  fmt.Println("Remove-> 删除成功!" )
 }
 // 删除多级子目录
 err2 := os.RemoveAll("./file/static")
 if err2 != nil {
  fmt.Println("RemoveAll-> 删除失败: " + err2.Error())
 } else {
  fmt.Println("RemoveAll-> 删除成功!" )
 }
}

4.文件操作

4.1 创建文件

os.Create()创建文件,如果文件存在,会将其覆盖。os.Create()本质上调用的是OpenFile,源码如下:

func Create(name string) (*File, error) {
 return OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666)
}
使用示例
package main
import (
 "fmt"
 "os"
)
func main(){
 fileName := "./file/a.txt"
    //如果文件存在,则会覆盖
 create, err := os.Create(fileName)
 if err != nil {
  fmt.Println("创建失败: " + err.Error())
  return
 }
 fmt.Printf("创建成功! %v\n",create)
}

4.2 打开和关闭文件

1.方法列表
方法名 描述
Open(name string) (*File, error) 只读模式打开文件,本质上调用的是OpenFile
OpenFile(name string, flag int, perm FileMode) (*File, error) 打开文件,可以传入模式和权限
2.OpenFile入参介绍
flag值 代表的模式
O_RDONLY 只读模式
O_WRONLY 只写模式
O_RDWR 读写模式
O_APPEND 追加模式
O_CREATE 文件不存在,则创建
O_EXCL O_CREATE配合使用,文件必须不存在
O_SYNC 打开文件用于同步I/O
O_TRUNC 如果可能,打开时清空文件
3.使用示例
package main
import (
 "fmt"
 "os"
)
func main(){
 // 该文件不存在
 fileName := "./public/test.txt"
 file1, err := os.Open(fileName)
 if err != nil {
  fmt.Printf("os.Open 打开文件失败,err:%s \n" , err.Error())
 } else  {
  fmt.Printf("os.Open 打开文件成功,open:%v \n" , file1)
  // 关闭文件
  defer file1.Close()
 }
 // 文件不存在,则会创建文件
 file2, err1 := os.OpenFile(fileName,os.O_CREATE,os.ModePerm)
 if err1 != nil {
  fmt.Printf("os.OpenFile 打开文件失败,err:%s \n" , err1.Error())
 } else  {
  fmt.Printf("os.OpenFile 打开文件成功,open:%v \n" , file2)
  // 关闭文件
  defer file2.Close()
 }
}
/**输出
os.Open 打开文件失败,err:open ./public/test.txt: no such file or directory 
os.OpenFile 打开文件成功,open:&{0xc0000b0120} 
*/

4.3 读取文件

读取文件步骤: 打开文件(Open) 、读取文件(Read)、关闭文件(Close)

package main
import (
 "fmt"
 "os"
)
func main(){
 fileName := "./public/test.txt"
 file1, err := os.Open(fileName)
 if err != nil {
  fmt.Printf("os.Open 打开文件失败,err:%s \n" , err.Error())
  return
 }
 // 定义一个变量存储读取的内容
 b := make([]byte,1024)
 // 读取文件
 for  {
  read, _ := file1.Read(b)
  if read == 0 {
   break
  }
 }
 fmt.Printf("读取内容: %s \n",b)
 // 关闭文件
 _ = file1.Close()
}
// 输出: 读取内容: Hello Word! 您好,世界!

4.4 写入文件

写入文件步骤: 打开文件(OpenFile) 、写入文件(Write)、关闭文件(Close)

package main
import (
 "fmt"
 "os"
)
func main(){
 fileName := "./public/hello.txt"
 // 读写|创建|追加的模式 模式打开文件
 file1, err := os.OpenFile(fileName,os.O_RDWR | os.O_CREATE | os.O_APPEND ,os.ModePerm )
 if err != nil {
  fmt.Printf("os.OpenFile 打开文件失败,err:%s \n" , err.Error())
  return
 }
 // 写入文件
 write, err := file1.Write([]byte("Go! Go! Let's Go!! 汉字 \n"))
 if err != nil {
  fmt.Printf("写入失败: %s \n",err.Error())
 }
 fmt.Printf("写入字节数: %d \n",write)
 // 关闭文件
 _ = file1.Close()
}

4.5 复制文件

Go语言提供了io.copy()方法,用来复制文件。

package main
import (
 "fmt"
 "io"
 "os"
)
func main(){
 // 把fileA文件复制给fileB
 fileA := "./public/hello.txt"
 fileB := "./public/new.txt"
 // 打开源文件
 sourceA, err := os.Open(fileA)
 if err != nil {
  fmt.Printf("os.Open 打开文件失败,err:%s \n" , err.Error())
  return
 }
 // 关闭文件A
 defer sourceA.Close()
 // 打开新文件
 targetB, err := os.Create(fileB)
 if err != nil {
  fmt.Printf("os.Create 创建新文件失败,err:%s \n" , err.Error())
  return
 }
 // 关闭文件B
 defer targetB.Close()
 // 把fileA文件复制给fileB
 written, err := io.Copy(targetB, sourceA)
 if err != nil {
  fmt.Printf("文件复制失败,err:%s \n" , err.Error())
  return
 }
 fmt.Printf("文件复制成功: %d \n" ,written)
}

4.6 删除文件

3.3 删除目录

分类:

后端

标签:

后端

作者介绍

猿码记
V1