Java program to copy files.

This program will copy files in Java.

Copy Files using Java Program

//Java program to copy file.

import java.io.*;

public class FileCopy {
	public static void main(String args[]) {
		try {
			//input file
			FileInputStream sourceFile 	=new FileInputStream (args[0]);
			//output file
			FileOutputStream targetFile =new FileOutputStream(args[1]);
			
			// Copy each byte from the input to output
			int byteValue;
			//read byte from first file and write it into second line
			while((byteValue = sourceFile.read()) != -1)
			targetFile.write(byteValue);

			// Close the files!!!
			sourceFile.close();
			targetFile.close();
			
			System.out.println("File copied successfully");
		}
		// If something went wrong, report it!
		catch(IOException e) {
			System.out.println("Exception: " + e.toString());
		}
	}
}
    
    Compile: javac FileCopy.java
    Run: java FileCopy file1.txt file2.txt
    File copied successfully







Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.