How to copy file in Java

Java didn’t comes with any ready make file copy function, you have to manual create the file copy process. To copy file, just convert the file into a bytes stream with FileInputStream and write the bytes into another file with FileOutputStream.

The overall processes are quite simple, just do not understand why Java doesn’t include this method into the java.io.File class.

File copy example

Here’s an example to copy a file named “Afile.txt” to another file named “Bfile.txt”. If the “Bfile.txt” is exists, the existing content will be replace, else it will create with the content of the “Afile.txt”.


package com.mkyong.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class CopyFileExample 
{
    public static void main(String[] args)
    {	
    	
    	InputStream inStream = null;
	OutputStream outStream = null;
		
    	try{
    		
    	    File afile =new File("Afile.txt");
    	    File bfile =new File("Bfile.txt");
    		
    	    inStream = new FileInputStream(afile);
    	    outStream = new FileOutputStream(bfile);
        	
    	    byte[] buffer = new byte[1024];
    		
    	    int length;
    	    //copy the file content in bytes 
    	    while ((length = inStream.read(buffer)) > 0){
    	  
    	    	outStream.write(buffer, 0, length);
    	 
    	    }
    	 
    	    inStream.close();
    	    outStream.close();
    	      
    	    System.out.println("File is copied successful!");
    	    
    	}catch(IOException e){
    		e.printStackTrace();
    	}
    }
}
author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar
newest oldest most voted
trackback
Java I/O Tutorials

[…] Copy a file […]

Werner Spurgeon
Guest
Werner Spurgeon

Nice weblog right here! Additionally your web site loads up very fast! What web host are you using? Can I am getting your associate link to your host? I wish my web site loaded up as fast as yours lol

Jeevan Kumar Vishwakarman
Guest
Jeevan Kumar Vishwakarman

Hopefully there is an easier way to do file related operations…
use “Files” class in java itself for easier file operations…
visit

http://docs.oracle.com/javase/tutorial/essential/io/fileOps.html
for more otions

Daniel
Guest
Daniel

This example has potential resource leaks! In case of exception streams are still open!

Pankaj
Guest
Pankaj

Java NIO FileChannel is the best way to copy file in java, read this article

http://www.journaldev.com/861/4-ways-to-copy-file-in-java

baker
Guest
baker

Are you korean ?

trackback
?Java???????

[…] ?????????????????????????????????? ?????http://www.mkyong.com/java/how-to-copy-file-in-java/ […]

Cuauh Medina
Guest
Cuauh Medina

I’ve got a question, why do you initialize the buffer with 1024 ? does that mean that I can only copy files of 1Mb?