[golang] golang文件讀寫 os.OpenFile(fileName,os.O_APPEND|os.O_WRONLY,os.ModeAppend)


[golang] golang文件讀寫 os.OpenFile(fileName,os.O_APPEND|os.O_WRONLY,os.ModeAppend)

讀寫文件要用到的OS包

func OpenFile(name string, flag int, perm FileMode) (*File, error) 

該方法第一個參數為文件路徑,第二個參數控制文件的打開方式,第三個參數控制文件模式

可用的打開方式有

// Flags to OpenFile wrapping those of the underlying system. Not all
// flags may be implemented on a given system.
const (
    // Exactly one of O_RDONLY, O_WRONLY, or O_RDWR must be specified.
    // 只讀模式
    O_RDONLY int = syscall.O_RDONLY // open the file read-only.
    // 只寫模式
    O_WRONLY int = syscall.O_WRONLY // open the file write-only.
    // 可讀可寫
    O_RDWR   int = syscall.O_RDWR   // open the file read-write.
    // The remaining values may be or'ed in to control behavior.
    // 追加內容
    O_APPEND int = syscall.O_APPEND // append data to the file when writing.
    // 創建文件,如果文件不存在
    O_CREATE int = syscall.O_CREAT  // create a new file if none exists.
    // 與創建文件一同使用,文件必須存在
    O_EXCL   int = syscall.O_EXCL   // used with O_CREATE, file must not exist.
    // 打開一個同步的文件流
    O_SYNC   int = syscall.O_SYNC   // open for synchronous I/O.
    // 如果可能,打開時縮短文件
    O_TRUNC  int = syscall.O_TRUNC  // truncate regular writable file when opened.
)

打開模式

// The defined file mode bits are the most significant bits of the FileMode.
// The nine least-significant bits are the standard Unix rwxrwxrwx permissions.
// The values of these bits should be considered part of the public API and
// may be used in wire protocols or disk representations: they must not be
// changed, although new bits might be added.
const (
    // The single letters are the abbreviations
    // used by the String method's formatting.
    // 文件夾模式
    ModeDir        FileMode = 1 << (32 - 1 - iota) // d: is a directory
    // 追加模式
    ModeAppend                                     // a: append-only
    // 單獨使用
    ModeExclusive                                  // l: exclusive use
    // 臨時文件
    ModeTemporary                                  // T: temporary file; Plan 9 only
    // 象征性的關聯
    ModeSymlink                                    // L: symbolic link
    // 設備文件
    ModeDevice                                     // D: device file
    // 命名管道
    ModeNamedPipe                                  // p: named pipe (FIFO)
    // Unix 主機 socket
    ModeSocket                                     // S: Unix domain socket
    // 設置uid
    ModeSetuid                                     // u: setuid
    // 設置gid
    ModeSetgid                                     // g: setgid
    // UNIX 字符串設備,當設備模式是設置unix
    ModeCharDevice                                 // c: Unix character device, when ModeDevice is set
    // 粘性的
    ModeSticky                                     // t: sticky
    // 非常規文件;對該文件一無所知
    ModeIrregular                                  // ?: non-regular file; nothing else is known about this file

    // bit位遮蓋,不變的文件設置為none
    // Mask for the type bits. For regular files, none will be set.
    ModeType = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice | ModeCharDevice | ModeIrregular
    // 權限位
    ModePerm FileMode = 0777 // Unix permission bits
)

創建一個文件並追加內容

package main

import (
    "os"
)
func main() {
    fname := "/tmp/t.txt"
    f, err := os.OpenFile(fname, os.O_CREATE|os.O_RDWR|os.O_APPEND, os.ModeAppend|os.ModePerm)
    if err != nil {
        fmt.Println(err)
    }
    f.WriteString("test")
    f.Close()
}

刪除文件

os.Remove(fname)

創建目錄

dname :="/tmp/d"

os.Mkdir(dname,os.ModeDir|os.ModePerm)

創建完整目錄路徑,即中間目錄不存在的話也一起創建

os.MkdirAll(dname,os.ModeDir|os.ModePerm)

golang os.OpenFile幾種常用模式

os.O_WRONLY | os.O_CREATE | O_EXCL           【如果已經存在,則失敗】

os.O_WRONLY | os.O_CREATE                         【如果已經存在,會覆蓋寫,不會清空原來的文件,而是從頭直接覆蓋寫】

os.O_WRONLY | os.O_CREATE | os.O_APPEND  【如果已經存在,則在尾部添加寫】


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM