一般情况下,ioutil库读取文件的效率最高,bufio库次之,直接使用文件的Read加上buffer的方法效率最低。但对于大文件,设置相同大小的buffer有时bufio反而会更慢。
func LoadFile(filename string) []byte {
buf, err := ioutil.ReadFile(filename) //读取全部内容
if err != nil {
fmt.Println("loadFile readfile err:", err)
return nil
} else {
return buf
}
}
func FileIsExisted(filename string) bool {
existed := true
if _, err := os.Stat(filename); os.IsNotExist(err) {
existed = false
}
return existed
}
func MakeDir(dir string) error {
if !FileIsExisted(dir) {
if err := os.MkdirAll(dir, 0777); err != nil { //os.ModePerm
fmt.Println("MakeDir failed:", err)
return err
}
}
return nil
}
func SaveFile(filename string, data []byte) bool {
var err error
if len(filename) > 0 && data != nil {
dir := filepath.Dir(filename)
if MakeDir(dir) != nil {
return false
}
err = ioutil.WriteFile(filename, data, 0666)
if err != nil {
fmt.Println("SaveFile err:", err)
} else {
return true
}
} else {
fmt.Println("SaveFile err: wrong params")
}
return false
}
func main() {
filename := "release.py"
file, err := os.OpenFile(filename, os.O_RDONLY, 0666)
if err != nil {
fmt.Println("Fail to open file:", err)
}
defer file.Close()
bufReader := bufio.NewReader(file)
buf := make([]byte, 1024)
for {
n, err := bufReader.Read(buf)
if err != nil {
if err == io.EOF {
fmt.Println("Read EOF")
} else {
fmt.Println("Read failed:", err)
}
break
}
fmt.Println(string(buf[:n]))
}
}
func main() {
filename := "release.py"
file, err := os.OpenFile(filename, os.O_RDONLY, 0666)
if err != nil {
fmt.Println("Fail to open file:", err)
}
defer file.Close()
buf := bufio.NewReader(file)
for {
line, err := buf.ReadString('\n')
line = strings.TrimSpace(line)
if len(line) > 0 {
fmt.Println(line)
}
//判断错误
if err == io.EOF {
fmt.Println("Read EOF")
break
} else if err != nil {
fmt.Println("Read error:", err)
break
}
}
}
func main() {
filename := "demo.txt"
file, err := os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666)
if err != nil {
fmt.Println("Fail to open file:", err)
}
defer file.Close()
bufReader := bufio.NewWriter(file)
bufReader.WriteString("This is the 1st line\n")
bufReader.WriteString("This is the 2nd line\n")
bufReader.WriteString("This is the 3rd line\n")
bufReader.Flush() //不执行Flush,内容不会完全保存在文件中
}
func main() {
filename := "demo.txt"
file, err := os.Open(filename)
if err != nil {
fmt.Println("Fail to open file:", err)
}
defer file.Close()
buf := make([]byte, 1024)
for {
n, err := file.Read(buf)
if err == io.EOF {
fmt.Println("Read EOF")
break
} else if err != nil {
fmt.Println("Read error:", err)
break
}
fmt.Println(string(buf[:n]))
}
}
var LOG_FILENAME = filepath.Join("log", "log.txt")
func FileIsExisted(filename string) bool {
existed := true
if _, err := os.Stat(filename); os.IsNotExist(err) {
existed = false
}
return existed
}
func MakeDir(dir string) error {
if !FileIsExisted(dir) {
if err := os.MkdirAll(dir, 0777); err != nil { //os.ModePerm
fmt.Println("MakeDir failed:", err)
return err
}
}
return nil
}
func LogsWrite(text string) {
dir := filepath.Dir(LOG_FILENAME)
if !FileIsExisted(dir) {
MakeDir(dir)
}
f, err := os.OpenFile(LOG_FILENAME, os.O_APPEND | os.O_CREATE | os.O_WRONLY, 0666)
if err == nil {
defer f.Close()
text = fmt.Sprintf("%s: %s\n", time.Now().Format("2006-01-02 15:04:05"), text)
f.Write([]byte(text))
}
}