Copy or Append the content of file into another file using Java

The following program can be able to copy the content of data from one file to another one by using command line argument, It can also able to append the data of one file into other file by using the following simple commands:

									
command line input :
		Usage command : java <class_name>  <input_file>  <output_file> < r or a > 
		 
where , r : for override the file. 
	    a: for appending into the file.	
									
								
								

Copy or append ( Java Program Code )

							
/*==========================================
; Title:  Copy file's content from one file to another
; Author: codenaive littleboy8506@  <admin@codenaive.com> <s.8506.a@gmail.com>
; Date:   29 Dec 2020
;==========================================*/

import java.io.*;  
import java.io.FileWriter;
   
public class Copy
{  
	
	public static void main(String args[]) throws IllegalArgumentException,FileNotFoundException
	{  
		try  
		{ 
		String cmd_file_name_input=args[0];
		String cmd_output_file_name=args[1];
		String input_r=args[2];
		
		
		System.out.println("Welcome ! Copying file wait......\n");
		
		    // Reading the file as Input_file 
			
			FileReader Input_file = new FileReader(new File(cmd_file_name_input));
			File output_f=  new File(cmd_output_file_name);
			
			// Check if the output file exist or not ?
			if(!output_f.exists()){
					input_r="r";       // ignoring the 3rd command line argument 
					output_f.createNewFile();
			}
				
			
			
			// Here is checking if the command line input for replace
			if(input_r.equals("r")){
				FileWriter Output_file = new FileWriter(output_f);
						int data = Input_file.read();
						Output_file.write(" ");
						while(data != -1) {
							Output_file.write(data);
							data = Input_file.read();
						}
					Input_file.close();
					Output_file.close();
			}
			// Here is checking if the command line input append 
			else if(input_r.equals("a"))
			{
				FileWriter  Output_file = new FileWriter(output_f,true);
						int data = Input_file.read();
						Output_file.write("\n");
						while(data != -1) {
							Output_file.write(data);
							data = Input_file.read();
						}
					Input_file.close();
					Output_file.close();
			}
			else{
				System.out.println("Please Enter Valid Keywords !");
			}
			
			System.out.println("Done.......");
		}  
		catch(IOException e)    // Handling the IOException  and FileNotFoundException
		{   System.out.println("Usage command : java <lass_name> <input_file>  <output_file> < r or a >"
					+"\n\t r : for override the file. \n\t a: for appending into the file\n\n");
			System.out.println("Oops.... \nPlease make sure input file exist ! ");
		}
	    catch (IllegalArgumentException exception) {
			System.out.println("Usage command : java <class_name> <input_file>  <output_file> < r or a >" 
			+"\n\t r : for override the file. \n\t a: for appending into the file");
		}
	}
}   								
							 
						
						

Output 1: Append File's data into another file

									
C:\Codenaive>javac Copy.java

C:\Codenaive>java Copy test.txt newFile.txt r
Welcome ! Copying file wait......

Done.......
									 
								

Output 2: Copy File's data into another file

									
C:\Codenaive>javac Copy.java

C:\Codenaive>java Copy test.txt newFile.txt r
Welcome ! Copying file wait......

Done.......
									 
								

Conclusion

We learn in this tutorial, how java program file handling can be use to copy the content of one file into the another one, Java File Handling is used in this tutorial.

Announcement

Advertisement