posts - 404,  comments - 115,  views - 118万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main;
 
import (
    "os"
    "fmt"
    "strconv"
)
 
func main() {
 
    //打开文件,返回文件指针
    file, error := os.Open("./1.txt");
    if error != nil {
        fmt.Println(error);
    }
    fmt.Println(file);
    file.Close();
 
    //以读写方式打开文件,如果不存在,则创建
    file2, error := os.OpenFile("./2.txt", os.O_RDWR|os.O_CREATE, 0766);
    if error != nil {
        fmt.Println(error);
    }
    fmt.Println(file2);
    file2.Close();
 
    //创建文件
    //Create函数也是调用的OpenFile
    file3, error := os.Create("./3.txt");
    if error != nil {
        fmt.Println(error);
    }
    fmt.Println(file3);
    file3.Close();
 
    //读取文件内容
    file4, error := os.Open("./1.txt");
    if error != nil {
        fmt.Println(error);
    }
    //创建byte的slice用于接收文件读取数据
    buf := make([]byte, 1024);
    //循环读取
    for {
        //Read函数会改变文件当前偏移量
        len, _ := file4.Read(buf);
 
        //读取字节数为0时跳出循环
        if len == 0 {
            break;
        }
 
        fmt.Println(string(buf));
    }
    file4.Close();
 
    //读取文件内容
    file5, error := os.Open("./1.txt");
    if error != nil {
        fmt.Println(error);
    }
    buf2 := make([]byte, 1024);
    ix := 0;
    for {
        //ReadAt从指定的偏移量开始读取,不会改变文件偏移量
        len, _ := file5.ReadAt(buf2, int64(ix));
        ix = ix + len;
        if len == 0 {
            break;
        }
 
        fmt.Println(string(buf2));
    }
    file5.Close();
 
    //写入文件
    file6, error := os.Create("./4.txt");
    if error != nil {
        fmt.Println(error);
    }
    data := "我是数据\r\n";
    for i := 0; i < 10; i++ {
        //写入byte的slice数据
        file6.Write([]byte(data));
        //写入字符串
        file6.WriteString(data);
    }
    file6.Close();
 
    //写入文件
    file7, error := os.Create("./5.txt");
    if error != nil {
        fmt.Println(error);
    }
    for i := 0; i < 10; i++ {
        //按指定偏移量写入数据
        ix := i * 64;
        file7.WriteAt([]byte("我是数据"+strconv.Itoa(i)+"\r\n"), int64(ix));
    }
    file7.Close();
 
    //删除文件
    del := os.Remove("./1.txt");
    if del != nil {
        fmt.Println(del);
    }
 
    //删除指定path下的所有文件
    delDir := os.RemoveAll("./dir");
    if delDir != nil {
        fmt.Println(delDir);
    }
}

  

posted on   怀素真  阅读(23563)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示