Go IO操作基础知识

IO操作基础知识

IO操作,指代读写操作->os库
1.路径分析
绝对路径,如:D:/go/ch1/abc.jpg,->path:=“D:/go/ch1/abc.jpg”;
相对路径,针对当前工程目录,》.表示当前目录,…表示上一层。假设工程目录为D:/go/,->path:="./ch1/abc.jpg";
2.文件信息结构体
type FileInfo struct{
Name() string ->文件名
Size() int64 ->文件大小
Mode() FileMode ->文件权限
IsDir bool ->是否为目录
Sys() interface{} ->文件详细信息
ModTime() interface{} ->文件最后修改时间
}
3.文件权限
-rwxrwxrwx ->0777 ->第一个符号表示类型(-:文件;d:目录)

package main

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

1.文件信息
//Stat(name string) (FileInfo, error)

func printMessage(filepath string){
	fileInfo, err := os.Stat(filepath)//return (FileInfo,error)
	if err!=nil{
		fmt.Println("err:",err.Error())
	}else{
		fmt.Printf("数据类型:%T\n",fileInfo)
		fmt.Println("文件名:",fileInfo.Name())
		fmt.Println("是否为目录:",fileInfo.IsDir())
		fmt.Println("文件大小:",fileInfo.Size())
		fmt.Println("文件权限:",fileInfo.Mode())
		fmt.Println("文件最后修改时间:",fileInfo.ModTime())
		fmt.Println("文件:",fileInfo.Sys())
	}

}

2.创建文件(默认0666权限)
//Create(name string) (*File, error)

func creatFile(){
	filename:="./abc.txt"
	file,err:=os.Create(filename)//创建文件,有则覆盖
	if err!=nil{
		fmt.Println("err:",err.Error())
	}else{
		fmt.Printf("%s创建成功:%v\n",filename,file)
	}
}

3.打开与关闭文件
//OpenFile(name string, flag int, perm FileMode) (*File, error)》文件名,打开方式(见书p191),权限(文件不存在时创建,需指定)
//Open(name string) (*File, error)

func opencloseFile(){
	filename1:="./abc.txt"
	filename2:="./cba.txt"
	file1,err1:=os.Open(filename1)
	file2,err2:=os.OpenFile(filename2,os.O_RDWR|os.O_CREATE,0777)
	defer file1.Close()
	defer file2.Close()
	if err1!=nil{
		fmt.Println("err1:",err1.Error())
	}else{
		fmt.Printf("%s打开成功:%v\n",filename1,file1)
	}
	if err2!=nil{
		fmt.Println("err2:",err2.Error())
	}else{
		fmt.Printf("%s打开成功:%v\n",filename2,file2)
	}
}

4.删除文件
//Remove(name string) error ->仅删除文件和空目录
//RemoveAll(path string) error ->删除文件和目录下所有

func removeFile(){
	filename:="./cba.txt"
	err:=os.Remove(filename)
	if err!=nil{
		fmt.Println("err:",err.Error())
	}else{
		fmt.Printf("%s删除成功\n",filename)
	}
}

5.读取文件 ->打开-读取-关闭
//(f *File) Read(b []byte) (n int, err error)

func readFile(){
	filename:="./abcd"
	file,err:=os.Open(filename)
	defer file.Close()
	if err!=nil{
		fmt.Println("err:打开错误",err.Error())
	}else{
		bs:=make([]byte,1024,1024*2)
		n:=-1
		for {
			n,err=file.Read(bs)
			if n==0||err==io.EOF{
				fmt.Println("读取结束")
				break
			}
			fmt.Println(string(bs[:n]))
		}
	}

}

6.写入文件 ->打开或创建-写入-关闭
//(f *File) Write(b []byte) (n int, err error)
//(f *File) WriteString(s string) (n int, err error)

func writeFile(){
	file,err:=os.OpenFile("./abc.txt",os.O_RDWR|os.O_CREATE,0777)
	if err!=nil{
		fmt.Println("err:打开错误",err.Error())
	}else{
		n,err:=file.Write([]byte("I like travelling!"))
		if err!=nil{
			fmt.Println("err:写入错误",err.Error())
		}else {
			fmt.Println("写入成功:", n)
		}
		n,err=file.WriteString("中国人")
		if err!=nil{
			fmt.Println("err:写入错误",err.Error())
		}else {
			fmt.Println("写入成功:", n)
		}
	}

}

7.复制文件
//Copy(dst Writer, src Reader) (written int64, err error)

func copyFile(){
	arcfile:="./arc.txt"
	destfile:="./dest.txt"
	file1,err:=os.OpenFile(arcfile,os.O_RDWR|os.O_CREATE,0777)
	defer file1.Close()
	if err!=nil{
		fmt.Println("err:创建失败",err.Error())
	}else{
		fmt.Println("源文件创建成功")
		n,err1:=file1.Write([]byte("Today is Tuesday!"))
		if err1 !=nil{
			fmt.Println("err1:写入失败",err1.Error())
		}else{
			fmt.Println("源文件写入成功:",n)
			file2,err2:=os.OpenFile(destfile,os.O_RDWR|os.O_CREATE,0777)
			defer file2.Close()
			if err2!=nil{
				fmt.Println("err2:创建失败",err2.Error())
			}else{
				file3,err4:=os.Open(arcfile)
				fmt.Println("目标文件创建成功")
				total,err3:=io.Copy(file2,file3)
				defer file3.Close()
				if err3!=nil||err4!=nil{
					fmt.Println("err3,4:复制失败",err3.Error(),err4.Error())
				}else{
					fmt.Println("复制成功:",total)
				}
			}
		}

	}
}

主函数调用

func main() {
	//1.文件信息
	path:="C:/Users/LY/Pictures/abc.png"
	printMessage(path)
	/*数据类型:*os.fileStat
	文件名: abc.png
	是否为目录: false
	文件大小: 759564
	文件权限: -rw-rw-rw-
	文件最后修改时间: 2020-07-06 15:08:02.4296976 +0800 CST
	文件: &{32 {734480284 30823268} {1727834270 30823422} {808453648 30823268} 0 759564}*/
	//2.创建文件
	creatFile()//./abc.txt创建成功:&{0xc0000cc780}
	//3.打开关闭文件
	opencloseFile()
	/*./abc.txt打开成功:&{0xc000096280} ./cba.txt打开成功:&{0xc000096500}*/
	//4.删除文件
	removeFile()//./cba.txt删除成功
	//5.读取文件
	readFile()
	//6.写入文件
	writeFile()
	//7.复制文件
	copyFile()
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值