goland读取文件写入文件

package main

import (
"fmt"
"io/ioutil"
"strings"
)
/**
* 读取文件
* string name 文件名称
*/
func ReadFile(name string) {
if contents,err := ioutil.ReadFile(name);err == nil {
result := strings.Replace(string(contents),"\n","",1)
fmt.Println("ReadFile:",result)
}
}
/**
* 写入文件
* string name 文件名称
* byte content 文件内容
*/
func WriteFile(name string, content []byte){
if ioutil.WriteFile(name, content, 0644) == nil{
fmt.Println("WriteFile")
}else{
fmt.Println("NOT WriteFile")
}
}

func main() {
name := "content.txt"
content := "content"
WriteFile(name, []byte(content))
ReadFile(name)
}

猜你喜欢

转载自www.cnblogs.com/cc-forever/p/11742007.html
今日推荐