Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


Java Tutorials



Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java Beginners CopyFile

User Comments
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl
  Blink   Furl   Simpy   Y! MyWeb 

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

22 comments so far (post your own) View All Comments Latest 10 Comments:

Write a program in Java to
create a file and copy the content of an already existing file into it.

Posted by shrdhdha on Thursday, 10.14.10 @ 20:53pm | #100293

How a Java Applet is different from Java Application program? Create an Applet program to display your Bio-Data. Make necessary assumptions and use appropriate layout in your program

Posted by shrdhdha on Tuesday, 10.12.10 @ 01:03am | #100197

in file handling how copy the file from one to another file, and how execute,
send it with screen diagram...please!

Posted by Alok on Sunday, 10.10.10 @ 20:42pm | #100169

i write a program for same topics but have some problem....it's compiled but not able to run it's copy only single line.
program to copy a file
to new file*/
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
class copyfile
public static void main(String args[])
throws FileNotFoundException
Scanner myd=new Scanner(new File("sparsh.txt"));
PrintStream mys=new PrintStream("delete.txt");
char symbol;

so please help me with this code

Posted by sparsh on Tuesday, 07.6.10 @ 09:25am | #97977

This is another way to do the same thing from java.io.File parameters. Here, i'm throwing the IOException and leaving the caller to handle it. Also, what is the point of calling System.exit() in the 'catch (FileNotFoundException )' block?

private static void copyfile(File srcFile, File dstFile) throws IOException {
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(srcFile);
out = new FileOutputStream(dstFile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
} finally {
if (in != null) {
try {
} catch (IOException e) {
if (out != null) {
try {
} catch (IOException e) {

Posted by Laurent on Tuesday, 03.2.10 @ 19:48pm | #95239

It is recommended to close streams in a "finally" block.

Thanks for the code.


Posted by Nicko on Monday, 08.10.09 @ 19:00pm | #89924

i could not understand where i can give the file names.

Posted by priyan on Monday, 05.11.09 @ 15:13pm | #87594

When I specify a map letter with the file name all is OK. When I try to provide a network path I run into issues for example:


does not work but K:\file does work. K: is mapped as \\disk1\path

Any pointers would be appreciated.


Posted by Gary on Friday, 02.20.09 @ 15:39pm | #85061

Alternate data streams and file attributes are not copied correctly in this example...

Posted by Bengt on Tuesday, 01.20.09 @ 07:47am | #83977

You make a comment in the Leap Year Example that leap years are years that are divisable by 4. This is not exactly true.

In the Gregorian calendar, a normal year consists of 365 days. Because the actual length of a sidereal year (the time required for the Earth to revolve once about the Sun) is actually 365.25635 days, a "leap year" of 366 days is used once every four years to eliminate the error caused by three normal (but short) years. Any year that is evenly divisible by 4 is a leap year: for example, 1988, 1992, and 1996 are leap years.

However, there is still a small error that must be accounted for. To eliminate this error, the Gregorian calendar stipulates that a year that is evenly divisible by 100 (for example, 1900) is a leap year only if it is also evenly divisible by 400.

For this reason, the following years are not
leap years:
1700, 1800, 1900, 2100, 2200, 2300, 2500, 2600
This is because they are evenly divisible by 100 but not by 400.

The following years are leap years:
1600, 2000, 2400
This is because they are evenly divisible by both 100 and 400.

Posted by Virginia on Sunday, 07.13.08 @ 02:28am | #66950

How to enter into j_security_check using java?
how to connect with url over ssl using java?.

Posted by ilango on Tuesday, 07.8.08 @ 18:55pm | #66323

How to pass the username and password from java file to j_security_check.

Posted by ilango on Monday, 07.7.08 @ 18:16pm | #66159

Iam asking How to copy a file as word document from any format(ex:txt,png,jpeg....)

Posted by anithavelde on Friday, 04.25.08 @ 12:41pm | #57839

I want to copy a file as word document from any format of file. please give the solution for my problem and send the solution to my ID

Posted by anitha on Wednesday, 04.23.08 @ 17:38pm | #57698

I'm having trouble with this problem written below, can somebody help me ? Please, I need it badly. I know there's a lot of java genius out there...

Write a java program that will be able to:
a. read text line by line;
b. create a directory;
c. delete files from the directory;
d. list the contents of the directory;
e. copy the contents of the file from one file to another; and

f.write text to a file (character output stream)

Posted by Odessy (student) on Sunday, 10.14.07 @ 14:30pm | #33809

i am proud of this website.

Posted by Yuran on Saturday, 10.13.07 @ 10:09am | #33464


ive run the sample program and i get the output that the file is not mention,,so for example which part of the program can i actually input the file to be copied and how can i check if the file has been copied...and lastly what file are we trying to copy here...

Posted by gina esteban on Saturday, 07.28.07 @ 14:00pm | #22101

Excellent and easily understandable tutorial

Posted by hari on Tuesday, 06.5.07 @ 17:15pm | #18284

at the command line type:
java CopyFile file1.txt file2.txt

where file1.txt and file2.txt are your source(sr) and destination(dt) files.

The contents of file1.txt will now be copied into the contents of file2.txt

Posted by tivup on Sunday, 04.1.07 @ 04:36am | #13104

when tryin to run the file to file copy code.
am gettin an errer ; File has not mentioned.
But i created 2 files seperately nd renamed f1 nd f2 with those file names . still not workin ..
kindly help....

Posted by George on Friday, 03.23.07 @ 15:49pm | #12559

very good website for begineers
and its realy cool and simple

Posted by binu on Monday, 02.19.07 @ 17:17pm | #8386

I would be of great help if you could elaborate a little extra than it is about why exactly buffer is to be used.


Posted by keshav Jha on Thursday, 01.4.07 @ 10:37am | #2608

Ask programming questions?



Back to Tutorial


  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name


Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.