1. 标准输入输出

os提供了标准输入输出文件:

    Stdin  = NewFile(uintptr(syscall.Stdin), "/dev/stdin")
Stdout = NewFile(uintptr(syscall.Stdout), "/dev/stdout")
Stderr = NewFile(uintptr(syscall.Stderr), "/dev/stderr")
func NewFile(fd uintptr, name string) *File

2. os包读取文件

文件使用os.File类型的指针来表示,也叫作文件句柄。File是struct,表示一个open file descriptor。标准输入输出os.Stdin/os.Stdout都是*os.File。

os.File与unix file descriptor fd使用类似,但不能共同使用。golang中用os.File封装或代替unix fd。

func NewFile(fd uintptr, name string) *File

NewFile returns a new File with the given file descriptor and name. The returned value will be nil if fd is not a valid file descriptor.

func (f *File) Fd() uintptr

Fd returns the integer Unix file descriptor referencing the open file. The file descriptor is valid only until f.Close is called or f is garbage collected.

On Unix systems this will cause the SetDeadline methods to stop working.

os包包含操作文件的最底层处理函数,类似unix系统调用。

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.
)
const (
SEEK_SET int = // seek relative to the origin of the file
SEEK_CUR int = // seek relative to the current offset
SEEK_END int = // seek relative to the end
)
var (
// ErrInvalid indicates an invalid argument.
// Methods on File will return this error when the receiver is nil.
ErrInvalid = errInvalid() // "invalid argument" ErrPermission = errPermission() // "permission denied"
ErrExist = errExist() // "file already exists"
ErrNotExist = errNotExist() // "file does not exist"
ErrClosed = errClosed() // "file already closed"
ErrNoDeadline = errNoDeadline() // "file type does not support deadline"
)
type File struct {
// contains filtered or unexported fields
}
func Open(name string) (*File, error)
func OpenFile(name string, flag int, perm FileMode) (*File, error)
func (f *File) Read(b []byte) (n int, err error)
func (f *File) Write(b []byte) (n int, err error)
func (f *File) WriteString(s string) (n int, err error)
func (f *File) Seek(offset int64, whence int) (ret int64, err error)
func Create(name string) (*File, error)

Open()默认的mode为O_RDONLY。

Read reads up to len(b) bytes from the File. It returns the number of bytes read and any error encountered. At end of file, Read returns 0, io.EOF.

Write writes len(b) bytes to the File. It returns the number of bytes written and an error, if any. Write returns a non-nil error when n != len(b).

Seek sets the offset for the next Read or Write on file to offset, interpreted according to whence: 0 means relative to the origin of the file, 1 means relative to the current offset, and 2 means relative to the end. It returns the new offset and an error, if any.

package main
import (
"fmt"
"os"
"log"
"io"
_"strings"
) func main(){
file, err := os.OpenFile("file.txt", os.O_APPEND | os.O_RDWR | os.O_CREATE, )
if err != nil {
log.Fatal(err)
}
defer file.Close() if _, err := file.Write([]byte("appended some data\n")); err != nil {
log.Fatal(err)
}
if _, err := file.Seek(, os.SEEK_SET); err != nil {
log.Fatal(err)
} buf := make([]byte, )
err = nil
for err == nil {
n, err := file.Read(buf)
if err == io.EOF {
break
}
fmt.Printf("Read %d bytes:", n)
//fmt.Println(strings.TrimSpace(string(buf)))
fmt.Println((string(buf)))
}
}

3. io/ioutil包读取文件

io包对os包的file基础操作封装为interface,并行处理非安全。

Package io provides basic interfaces to I/O primitives. Its primary job is to wrap existing implementations of such primitives, such as those in package os, into shared public interfaces that abstract the functionality, plus some other related primitives.

Because these interfaces and primitives wrap lower-level operations with various implementations, unless otherwise informed clients should not assume they are safe for parallel execution.

type Reader interface {
Read(p []byte) (n int, err error)
}
type Writer interface {
Write(p []byte) (n int, err error)
}
type StringWriter interface {
WriteString(s string) (n int, err error)
}
type ReadWriter interface {
Reader
Writer
}
func ReadFull(r Reader, buf []byte) (n int, err error)
func WriteString(w Writer, s string) (n int, err error)
func Copy(dst Writer, src Reader) (written int64, err error)
    r := strings.NewReader("some io.Reader stream to be read\n")

    if _, err := io.Copy(os.Stdout, r); err != nil {
log.Fatal(err)
}
func copyFile(source string, dest string) (err error) {
sf, err := os.Open(source)
if err != nil {
return err
}
defer sf.Close()
df, err := os.Create(dest)
if err != nil {
return err
}
defer df.Close()
_, err = io.Copy(df, sf)
if err == nil {
si, err := os.Stat(source)
if err == nil {
err = os.Chmod(dest, si.Mode())
} }
return
}

ioutil将整个文件的内容读到一个内存中。

func ReadFile(filename string) ([]byte, error)
func WriteFile(filename string, data []byte, perm os.FileMode) error
func ReadAll(r io.Reader) ([]byte, error)

示例:

package main
import (
"fmt"
"io/ioutil"
) func main(){
// data, err := ioutil.ReadFile("/home/golang/file/test.txt")
data, err := ioutil.ReadFile("./test.txt")
if err != nil {
fmt.Println("File reading error", err)
return
}
fmt.Println("Contents of File:", string(data))
}

4. bufio包缓冲读取(buffered reader)文件

Package bufio implements buffered I/O. It wraps an io.Reader or io.Writer object, creating another object (Reader or Writer)

that also implements the interface but provides buffering and some help for textual I/O.

bufio提供了新的bufio.Reader, bufio.Writer, bufio.Scanner结构体,提供缓冲读写操作。

type ReadWriter struct {
*Reader
*Writer
}
type Reader struct {
// contains filtered or unexported fields
}
type Scanner struct {
// contains filtered or unexported fields
}
type Writer struct {
// contains filtered or unexported fields
}
type SplitFunc func(data []byte, atEOF bool) (advance int, token []byte, err error)

相关函数:

func NewReader(rd io.Reader) *Reader
func (b *Reader) ReadString(delim byte) (string, error)
func (b *Reader) ReadByte() (byte, error)
func (b *Reader) ReadBytes(delim byte) ([]byte, error)
func (b *Reader) Read(p []byte) (n int, err error)

func NewWriter(w io.Writer) *Writer
func (b *Writer) Write(p []byte) (nn int, err error)
func (b *Writer) WriteByte(c byte) error
func (b *Writer) WriteRune(r rune) (size int, err error)
func (b *Writer) WriteString(s string) (int, error)
func (b *Writer) Flush() error

ReadString(delim byte)从输入中读取内容,直到碰到 delim 指定的字符,然后将读取到的内容连同 delim 字符一起放到缓冲区。ReadBytes()类似,但ReadByte()仅读取一个字节。

ReadString()只能读取字符串,Read()可以读取任何数据,包括二进制文件。

os.File实现了io.Reader接口定义的方法,所以可用os.File替换io.Reader传参。如NewReader(os.File)。

        var inputReader *bufio.Reader
var input string
var err error inputReader = bufio.NewReader(os.Stdin)
fmt.Println("Please Enter some input:")
input, err = inputReader.ReadString('\n')
if err == nil {
fmt.Printf("The input was: %s\n", input)
}
        inputFile, inputError := os.Open("test.txt")
if inputError != nil {
fmt.Printf("An error occurred on openning th inputfile\n")
return
} defer inputFile.Close()
inputReader := bufio.NewReader(inputFile)
for {
inputString, readerError := inputReader.ReadString('\n')
if readerError == io.EOF {
return
}
fmt.Printf("The input was: %s", inputString)
}
    r := bufio.NewReader(f)
b := make([]byte, 3)
for {
_, err := r.Read(b) //每次读取3个字节
if err != nil {
fmt.Println("Error reading file:", err)
break
}
fmt.Println(string(b))
}
    writer := bufio.NewWriter(fileObj)
defer writer.Flush()
writer.WriteString(data)

bufio逐行读取文件

    f, err := os.Open("./test.txt")
if err != nil {
log.Fatal(err)
}
defer func() {
if err = f.Close(); err != nil {
log.Fatal(err)
}
}()
s := bufio.NewScanner(f)
for s.Scan() {
fmt.Println(s.Text())
}
err = s.Err()
if err != nil {
log.Fatal(err)
}

相关函数:

func NewScanner(r io.Reader) *Scanner
func (s *Scanner) Scan() bool
func (s *Scanner) Text() string

NewScanner returns a new Scanner to read from r. The split function defaults to ScanLines.

5. 判断文件是否存在

type FileInfo interface {
Name() string // base name of the file
Size() int64 // length in bytes for regular files; system-dependent for others
Mode() FileMode // file mode bits
ModTime() time.Time // modification time
IsDir() bool // abbreviation for Mode().IsDir()
Sys() interface{} // underlying data source (can return nil)
} func Lstat(name string) (FileInfo, error)
Lstat returns a FileInfo describing the named file. If the file is a symbolic link,
the returned FileInfo describes the symbolic link. func Stat(name string) (FileInfo, error)
Stat returns a FileInfo describing the named file.
If there is an error, it will be of type *PathError.
func FileExist(filename string) bool {
_, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
// fmt.Println(err)
return err == nil || os.IsExist(err)
}

6. 遍历目录

需要用到path和path/filepath包。

func Rel(basepath, targpath string) (string, error)
Rel returns a relative path that is lexically equivalent to targpath
when joined to basepath with an intervening separator.
That is, Join(basepath, Rel(basepath, targpath)) is equivalent to targpath itself.
basepath/rel == targpath func Walk(root string, walkFn WalkFunc) error
Walk walks the file tree rooted at root, calling walkFn for each file or directory in the tree,
including root. All errors that arise visiting files and directories are filtered by walkFn. type WalkFunc func(path string, info os.FileInfo, err error) error
The path argument contains the argument to Walk as a prefix;
that is, if Walk is called with "dir", which is a directory containing the file "a",
the walk function will be called with argument "dir/a".
The info argument is the os.FileInfo for the named path.
import "path/filepath"
func fileList(dir string) error {
fullPath, err := filepath.Abs(dir)
if err != nil {
return err
} callback := func(path string, fi os.FileInfo, err error) error {
if fi.IsDir() {
return nil
} rel, err := filepath.Rel(fullPath, path)
if err != nil {
return err
}
fmt.Println("rel:", rel, "\tbasepath:", fullPath, "\ttargpath:", path)
return nil
} return filepath.Walk(fullPath, callback)
}

遍历一个目录下指定类型文件,使用ioutil.ReadDir()

func ReadDir(dirname string) ([]os.FileInfo, error)
ReadDir reads the directory named by dirname and
returns a list of directory entries sorted by filename. func Ext(path string) string
Ext returns the file name extension used by path.
The extension is the suffix beginning at the final dot in the final slash-separated element of path;
it is empty if there is no dot.
package main

import (
"fmt"
_"io"
"io/ioutil"
"path"
) func fileList(dir string, ftype string) ( map[int] string, error) {
fdir , err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
} var list map[int] string = make(map[int]string)
for n, fi := range fdir {
fname := fi.Name()
if ext := path.Ext(fname); ext != ftype{
continue
}
fname_rel := dir + "/"+fname
list[n] = fname_rel
} return list, nil
} func main(){
flist, err := fileList(".", ".go")
if err != nil {
fmt.Println(err)
} for i, ff := range flist {
fmt.Println(i, ":", ff)
}
}

参考:

1.   https://golang.google.cn/pkg/fmt/#Scanln

2.   https://www.kancloud.cn/kancloud/the-way-to-go/72678

3.   https://studygolang.com/subject/2

4.    https://golang.google.cn/pkg/os

5.     golang读写文件的几种方式

6.    go语言功能代码

golang读写文件的更多相关文章

  1. golang读写文件的几种方式

    golang中处理文件有很多种方式,下面我们来看看. (1)使用os模块 先来看看如何查看文件属性 package main import ( "fmt" "os&quo ...

  2. golang读写文件之Scan和Fprintf

    1. 标准输入输出 os提供了标准输入输出: Stdin = NewFile(uintptr(syscall.Stdin), "/dev/stdin") Stdout = NewF ...

  3. Golang 读写文件

    读文件 func ReadFile_v1(filename string) { var ( err error content []byte ) fileObj,err := os.Open(file ...

  4. golang操作文件的四种方法

    golang追加内容到文件末尾 字数349 阅读54 评论0 喜欢2 golang读写文件,网上很多教程了但是今天有个需求,想要把内容追加写到文件末尾google了好久,没有查到研究了一会儿file库 ...

  5. Golang的文件处理方式-常见的读写姿势

    Golang的文件处理方式-常见的读写姿势 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在 Golang 语言中,文件使用指向 os.File 类型的指针来表示的,也叫做文件句柄 ...

  6. Golang之文件读写

    读写文件,不添加文件路径,默认写入到GOPATH路径下 终端读写: 源码 func Sscanf func Sscanf(str string, format string, a ...interfa ...

  7. golang学习笔记 ----读写文件

    使用io/ioutil进行读写文件 ioutil包 其中提到了两个方法: func ReadFile func ReadFile(filename string) ([]byte, error) Re ...

  8. Golang的文件处理方式-常见的读写

    在 Golang 语言中,文件使用指向 os.File 类型的指针来表示的,也叫做文件句柄.注意,标准输入 os.Stdin 和标准输出 os.Stdout ,他们的类型都是 *os.File 哟.在 ...

  9. Hyper-V无法文件拖拽解决方案~~~这次用一个取巧的方法架设一个FTP来访问某个磁盘,并方便的读写文件

    异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 服务器相关的知识点:http://www.cnblogs.com/dunitia ...

随机推荐

  1. Synchronize深入

    前言:    synchronize会使用,但是对于深层次的知识,不是很清楚,故整理一篇博客. 简介:   能够保证在同一时刻,最多只有一个线程执行该端代码,以达到保证并发安全效果. 两种用法: 对象 ...

  2. LeetCode 257. Binary Tree Paths(二叉树根到叶子的全部路径)

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  3. SDN实验---Ryu的应用开发(三)流量监控

    一:实现流量监控 (一)流量监控原理 其中控制器向交换机周期下发获取统计消息,请求交换机消息------是主动下发过程 流速公式:是(t1时刻的流量-t0时刻的流量)/(t1-t0) 剩余带宽公式:链 ...

  4. 自动生成LR脚本且运行

    背景:作为一个测试,特别是性能测试,尤其在活动的测试,时间紧,有很多要测的,我们的LR11因为浏览器兼容问题全录制不了脚本了,用浏览器加代理或手机加代理录制,我感觉好麻烦 ,所以就想如果能用脚本把所有 ...

  5. 【454】ML-DL相关链接

    GD(梯度下降)和SGD(随机梯度下降) 机器学习中的Bias和Variance 机器学习之判别式模型和生成式模型 笔记 | 什么是Cross Entropy

  6. POI 读取Excel文件 并解析JSON数据

    package skuPrice; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundEx ...

  7. EasyDSS高性能RTMP、HLS(m3u8)、HTTP-FLV、RTSP流媒体服务器解决方案之Windows服务安装

    背景说明 EasyDSS流媒体解决方案是由安徽旭帆信息科技有限公司自主研发的一套集流媒体点播.转码.管理.直播.录像.检索.时移回看于一体的一套完整的商用流媒体解决方案.EasyDSS软件以压缩包的形 ...

  8. 【bat批处理】批量执行某个文件夹下的所有sql文件bat批处理

    遍历文件夹下所有的sql文件,然后命令行执行 for /r "D:\yonyou\UBFV60\U9.VOB.Product.Other" %%a in (*.sql) do ( ...

  9. vue使用px2rem

    配置 flexible 安装 lib-flexible 在命令行中运行如下安装: 1 npm i lib-flexible --save 引入 lib-flexible 在项目入口文件 main.js ...

  10. STL源码之traits编程技法

    摘要 主要讨论如何获取迭代器相应型别.使用迭代器时,很可能用到其型别,若需要声明某个迭代器所指对象的型别的变量,该如何解决.方法如下: function template的参数推导机制 例如: tem ...