Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
<div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
<span class="token string">"io"</span>
<span class="token string">"os"</span>
<span class="token punctuation">)</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
sourceFile <span class="token operator">:</span><span class="token operator">=</span> <span class="token string">"src.txt"</span>
destinationFile <span class="token operator">:</span><span class="token operator">=</span> <span class="token string">"dst.txt"</span>
source<span class="token punctuation">,</span> err <span class="token operator">:</span><span class="token operator">=</span> <span class="token class-name"><span class="token namespace">os<span class="token punctuation">.</span></span>Open</span><span class="token punctuation">(</span>sourceFile<span class="token punctuation">)</span> <span class="token comment">//open the source file </span>
<span class="token keyword">if</span> err <span class="token operator">!=</span> nil <span class="token punctuation">{</span>
<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
defer <span class="token class-name"><span class="token namespace">source<span class="token punctuation">.</span></span>Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
destination<span class="token punctuation">,</span> err <span class="token operator">:</span><span class="token operator">=</span> <span class="token class-name"><span class="token namespace">os<span class="token punctuation">.</span></span>Create</span><span class="token punctuation">(</span>destinationFile<span class="token punctuation">)</span> <span class="token comment">//create the destination file</span>
<span class="token keyword">if</span> err <span class="token operator">!=</span> nil <span class="token punctuation">{</span>
<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
defer <span class="token class-name"><span class="token namespace">destination<span class="token punctuation">.</span></span>Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
_<span class="token punctuation">,</span> err <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">io<span class="token punctuation">.</span></span>Copy</span><span class="token punctuation">(</span>destination<span class="token punctuation">,</span> source<span class="token punctuation">)</span> <span class="token comment">//copy the contents of source to destination file</span>
<span class="token keyword">if</span> err <span class="token operator">!=</span> nil <span class="token punctuation">{</span>
<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</div>
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.
<div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
<span class="token string">"fmt"</span>
<span class="token string">"io/ioutil"</span>
<span class="token punctuation">)</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
sourceFile <span class="token operator">:</span><span class="token operator">=</span> <span class="token string">"src.txt"</span>
destinationFile <span class="token operator">:</span><span class="token operator">=</span> <span class="token string">"dst.txt"</span>
data<span class="token punctuation">,</span> err <span class="token operator">:</span><span class="token operator">=</span> <span class="token class-name"><span class="token namespace">ioutil<span class="token punctuation">.</span></span>ReadFile</span><span class="token punctuation">(</span>sourceFile<span class="token punctuation">)</span> <span class="token comment">//read the contents of source file</span>
<span class="token keyword">if</span> err <span class="token operator">!=</span> nil <span class="token punctuation">{</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Error reading file:"</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
<span class="token keyword">return</span>
<span class="token punctuation">}</span>
err <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">ioutil<span class="token punctuation">.</span></span>WriteFile</span><span class="token punctuation">(</span>destinationFile<span class="token punctuation">,</span> data<span class="token punctuation">,</span> <span class="token number">0644</span><span class="token punctuation">)</span> <span class="token comment">//write the content to destination file</span>
<span class="token keyword">if</span> err <span class="token operator">!=</span> nil <span class="token punctuation">{</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Error writing file:"</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
<span class="token keyword">return</span>
<span class="token punctuation">}</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"File copied successfully"</span><span class="token punctuation">)</span> <span class="token comment">//success message will be printed when one file is copied into another</span>
<span class="token punctuation">}</span>
</div>
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.
