package main
import (
"bytes"
"fmt"
"io"
"os"
)
func main() {
file, err := os.OpenFile("./a.png", os.O_RDONLY, os.ModePerm)
defer file.Close()
if err != nil {
fmt.Println("open file err:", err.Error())
os.Exit(1)
}
fmt.Println("open file ./a.png success!")
fileinfo, err := file.Stat()
if err != nil {
fmt.Println("stat err:", err.Error())
os.Exit(1)
}
fmt.Println("fileinfo.Size: ", fileinfo.Size())
fmt.Println("fileinfo.IsDir : ", fileinfo.IsDir())
fmt.Println("fileinfo.Name :", fileinfo.Name())
fmt.Println("fileinfo.ModTime : ", fileinfo.ModTime().String())
var buffer bytes.Buffer
io.CopyN(&buffer, file, 16)
_bytes := buffer.Bytes()
for index, byte := range _bytes {
if index != 0 && index%8 == 0 {
fmt.Printf("\n")
}
fmt.Printf("%02X ", byte)
}
}
输出:
open file ./a.png success!
fileinfo.Size: 91447
fileinfo.IsDir : false
fileinfo.Name : a.png
fileinfo.ModTime : 2017-05-15 15:51:41.1999235 +0800 CST
89 50 4E 47 0D 0A 1A 0A
00 00 00 0D 49 48 44 52