Copy contents of a file to another using Java
Here’s a small utility to copy contents of one file to another using java programming.
We make use of java.io package present in JDK. This class
provides rich set of I/O streams for reading and writing to files.
For copying contents of one file to another, we will make
use of FileReader and FileWriter classes of java.io package. Since methods
provided by these classes are low-level i.e they work on single characters or stream
of characters, these classes need to wrapped with higher-level classes such as
BufferedReader for FileReader and BufferedWriter for FileWriter. These
high-level classes are efficient as they make use of buffer and work on large
chuck of data at a time.
Below is the code, that will copy contents of one file to
another. Userinput.txt is the source file and useroutput.txt is destination file. Complete code is available at my GitHub Project.
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class FileReadAndWrite { public static void main(String[] args) { File inFile = new File("userinput.txt"); File outFile = new File("useroutput.txt"); try { FileReader fr = new FileReader(inFile); BufferedReader br = new BufferedReader(fr); FileWriter fw = new FileWriter(outFile); BufferedWriter bw = new BufferedWriter(fw); String s = null; while((s = br.readLine()) != null){ bw.write(s); bw.newLine(); } br.close(); bw.flush(); bw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
Script Code Explanation:
inFile and outFile object of File class hold userinput.txt
file and useroutput.txt file respectively. File class is nothing but an
abstract representation of file and directory pathnames.
File inFile = new File("userinput.txt"); File outFile = new File("useroutput.txt");
fr is object of FileReader class and its gets instantiated with inFile object of File Class. fr object in turn is wrapped using BufferedReader class so that we can use its very convenient and efficient readLine() method.
fw is object of FileWriter class and its gets instantiated
with outFile object of File Class. fw object in turn is wrapped using
BufferedWriter class.
FileReader fr = new FileReader(inFile); BufferedReader br = new BufferedReader(fr); FileWriter fw = new FileWriter(outFile); BufferedWriter bw = new BufferedWriter(fw);
The main code that reads source file and writes to destination file is given below –
String s = null; while((s = br.readLine()) != null){ bw.write(s); bw.newLine(); }
When there is no more data to read the readLine() method returns null that is when we stop reading the file, until this time, we read each line of source file and write to the destination file.
Once control comes out of while loop, we close the input stream
reader and flush the contents of buffer to destination file if there are any
and then close the output stream writer.
Don't forget to put comment about your doubts/questions/feedback!
Enjoy and Happy Testing!!
Thanks for sharing a valuable information on Selenium moving content from file to another.
ReplyDeletePlease keep sharing such more articles.
Best Selenium training in Chennai
Thanks a lot.