Golang program to copy one file into another file


In Golang, we can use Os Packages and IO packages to copy data from one file to another file. n the first method, we'll use OS packages like os.open,os.create and os.copy function. Whereas in the second method, we will use ioutill.Readfile and ioutil.Writefile to copy the files.

Method1: Using OS Package

In this illustration, the program initially uses the os.Open function to open the source file, source.txt. Then it uses the os.Generate function to create the destination file, destinaton.txt. Then, io.Copy function is used to copy the contents of the source file to the target file.

Syntax

os.Open

It is used to open a file to read. It takes one input i.e. the filename which will be opened.

os.create

It helps in the creation of a new file. The filename is given as the input in the function.

os.copy

It helps in copying the content from one file to another. In this function two parameter is required and that is destination and the source.

io.Copy

It is the function of the io package used to copy the files from one location to another. It takes two arguments i.e. the source file and the destination file.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package), io and os package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create a main function and in that function create sourceFile and DestinationFile as variables assigned to the particular files.

  • Step 3 − In the next Step, open the source file using the built-in function os.Open and if an error comes while opening the file , create a panic with the error.

  • Step 4 − Close the source file using defer keyword and close function.

  • Step 5 − In this Step, create the destination file using os.Create function and create panic with error if the file is not created.

  • Step 6 − Then close the destination file using defer keyword and close function.

  • Step 7 − The, using io.copy function copy the contents of the source file to the destination file.

  • Step 8 − If any error appears, create a panic with an error.

Example

In this example, we will use functions associated with os package.

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) } }

Output

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.

Method 2: Using io/ioutil Package

In this method, we will use io/ioutil package functions -ioutil.ReadFile and io.WriteFile, where the former will be used to read the content of the source file while the latter will be used to write the content to the destination file.

Syntax

Ioutil.ReadFile

This function is available in the ioutil package and is used to read the contents of a file with filename as an input in the function.

ioutil.WriteFile

In Go, WriteFile belongs to ioutil package and contains three parameters, the first is the filename where data is to be written, second is the data which is to be written and third is the file permission. The data is written to the file if function is executed successfully.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package), io/ioutil package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create a main function and in that function create a variable sourceFile and destinationFile and assign these variables to the respective files.

  • Step 3 − In this Step, read the content of source file using ioutil.ReadFile and check if any error comes while reading the content of the file print the message of “error reading file” and return.

  • Step 4 − Then, write the contents read from source file to destination file using ioutil.WriteFile function and here also check if any error comes while writing the content to file print the failure message and return.

  • Step 5 − In the end if no error comes this implies that content is copied successfully to the file and success message will be printed on the console using fmt.Println() function where ln means new line.

Example

In this example, the functions of io/ioutil package will be used to execute the program.

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 }

Output

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

Conclusion

We executed the program of copying one file into another file using two methods. In the first method, we used os package functions and in the second method, we used io/ioutil package functions.

Updated on: 22-Feb-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements