当前位置:网站首页>File creation, writing, reading, deletion (transfer) in go language

File creation, writing, reading, deletion (transfer) in go language

2022-07-04 14:10:00 Lucky little yellow man

from :go File creation in language , write in , Read , Delete

1、 Open file

package main;
 
import (
    "os"
    "fmt"
    "strconv"
)
 
func main() {
 
    // Open file , Return file pointer 
    file, error := os.Open("./1.txt");
    if error != nil {
        fmt.Println(error);
    }
    fmt.Println(file);
    file.Close();
 }
}

2、 Read and write to open the file , If it doesn't exist , Create

// Open the file read-write , If it doesn't exist , Create 
    file2, error := os.OpenFile("./2.txt", os.O_RDWR|os.O_CREATE, 0766);
    if error != nil {
        fmt.Println(error);
    }
    fmt.Println(file2);
    file2.Close();

3、 create a file

// create a file 
    //Create Functions are also called OpenFile
    file3, error := os.Create("./3.txt");
    if error != nil {
        fmt.Println(error);
    }
    fmt.Println(file3);
    file3.Close();

4、 Read file contents

// Read file contents 
    file4, error := os.Open("./1.txt");
    if error != nil {
        fmt.Println(error);
    }
    // establish byte Of slice Used to receive file read data 
    buf := make([]byte, 1024);
    // Cyclic reading 
    for {
        //Read Function will change the current offset of the file 
        len, _ := file4.Read(buf);
 
        // The number of bytes read is 0 Jump out of the loop 
        if len == 0 {
            break;
        }
 
        fmt.Println(string(buf));
    }
    file4.Close();

5、 Specified offset , Read file contents

// Read file contents 
    file5, error := os.Open("./1.txt");
    if error != nil {
        fmt.Println(error);
    }
    buf2 := make([]byte, 1024);
    ix := 0;
    for {
        //ReadAt Read from the specified offset , Does not change the file offset 
        len, _ := file5.ReadAt(buf2, int64(ix));
        ix = ix + len;
        if len == 0 {
            break;
        }
 
        fmt.Println(string(buf2));
    }
    file5.Close();

6、 write file

// write file 
    file6, error := os.Create("./4.txt");
    if error != nil {
        fmt.Println(error);
    }
    data := " I'm data \r\n";
    for i := 0; i < 10; i++ {
        // write in byte Of slice data 
        file6.Write([]byte(data));
        // Write string 
        file6.WriteString(data);
    }
    file6.Close();

7、 Specify offset to write to file

// write file 
    file7, error := os.Create("./5.txt");
    if error != nil {
        fmt.Println(error);
    }
    for i := 0; i < 10; i++ {
        // Write data at the specified offset 
        ix := i * 64;
        file7.WriteAt([]byte(" I'm data "+strconv.Itoa(i)+"\r\n"), int64(ix));
    }
    file7.Close();

8、 Delete files and delete folders

// Delete file 
    del := os.Remove("./1.txt");
    if del != nil {
        fmt.Println(del);
    }
 
    // Delete the specified path All files under 
    delDir := os.RemoveAll("./dir");
    if delDir != nil {
        fmt.Println(delDir);

from :go File creation in language , write in , Read , Delete

原网站

版权声明
本文为[Lucky little yellow man]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041105289846.html

随机推荐