zoukankan      html  css  js  c++  java
  • (Go)12.文件操作

    func OpenFile
    func OpenFile(name string, flag int, perm FileMode) (file *File, err error)
    const (
        O_RDONLY int = syscall.O_RDONLY // 只读模式打开文件
        O_WRONLY int = syscall.O_WRONLY // 只写模式打开文件
        O_RDWR   int = syscall.O_RDWR   // 读写模式打开文件
        O_APPEND int = syscall.O_APPEND // 写操作时将数据附加到文件尾部
        O_CREATE int = syscall.O_CREAT  // 如果不存在将创建一个新文件
        O_EXCL   int = syscall.O_EXCL   // 和O_CREATE配合使用,文件必须不存在
        O_SYNC   int = syscall.O_SYNC   // 打开文件用于同步I/O
        O_TRUNC  int = syscall.O_TRUNC  // 如果可能,打开时清空文件
    )

    1.//创建一个新文件,写入内容 5句 "你好,Golang!"

    package main
    import (
        "fmt"
        "bufio"
        "os" 
    )
    
    func main() {
        
        //创建一个新文件,写入内容 5句 "你好,Golang!"
        filePath := "d:/abc.txt"
        file, err := os.OpenFile(filePath, os.O_WRONLY | os.O_CREATE, 0666)
        if err != nil {
            fmt.Printf("open file err=%v
    ", err)
            return 
        }
        //及时关闭file句柄
        defer file.Close()
        str := "你好,Golang!
    " // 
     表示换行
        //写入时,使用带缓存的 *Writer
        writer := bufio.NewWriter(file)
        for i := 0; i < 10; i++ {
            writer.WriteString(str)
        }
        //内容是先写入到缓存的,所以需要调用Flush方法,将缓冲的数据写入文件
        writer.Flush()
    
    }

    2.打开一个存在的文件,在原来的内容追加内容 'this is test!'

    package main
    
    import (
        "fmt"
        "bufio"
        "os" 
    )
    
    func main() {
        
        //打开一个存在的文件,在原来的内容追加内容 'this is test!'
        filePath := "d:/abc.txt"
        file, err := os.OpenFile(filePath, os.O_WRONLY | os.O_APPEND, 0666)
        if err != nil {
            fmt.Printf("open file err=%v
    ", err)
            return 
        }
        //及时关闭file句柄
        defer file.Close()
        str := "this is test!
    " // 
     表示换行
        //写入时,使用带缓存的 *Writer
        writer := bufio.NewWriter(file)
        for i := 0; i < 5; i++ {
            writer.WriteString(str)
        }
        //内容是先写入到缓存的,所以需要调用Flush方法,将缓冲的数据写入文件
        writer.Flush()
    
    }

    3.打开一个存在胡文件,读出原来的内容,再追加5行 hello world!

    
    
    package main


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

    func main() {
        filePath := "d:/abc.txt"
        file, err := os.OpenFile(filePath, os.O_RDWR | os.O_APPEND, 066)
        if err != nil {
            fmt.Printf("open file err=%v ", err)
            return
        }

        defer file.Close()

        reader := bufio.NewReader(file)
        for {
            str, err := reader.ReadString(' ')
            if err == io.EOF {
                break
            }
            fmt.Print(str)
        }


        str := "hello world! "
        writer := bufio.NewWriter(file)
        for i := 0; i < 5; i++ {
            writer.WriteString(str)

        }
        writer.Flush()

    }
     
  • 相关阅读:
    Ubuntu 或 UbuntuKyLin14.04 Unity桌面側边栏和顶层菜单条显示异常解决方法
    关于程序猿的几个阶段!
    独立开发人员低成本推广APP的18条技巧
    Effective C++ 条款27
    OpensStack instance debug
    OpenStackCLI调试及术语识记
    OpenStack术语名词及问题调试
    apacheOfbiz
    obiz
    How to run OFBiz as a Service on linux
  • 原文地址:https://www.cnblogs.com/lvcisco/p/12151034.html
Copyright © 2011-2022 走看看