Table of Contents
Java program to copy the contents from one file to other file
Write a Java program to copy the contents from one file to other file.
We have used FileReader and FileWrite classes along with BufferedReader and BufferedWriter classes to perform this practical.
Java program for billing system
File name : itvoyagers.txt
Welcome to ITVoyagers... Do visit itvoyagers.in for practical and notes. Follow us on Instagram and Facebook.
File name : CopyContentDemo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import java.util.*; import java.io.*; class CopyContentDemo { static String copyfilename, pastefilename, contents; Scanner sc; public CopyContentDemo() { sc = new Scanner(System.in); } public void getFilesName() { System.out.print(" Enter file name with extension (itvoyagers.txt) from where you want to copy the content : "); copyfilename = sc.nextLine(); System.out.print("n Enter file name with extension (itvoyagers.txt) from where you want to paste the content : "); pastefilename = sc.nextLine(); } //itvoyagers.in public void copyContent() { try { FileReader cfr = new FileReader(copyfilename); FileWriter pfr = new FileWriter(pastefilename); BufferedReader cbr = new BufferedReader(cfr); BufferedWriter pbr = new BufferedWriter(pfr); while((contents = cbr.readLine()) != null) { pbr.write(contents); pbr.newLine(); } cbr.close(); pbr.close(); System.out.println("n Conten of '" + copyfilename + "' file has been copied in '" + pastefilename + "' file."); } catch(IOException ex) { System.out.println("n Something went wrong!!! Please check forn 1. If the file name is correctn 2. The file '" + copyfilename + "' does not exist."); } } public static void main(String[] args) { CopyContentDemo rd = new CopyContentDemo(); rd.getFilesName(); rd.copyContent(); } } |
Output

File name : demo.txt (After the program execution)

PRACTICALS/PRACTICE PROGRAM IN Java
CHECKOUT OTHER RELATED TOPICS
We are aiming to explain all concepts of Java in easiest terms as possible.

ITVoyagers
Author