GoLang 文件IO操作(一)
1.file操作
func LearnFileMain() {
fileInfo, err := os.Stat("C:\\Users\\31927\\Desktop\\aa.txt")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(fileInfo)
fmt.Println(fileInfo.Name())
fmt.Println(fileInfo.Mode())
fmt.Println(fileInfo.IsDir())
fmt.Println(fileInfo.ModTime())
fmt.Println(fileInfo.Size())
fileName1 := "C:/Users/31927/Desktop/aa.txt"
fileName2 := "aa.txt"
fmt.Println(filepath.IsAbs(fileName1))
fmt.Println(filepath.IsAbs(fileName2))
fmt.Println(filepath.Abs(fileName1))
fmt.Println(filepath.Abs(fileName2))
fmt.Println("父目录:", path.Join(fileName1, ".."))
if _, dirErr := os.Stat("zzs"); os.IsNotExist(dirErr) {
err1 := os.Mkdir("zzs", os.ModePerm)
if err1 != nil {
fmt.Println(err1)
return
}
fmt.Println("单层文件夹创建成功")
}
err2 := os.MkdirAll("zzs1/ywj", os.ModePerm)
if err2 != nil {
fmt.Println("err:", err2)
return
}
fmt.Println("多层文件夹创建完成")
fmt.Println("================创建一个文件================")
file1, fileError1 := os.Create("zzs.txt")
if fileError1 != nil {
fmt.Println("err:", fileError1)
return
}
fmt.Println(file1)
file1.Close()
fmt.Println("================打开一个文件================")
file2, fileError2 := os.Open("zzs.txt")
if fileError2 != nil {
fmt.Println(fileError2)
return
}
fmt.Println(file2)
file3, fileError3 := os.OpenFile("zzs.txt", os.O_RDWR, os.ModePerm)
if fileError3 != nil {
fmt.Println(fileError3)
return
}
fmt.Println(file3)
fmt.Println("================关闭文件================")
file2.Close()
file3.Close()
fmt.Println("================删除文件或文件夹================")
if removeError := os.Remove("zzs.txt"); removeError != nil {
fmt.Println("1:", removeError)
return
}
fmt.Println("删除文件成功")
if removeError1 := os.Remove("zzs"); removeError1 != nil {
fmt.Println("2:", removeError1)
return
}
fmt.Println("删除文件夹成功")
if removeError3 := os.RemoveAll("zzs1"); removeError3 != nil {
fmt.Println("4:", removeError3)
return
}
fmt.Println("RemoveAll方法执行成功!")
}
2.读文件
func LearnFileReadMain() {
file1, err1 := os.Open("zzs.txt")
if err1 != nil {
fmt.Println(err1)
return
}
defer file1.Close()
fileInfo, err2 := os.Stat("zzs.txt")
if err2 != nil {
fmt.Println(err2)
return
}
buffer := make([]byte, fileInfo.Size())
bytesRead, readErr := file1.Read(buffer)
if readErr != nil {
fmt.Println(readErr)
return
}
fmt.Println(bytesRead)
fmt.Println(string(buffer))
}
3.写文件
func LearnFileWrite() {
file1, err1 := os.OpenFile("zzs.txt", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
defer file1.Close()
HandleError(err1)
_, err2 := file1.Write([]byte{80, 80, 67, 68, 69, 70})
HandleError(err2)
fmt.Println("写入成功")
}
func LearnFileWrite1() {
file2, err2 := os.OpenFile("zzs.txt", os.O_WRONLY|os.O_TRUNC, os.ModePerm)
defer file2.Close()
HandleError(err2)
_, err3 := file2.WriteString("zzs Hello 你好世界")
HandleError(err3)
fmt.Println("写入成功")
}
func HandleError(err error) {
if err != nil {
log.Fatal(err)
}
}
4.拷贝文件
(1)使用os包提供的Write和Read进行文件的拷贝
func Copy1(srcPath, targetPath string) (int, error) {
srcFile, srcFileError := os.Open(srcPath)
if srcFileError != nil {
return 0, srcFileError
}
targetFile, targetFileError := os.OpenFile(targetPath, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if targetFileError != nil {
return 0, targetFileError
}
defer srcFile.Close()
defer targetFile.Close()
bs := make([]byte, 1024, 1024)
total := 0
for {
n, err := srcFile.Read(bs)
if err == io.EOF || n == 0 {
fmt.Println("拷贝完成!")
break
} else if err != nil {
return total, err
}
total += n
targetFile.Write(bs[:n])
}
return total, nil
}
(2)使用io包提供的Copy函数进行文件的拷贝
func Copy2(srcPath, targetPath string) (int64, error) {
srcFile, err1 := os.Open(srcPath)
if err1 != nil {
return 0, err1
}
targetFile, err2 := os.OpenFile(targetPath, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err2 != nil {
return 0, err2
}
defer srcFile.Close()
defer targetFile.Close()
return io.Copy(targetFile, srcFile)
}
(3)使用ioutils包中的WriteFile和ReadFile进行文件的拷贝
func Copy3(srcPath, targetPath string) (int, error) {
bs, err := ioutil.ReadFile(srcPath)
if err != nil {
return 0, err
}
err1 := ioutil.WriteFile(targetPath, bs, os.ModePerm)
if err1 != nil {
return 0, err1
}
return len(bs), nil
}
5.断点续传
- 实现原理,每次去读取资源文件,记录读取的数量,记录到临时文件中,当中断后下一次会读取临时文件,从而继续读取
func LearnFileAgainMain(srcPath, targetPath string) (int, error) {
file1, err := os.Open(srcPath)
HandleError(err)
file2, err := os.OpenFile(targetPath, os.O_WRONLY|os.O_CREATE, os.ModePerm)
HandleError(err)
file3, err := os.OpenFile("tmp.txt", os.O_WRONLY|os.O_CREATE, os.ModePerm)
HandleError(err)
defer file1.Close()
defer file2.Close()
file3.Seek(0, io.SeekStart)
bs := make([]byte, 100, 100)
n1, err := file3.Read(bs)
countStr := string(bs[:n1])
count, err := strconv.ParseInt(countStr, 10, 64)
fmt.Println(count)
file1.Seek(count, io.SeekStart)
file2.Seek(count, io.SeekStart)
data := make([]byte, 1024, 1024)
n2 := -1
n3 := -1
total := 0
for {
n2, err = file1.Read(data)
if err == io.EOF || n2 == 0 {
file3.Close()
os.Remove("tmp.txt")
fmt.Println("复制完毕")
break
}
n3, err = file2.Write(data[:n2])
total += n3
file3.Seek(0, io.SeekStart)
file3.WriteString(strconv.Itoa(total))
}
return total, nil
}