Package main

Import(

BufIO' //缓存IO

“Fmt”

“输入/输出”

Io/ioutil' //io套件

“操作系统”

)。

Func check(e error) {

If e!=nil {

Panic(e)

}

}

/* *

*检查文件是否存在返回true无返回false

*/

Func checkfileisexist(文件名字符串)bool {

Var exist=true

If _,err:=os.stat(文件名);操作系统。IsNotExist(err) {

Exist=false

}

Return exist

}

/* *

From : 3358 www . oicqzonne.com/

*/

Func main() {

var wire testring=' test n '

Var filename='。/output1.txt '

Var f *os。File

Var err1错误

/* * * * * * * * * * * * * *种方法3360是io。使用WriteString写入* *文件

If checkfileisexist(文件名){//文件存在。

f,err1=os.openfile(文件名,os.o _ append,0666)//打开文件

Fmt。Println(“文件存在”)

} else {

f,err1=os.create(文件名)//创建文件

Fmt。Println(“文件不存在”)

}

检查(err1)

n,err 1 3360=io . write string(f,wire testring)//写入文件(字符串)

检查(err1)

Fmt。Printf(“写%d字节n”,n)

/* * * * * * * * * * * *第二种方法3360是ioutil。使用WriteFile写入*文件

Vard1=[] byte(导线测试)

Err2 :=ioutil.writefile('./output2.txt ',d1,0666)//写入文件(字节数组)

检查(err2)

/* * * * * * * * * * * *第三种方法3360是文件(write,writestes)

f,err3 :=os.create('./output3.txt')//创建文件

检查(err3)

Defer f.Close()

N2,err 3 3360=f . Write(D1)//写入文件(字节数组)

检查(err3)

Fmt。Printf(“写%d字节n”,N2)

N3,err 3 3360=f . writestring(' writesn ')//写入文件(字节数组)

Fmt。Printf(“写%d字节n”,n3)

F.Sync()

/* * * * * * * * * * * * * *第四种方法3360是bufio .使用NewWriter创建文件*

W :=bufio。NewWriter(f) //新建写入程序对象

N4,ERR 3 3360=w . Writestring(' Buffered N ')

Fmt。Printf(“写%d字节n”,n4)

W.Flush()

F.Close()

}