Blog

Copy one file to another in java

Posted by Satyapriya Nayak Blogs | Java Sep 23, 2011
In this blog we will know how to copy one file to another file in java.
Download Files: java.rar

In this blog we will know how to copy one file to another file in java.

 

Copyfile.java

 

import   java.io.*;

import java.util.*;

class  Copyfile

{

public static  void  main(String arg[]) throws  Exception

{

Scanner sc=new Scanner(System.in);

System.out.print("Provide source file name :");

String sfile=sc.next();

System.out.print("Provide destination file name :");

String dfile=sc.next();

FileReader  fin = new   FileReader(sfile);

FileWriter fout=new FileWriter(dfile,true);

int c;

while((c=fin.read()) != -1)

{

fout.write(c);

}

System.out.println("Copy finish...");

fin.close();

fout.close();

}

}

 

Compile

 

Make a directory java in any drive (E:\java). Store two text files one containing data and one empty and java file(Copyfile.java) into that directory.Open command prompt and go to that directory for compiling the java file as

 

E:\Documents and Settings\Administrator>cd\

E:\>cd E:\java

E:\java>javac Copyfile.java

E:\java>java Copyfile

Provide source file name :x.txt

Provide destination file name :y.txt

Copy finish…

 

You will notice all data present in x.txt file are copied to y.txt file.

 

 

 

post comment
     
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter