Java Copy file example

In this tutorial you will learn how to copy file in Java?

Copy one file into another


Copy one file into another

In this section, you will learn how to copy content of one file into another file. We will perform this operation by using the read & write methods of BufferedWriter class.

Given below example will give you a clear idea :

Example :


public class CopyFile {
public static void main(String[] args) throws Exception {
BufferedWriter bf = new BufferedWriter(new FileWriter("source.txt"));
bf.write("This string is copied from one file to another\n");
InputStream instm = new FileInputStream(new File("source.txt"));
OutputStream outstm = new FileOutputStream(new File("destination.txt"));
byte[] buf = new byte[1024];
int siz;
while ((siz = > 0) {
outstm.write(buf, 0, siz);
BufferedReader br = new BufferedReader(
new FileReader("destination.txt"));
String str;
while ((str = br.readLine()) != null) {


Output :

The above will produce the following code after execution :

This string is copied from one file to another    

Download Source Code

Share on Google+Share on Google+

Java Copy file example

Posted on: February 19, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Java Copy file example  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image