diff --git a/fileutil/file.go b/fileutil/file.go index 26eae3d..dc2dc85 100644 --- a/fileutil/file.go +++ b/fileutil/file.go @@ -5,6 +5,7 @@ package fileutil import ( + "bufio" "errors" "io" "io/ioutil" @@ -75,6 +76,53 @@ func CopyFile(srcFilePath string, dstFilePath string) error { } } +//ClearFile write empty string to path file +func ClearFile(path string) error { + f, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777) + if err != nil { + return err + } + defer f.Close() + + _, err = f.WriteString("") + return err +} + +//ReadFileToString return string of file content +func ReadFileToString(path string) (string, error) { + bytes, err := ioutil.ReadFile(path) + if err != nil { + return "", err + } + return string(bytes), nil +} + +// ReadFileByLine +func ReadFileByLine(path string)([]string, error) { + f, err := os.Open(path) + if err != nil { + return nil, err + } + defer f.Close() + + res := make([]string, 0) + buf := bufio.NewReader(f) + + for { + line, _, err := buf.ReadLine() + l := string(line) + if err == io.EOF { + break + } + if err != nil { + continue + } + res = append(res, l) + } + + return res, nil +} + // ListFileNames return all file names in the path func ListFileNames(path string) ([]string, error) { if !IsExist(path) { diff --git a/fileutil/file_test.go b/fileutil/file_test.go index 3de883c..462867b 100644 --- a/fileutil/file_test.go +++ b/fileutil/file_test.go @@ -96,5 +96,43 @@ func TestListFileNames(t *testing.T) { utils.LogFailedTestInfo(t, "ToChar", "./", expected, filesInCurrentPath) t.FailNow() } - } + +func TestReadFileToString(t *testing.T) { + path := "./text.txt" + CreateFile(path) + f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777) + f.WriteString("hello world") + + res, _ := ReadFileToString(path) + if res != "hello world" { + utils.LogFailedTestInfo(t, "ReadFileToString", path, "hello world", res) + } +} + +func TestClearFile(t *testing.T) { + path := "./text.txt" + CreateFile(path) + f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777) + f.WriteString("hello world") + + CreateFile(path) + + res, _ := ReadFileToString(path) + if res != "" { + utils.LogFailedTestInfo(t, "CreateFile", path, "", res) + } +} + +func TestReadFileByLine(t *testing.T) { + path := "./text.txt" + CreateFile(path) + f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777) + f.WriteString("hello\nworld") + + expected := []string{"hello", "world"} + res, _ := ReadFileByLine(path) + if !reflect.DeepEqual(res, expected) { + utils.LogFailedTestInfo(t, "ReadFileByLine", path, expected, res) + } +} \ No newline at end of file diff --git a/slice/slice_test.go b/slice/slice_test.go index 20699db..044b0c2 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -378,11 +378,6 @@ func TestUpdateByIndex(t *testing.T) { r3 := []string{"a", "b", "1"} updateByIndex(t, t1, 2, "1", r3) - //failed - //t1 = []string{"a","b","c"} - //r4 := []string{"a", "b", "1"} - //updateByIndex(t, t1, 3, "1", r4) - } func updateByIndex(t *testing.T, test interface{}, index int, value, expected interface{}) {