Golang程序 将一个文件复制到另一个文件中

在Golang中,我们可以使用Os包和IO包将数据从一个文件复制到另一个文件。在第一个方法中,我们将使用OS包,如os.open,os.create和os.copy函数。而在第二种方法中,我们将使用ioutill.Readfile和ioutil.Writefile来复制文件。

方法1:使用操作系统包

在这个例子中,程序最初使用os.Open函数来打开源文件,source.txt。然后它使用os.Generate函数来创建目标文件,destinaton.txt。然后,使用io.Copy函数将源文件的内容复制到目标文件中。

语法

os.Open

它用于打开一个文件进行阅读。它需要一个输入,即将被打开的文件名。

os.create

它有助于创建一个新的文件。文件名在该函数中被作为输入。

os.copy

它有助于将内容从一个文件复制到另一个文件。在这个函数中,需要两个参数,即目标和源。

io.Copy

它是io包的一个功能,用于将文件从一个位置复制到另一个位置。它需要两个参数,即源文件和目标文件。

算法

  • 第1步 – 创建一个包main,并在程序中声明fmt(格式包)、io和os包,其中main产生可执行代码,fmt帮助格式化输入和输出。
  • 第2步 – 创建一个main函数,并在该函数中创建sourceFile和DestinationFile作为变量分配给特定文件。

  • 第3步 – 在下一步,使用内置函数os.Open打开源文件,如果在打开文件时出现错误,则创建一个带有错误的恐慌。

  • 第4步 – 使用defer关键字和close函数关闭源文件。



  • 第5步 – 在这一步,使用os.Create函数创建目标文件,如果文件没有被创建,则创建一个错误的恐慌。

  • 第6步 – 然后使用defer关键字和close函数关闭目标文件。

  • 第7步 – 使用io.copy函数将源文件的内容复制到目标文件中。

  • 第8步 – 如果出现任何错误,创建一个带有错误的恐慌。



例子

在这个例子中,我们将使用与os包相关的函数。

package main
import (
   "io"
   "os"
)

func main() {
   sourceFile := "src.txt"
   destinationFile := "dst.txt"

   source, err := os.Open(sourceFile)  //open the source file 
   if err != nil {
      panic(err)
   }
   defer source.Close()

   destination, err := os.Create(destinationFile)  //create the destination file
   if err != nil {
      panic(err)
   }
   defer destination.Close()
   _, err = io.Copy(destination, source)  //copy the contents of source to destination file
   if err != nil {
      panic(err)
   }
}

输出

If the file exists content will be copied to the file successfully but if the file is not present, file not found will be printed on the console.

方法2:使用io/ioutil包

在这个方法中,我们将使用io/ioutil包的函数-ioutil.ReadFile和io.WriteFile,前者将用于读取源文件的内容,而后者将用于将内容写入目标文件。

语法

Ioutil.ReadFile

这个函数在ioutil软件包中可用,用于读取文件的内容,文件名是函数中的输入。

ioutil.WriteFile

在Go中,WriteFile属于ioutil包,包含三个参数,第一个是要写入数据的文件名,第二个是要写入的数据,第三个是文件的权限。如果函数被成功执行,数据将被写入文件。

算法

  • 第1步 – 创建一个包main,并在程序中声明fmt(格式包)、io/ioutil包,其中main产生可执行代码,fmt帮助格式化输入和输出。
  • 第2步 – 创建一个main函数,在该函数中创建一个变量sourceFile和destinationFile,并将这些变量分配给各自的文件。

  • 第3步 – 在这一步中,使用ioutil.ReadFile读取源文件的内容,如果在读取文件内容时出现任何错误,则打印 “读取文件错误 “信息并返回。

  • 第4步 – 然后,使用ioutil.WriteFile函数将从源文件中读到的内容写到目标文件中,这里也要检查如果在将内容写到文件时出现任何错误,则打印失败信息并返回。

  • 第5步 – 最后,如果没有错误出现,这意味着内容被成功复制到文件,成功信息将使用fmt.Println()函数打印在控制台,其中ln表示新行。

例子

在这个例子中,io/ioutil包的函数将被用来执行该程序。

package main
import (
   "fmt"
   "io/ioutil"
)

func main() {
   sourceFile := "src.txt"
   destinationFile := "dst.txt"

   data, err := ioutil.ReadFile(sourceFile) //read the contents of source file
   if err != nil {
      fmt.Println("Error reading file:", err)
      return
   }
   err = ioutil.WriteFile(destinationFile, data, 0644) //write the content to destination file
   if err != nil {
      fmt.Println("Error writing file:", err)
      return
   }
   fmt.Println("File copied successfully")  //success message will be printed when         one file is copied into another
}

输出

If file is present the content of source file will be copied to the destination file and success message will be printed on the console but if the error appears failure message will be printed on the console i.e.

Error reading file: open src.txt: no such file or directory

结论

我们用两种方法执行了将一个文件复制到另一个文件的程序。在第一种方法中,我们使用了os包函数,在第二种方法中,我们使用了io/ioutil包函数。