Go
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
file,err :=os.OpenFile("D:/Kugou/nihao.txt",os.O_RDWR |os.O_APPEND | os.O_CREATE,0777)
if err != nil {
fmt.Println("打开失败了",err)
return
}
defer file.Close()
write := bufio.NewWriter(file)
for i :=0 ;i <5 ; i++ {
write.WriteString("你好 golang \n")
}
write.Flush()
}
os.OpenFile,OS包的方法打开文件,将程序的内容写入到缓存中,传给变量,来展示
Go
package main
import (
"fmt"
"io/ioutil"
)
func main() {
file1Path :="D:/Kugou/nihao.txt"
file2Path := "D:/Kugou/test.txt"
content,err := ioutil.ReadFile(file1Path)
if err != nil {
fmt.Println("有问题")
return
}
err = ioutil.WriteFile(file2Path,content,0777)
if err != nil {
fmt.Println("写失败了")
}
}
ioutil.ReadFile 读取文件,写入到变量,传入到路径下