0
点赞
收藏
分享

微信扫一扫

go文件&&目录操作

一:常规文件

1.go读取文件第一种做法【案例就是复制文件】

filePath:="./show.txt"
copyFilepath:="./copy.txt"
file,_:=os.Open(filePath)
defer file.Close()
buf:=make([]byte,1024)
for {
_,err:=file.Read(buf)
if err == io.EOF {
break;
}
copyfile,_:= os.OpenFile(copyFilepath,os.O_APPEND|os.O_CREATE,666)//复制文件给另一个
copyfile.Write(buf)
}

2.go读取完人家第二种做法

//读取文件第二种办法 bufio
filePath:="./show.txt"
file,_:=os.OpenFile(filePath,os.O_CREATE|os.O_APPEND,666)
buf:=make([]byte,1024)
newReader:=bufio.NewReader(file)
n,_:=newReader.Read(buf)
fmt.Println(string(buf[:n]))

3.go读取文件第三种做法

//读取文件第三中办法
filePath:="./show.txt"
buf,err:=ioutil.ReadFile(filePath)
if err != nil {
fmt.Println("read file is error")
}
fmt.Println(string(buf))

 二:json文件

 1.读取json文件【简单案例】

package main
import (
"encoding/json"
"fmt"
"io/ioutil"
)
type Persion struct {
Name string
Age int
Sex int
}
func main(){
p1:= Persion{}
filePath:="./t1.json"
content,_:=ioutil.ReadFile(filePath)
json.Unmarshal(content,&p1)
fmt.Println(p1.Name)
fmt.Println(p1.Age)
fmt.Println(p1.Sex)
}

   2.复杂一点

//这是json文件
{
"name":"lisi",
"age": 20,
"sex": 1,
"company":{
"name": "测试信息",
"address": "测试地址",
"tel": "1231312312312"
}
}

 

//这是代码
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
)
type Company struct {
Name string
Address string
Tel string
}
type Persion struct {
Name string
Age int
Sex int
Company Company
}
//如果json文件有嵌套的话
func main(){
p1:= Persion{}
filePath:="./t1.json"
content,_:=ioutil.ReadFile(filePath)
fmt.Println(string(content))
json.Unmarshal(content,&p1)
fmt.Println(p1.Name)
fmt.Println(p1.Age)
fmt.Println(p1.Sex)
fmt.Println(p1.Company.Name)
}

 3.生成json文件

package main

import (
"encoding/json"
"io/ioutil"
)
type Company struct {
Name string
Address string
Tel string
}
type Persion struct {
Name string
Age int
Sex int
Company Company
}
//如果json文件有嵌套的话
func main(){
p1 :=&Persion{}
p1.Name="lisi"
p1.Age=20
p1.Sex=1
p1.Company.Name="测试名字"
p1.Company.Address="测试地址信息"
p1.Company.Tel="130011666"
targetFile := "./per.json"
data,_:=json.Marshal(p1)
ioutil.WriteFile(targetFile,data,666)
}

  三:日志文件

  描述:go记录日志方式使用包【log】里面有三个比较重要的方法print,Panicln,Fatalln使用方式如下

        1.print方法使用

filePath:="./log.txt"
file,_:= os.OpenFile(filePath,os.O_APPEND|os.O_CREATE,666)
no:=[]int{6,8,10}
log.SetOutput(io.Writer(file))
defer file.Close()
log.Print(no)
//打日志,程序继续向下执行

2.Panic函数

filePath:="./log.txt"
file,_:= os.OpenFile(filePath,os.O_APPEND|os.O_CREATE,666)
no:=[]int{6,8,10}
log.SetOutput(io.Writer(file))
defer file.Close()
log.Panic(no)
//打日志,报错,程序执行到这里终止执行

3.Fatal函数

filePath:="./log.txt"
file,_:= os.OpenFile(filePath,os.O_APPEND|os.O_CREATE,666)
no:=[]int{6,8,10}
log.SetOutput(io.Writer(file))
defer file.Close()
log.Fatal(no)
//打日志,不报错 程序执行到这里终止执行

 

go目录操作

  1、创建单层目录

  mkdir(name,perm)权限  

dir:="./mkdir"
err:=os.Mkdir(dir,0777)
if err != nil {
fmt.Println("create dir is error")
}

 2、创建多层目录

path:="./aa/bb/cc"
err:=os.MkdirAll(path,0777)
if err != nil {
fmt.Println("create is error")
}

     3.根据时间创建目录 

uploadifle:="./upload/file/"+time.Now().Format("2006/01/02/")

_,err:=os.Stat(uploadifle) if err!= nil { os.MkdirAll(uploadifle,0777)
}

 

二:.go重命名目录和删除目录

go文件&&目录操作_读取文件

oldPath:= "upload"
newPath:="newupload"
err:=os.Rename(oldPath,newPath)
if err != nil {
fmt.Println(err)
}
path:="newupload"
err:=os.Remove(path)//如果是递归目录,删除失败
err:=os.RemoveAll(path) //是否递归目录一次性全部删除
if err != nil {
fmt.Println("is error")
}

go文件&&目录操作_读取文件

 三:遍历目录  

go文件&&目录操作_读取文件

func main(){
path:="./upload_walk/upload"+time.Now().Format("2006/01/02")
_,err:= os.Stat(path)
if err != nil {
os.MkdirAll(path,0777)//创建目录
}
rootpath:="./upload_walk"
filepath.Walk(rootpath,readdir)
}
func readdir(path string, f os.FileInfo, err error) error{
fmt.Println(path)
return nil
}

go文件&&目录操作_读取文件

 

举报

相关推荐

0 条评论