package models

import (
	"fmt"
	"io"
	"os"
)

// 文件读写
type FileWR struct {
}

// 只读
func (this *FileWR) Open(path string) string {
	file, err := os.Open(path)
	defer file.Close()
	if err != nil {
		fmt.Println(err)
		return ""
	}
	result := []byte{}
	for {
		mm := make([]byte, 1024)
		_, errR := file.Read(mm)
		if errR != nil && errR == io.EOF {
			fmt.Println("读取完毕")
			break
		}
		result = append(result, mm...)
	}
	return string(result)
}

// 清空重新写入
func (this *FileWR) Put(path string, str string) bool {
	file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0666)
	defer file.Close()
	if err != nil {
		fmt.Println(err)
		return false
	}
	file.WriteString(str)
	return true
}
