一、读取文件

二、文件写入

三、文件复制

1、普通版读取文件

package main

import (
"path/filepath"
"os"
"log"
"io"
"fmt"
) func main() { path, err := filepath.Abs("./")
if err != nil {
panic(err)
} filePath := path + "/day6/filehandler/openfile.go" file, err := os.Open(filePath)
if err != nil {
log.Printf("Open file: %s failed, err: %s\v", filePath, err)
return
} defer file.Close() var content []byte
var buf[]byte for {
n, err := file.Read(buf[:])
if err != nil && err != io.EOF {
log.Printf("read file: %s failed, err: %s\n", filePath, err)
return
}
if err == io.EOF {
break
} fmt.Println("the buff n value is :", n)
validBuf := buf[:n]
content = append(content, validBuf...) } fmt.Printf("the file content is: %s\n", content)
}

2、ioutil版读取文件

import (
"path/filepath"
"io/ioutil"
"log"
"fmt"
) func main() { path, err := filepath.Abs("./")
if err != nil {
panic(err)
} filePath := path + "/day6/filehandler/normalReadFile.go"
contentBytes, err := ioutil.ReadFile(filePath)
if err != nil {
log.Printf("read file '%s' failed, err : %v\n", filePath, err)
return
} fmt.Printf("the file content: %s\n", contentBytes)
}

3、bufio版读取文件  ( bufio介绍 http://www.okyes.me/2016/05/30/go-bufio.html)

package main

import (
"path/filepath"
"os"
"log"
"bufio"
"io"
"fmt"
) func main() { path, err := filepath.Abs("./")
if err != nil {
panic(err)
} filePath := path + "/day6/filehandler/normalReadFile.go"
file, err := os.Open(filePath)
if err != nil {
log.Printf("Open file: %s failed, err: %s\v", filePath, err)
return
}
defer file.Close() reader := bufio.NewReader(file)
var content []byte
var buf[]byte
for {
n, err := reader.Read(buf[:])
if err != nil && err != io.EOF {
log.Printf("Read file '%s' failed, err: %s\v", filePath, err)
return
}
if err == io.EOF {
break
} validBuf := buf[:n]
content = append(content, validBuf...)
} fmt.Printf("the file content is: %s", content) }

4、普通版文件写入

package main

import (
"path/filepath"
"os"
"fmt"
) func main() { pathStr, err := filepath.Abs("./")
if err != nil {
panic(err)
}
filePath := pathStr + "/day6/FileWrite/normalWriter"
file, err := os.Create(filePath)
if err != nil {
panic(err)
} contentByte := []byte("hello world!!!")
n, err := file.Write(contentByte)
if err != nil {
panic(err)
}
fmt.Printf("write byte lenght %d", n) }

5、ioutil版文件写入

package main

import (
"path/filepath"
"io/ioutil"
) func main() { pathStr, err := filepath.Abs("./")
if err != nil {
panic(err)
}
filePath := pathStr + "/day6/FileWrite/ioutilWriter" data := []byte("IOUtil Hello World!!!")
err = ioutil.WriteFile(filePath, data, )
if err != nil {
panic(err)
}
}

6、bufio版文件写入

package main

import (
"path/filepath"
"bufio"
"os"
"fmt"
) func main() { pathStr, err := filepath.Abs("./")
if err != nil {
panic(err)
} filePath := pathStr + "/day6/FileWrite/bufioWriter"
file, err := os.Create(filePath)
if err != nil {
panic(err)
} // writer := bufio.NewWriter(file) 默认的buffSize为4096
writer := bufio.NewWriterSize(file, )
dataByte := []byte("Bufio Hello World!!!")
nn, err := writer.Write(dataByte)
if err != nil {
panic(err)
}
writer.Flush()
fmt.Printf("write byte length :%d", nn)
}

7、文件复制

package main

import (
"io"
"os"
"path/filepath"
"log"
) func main() { path, err := filepath.Abs("./")
if err != nil {
panic(err)
} srcFilePath := path + "/day6/FileRead/normalReadFile.go"
srcFile, err := os.Open(srcFilePath)
if err != nil {
log.Printf("Open file: %s failed, err: %s\v", srcFilePath, err)
return
}
defer srcFile.Close() dstFilePath := path + "/day6/FileCopy/copyNormalReadFile.go"
desFile, _ := os.OpenFile(dstFilePath, os.O_RDWR|os.O_CREATE, )
defer desFile.Close() io.Copy(desFile,srcFile)
}

Go 文件操作的更多相关文章

  1. 【.NET深呼吸】Zip文件操作(1):创建和读取zip文档

    .net的IO操作支持对zip文件的创建.读写和更新.使用起来也比较简单,.net的一向作风,东西都准备好了,至于如何使用,请看着办. 要对zip文件进行操作,主要用到以下三个类: 1.ZipFile ...

  2. 野路子出身PowerShell 文件操作实用功能

    本文出处:http://www.cnblogs.com/wy123/p/6129498.html 因工作需要,处理一批文件,本想写C#来处理的,后来想想这个是PowerShell的天职,索性就网上各种 ...

  3. Node基础篇(文件操作)

    文件操作 相关模块 Node内核提供了很多与文件操作相关的模块,每个模块都提供了一些最基本的操作API,在NPM中也有社区提供的功能包 fs: 基础的文件操作 API path: 提供和路径相关的操作 ...

  4. 归档NSKeyedArchiver解归档NSKeyedUnarchiver与文件管理类NSFileManager (文件操作)

    ========================== 文件操作 ========================== 一.归档NSKeyedArchiver 1.第一种方式:存储一种数据. // 归档 ...

  5. SQL Server附加数据库报错:无法打开物理文件,操作系统错误5

    问题描述:      附加数据时,提示无法打开物理文件,操作系统错误5.如下图: 问题原因:可能是文件访问权限方面的问题. 解决方案:找到数据库的mdf和ldf文件,赋予权限即可.如下图: 找到mdf ...

  6. 通过cmd完成FTP上传文件操作

    一直使用 FileZilla 这个工具进行相关的 FTP 操作,而在某一次版本升级之后,发现不太好用了,连接老是掉,再后来完全连接不上去. 改用了一段时间的 Web 版的 FTP 工具,后来那个页面也 ...

  7. Linux文件操作的主要接口API及相关细节

    操作系统API: 1.API是一些函数,这些函数是由linux系统提供支持的,由应用层程序来使用,应用层程序通过调用API来调用操作系统中的各种功能,来干活 文件操作的一般步骤: 1.在linux系统 ...

  8. C语言的fopen函数(文件操作/读写)

    头文件:#include <stdio.h> fopen()是一个常用的函数,用来以指定的方式打开文件,其原型为:    FILE * fopen(const char * path, c ...

  9. Python的文件操作

    文件操作,顾名思义,就是对磁盘上已经存在的文件进行各种操作,文本文件就是读和写. 1. 文件的操作流程 (1)打开文件,得到文件句柄并赋值给一个变量 (2)通过句柄对文件进行操作 (3)关闭文件 现有 ...

  10. python 文件操作(转)

    python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 返回指定目录下的所有文件和目 ...

随机推荐

  1. hdu 1534(差分约束+spfa求最长路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1534 思路:设s[i]表示工作i的开始时间,v[i]表示需要工作的时间,则完成时间为s[i]+v[i] ...

  2. Android Studio Error -- Could not create the Java Virtual Machine

    :app:dexDebug Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurre ...

  3. 有关于iOS字体的设置

    1.网上搜索字体文件(后缀名为.ttf,或.odf) 2.把字体库导入到工程的resouce中 3.在程序viewdidload中加载一下一段代码 NSArray *familyNames = [UI ...

  4. Oracle SQL*Loader 数据导入工具

    SQL*Loader是一个向Orale大量倒数据的工具,可以从界定文件中导入数据如用 , 界定的,可以从定宽的文件导入数据,

  5. linux 下 Shell编程(三)

    if语句应用实例 if语句可以在程序中实现各种逻辑判断. 用if语句判断并显示文件的信息 可以用test命令和相关的参数来判断文件的属性,然后根据判断结果输出文件的信息. #!/bin/bash #4 ...

  6. c++ 继承,组合

    .什么是继承 A继承B,说明A是B的一种,并且B的所有行为对A都有意义 eg:A=WOMAN B=HUMAN A=鸵鸟 B=鸟 (不行),因为鸟会飞,但是鸵鸟不会. .什么是组合 若在逻辑上A是B的“ ...

  7. CodeIgniter框架——函数(时间函数、装载函数)剖析+小知识点

    连接数据库: 格式: mysql -h主机地址 -u用户名-p用户密码 数据库的提示符:mysql> 退出数据库: exit(回车) 知识点积累: 1.date_default_timezone ...

  8. 哈工大LTP

    http://ltp.ai/ http://pyltp.readthedocs.io/zh_CN/latest/ http://www.cnblogs.com/Denise-hzf/p/6612886 ...

  9. codevs1068 乌龟棋==洛谷P1541 乌龟棋

    P1541 乌龟棋 题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩家 ...

  10. 关于微信小程序下拉出现三个小点

    包子这天看美团外卖的小程序,再瞅瞅自己的背景色,发现,美团下拉的时候有三个小点,但是我自己的校车徐下拉的时候没有三个小点,很是郁闷,于是各种的找各种的找,发现,这三个小点是微信小程序自带的,你只需要设 ...