You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
893 B
Go
55 lines
893 B
Go
package monkey
|
|
|
|
import (
|
|
"bufio"
|
|
"errors"
|
|
"os"
|
|
)
|
|
|
|
func HasDir(path string) (bool, error) {
|
|
_, err := os.Stat(path)
|
|
if err == nil {
|
|
return true, nil
|
|
}
|
|
if os.IsNotExist(err) {
|
|
return false, nil
|
|
}
|
|
return false, err
|
|
}
|
|
|
|
func CreateDir(path string) error {
|
|
exist, err := HasDir(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if exist {
|
|
return errors.New("文件夹已存在")
|
|
} else {
|
|
err := os.Mkdir(path, os.ModePerm)
|
|
if err != nil {
|
|
return err
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
|
|
func CreateAndWriteFile(file_path string, data []byte) error {
|
|
file, err := os.OpenFile(file_path, os.O_WRONLY|os.O_CREATE, 0755)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer file.Close()
|
|
|
|
write := bufio.NewWriter(file)
|
|
_, err = write.Write(data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = write.Flush()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|