Tell A Friend Your Friend Name
OK Java| Frameworks | Databases | Technology | Development| Build/Test tools | OS | Servers | PHP| Books | More | What's New? C ore Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions? | Software Development
Search Search
Java - Copying one file to another
In this section, you will learn how to copy contents from one file to another file.
Java - Copying one file to another
This example illustrates how to copy contents from one file to another file. This topic is related to the I/O (input/output) of java.io package.
In this example we are using File class of java.iopackage. The File class is an abstract representation of file and directory pathnames. This class is an abstract, system-independent view of hierarchical pathnames. An abstract pathname has two components:
1. An optional system-dependent prefix string,
such as a disk-drive specifier, "/" for the UNIX root directory, or "\\" for a Win32 UNC pathname, and
2. A sequence of zero or more string names. Explanation
This program copies one file to another file. We will be declaring a function called copyfile which copies the contents from one specified file to another specified file.
copyfile(String srFile, String dtFile)
The function copyfile(String srFile, String dtFile) takes both file name as parameter. The function creates a new File instance for the file name passed as parameter
File f1 = new File(srFile); File f2 = new File(dtFile);
and creates another InputStream instance for the input object and OutputStream instance for the output object passed as parameter
InputStream in = new FileInputStream(f1); OutputStream out = new FileOutputStream(f2);
and then create a byte type buffer for buffering the contents of one file and write to another
Programming
Tutorials
Spring
Framework
Hibernate-Tutorials
Ajax
Core Java
Database-Tutorials
Enterprise Java
iPhone
J2ME-Tutorials
Java-Tutorials
JavaScript
JDBC-Tutorials
JMS-Tutorials
JPA
JSP-Tutorials
Open Source
PHP
Servlets-Tutorials
Struts-Tutorials
Technology
UML-Tutorials
Web-Services-Tutorials
XML
More
Tutorials....
Ads by Google Java Programming Java Source Code Java Jobs Java SwingEJB M anage d Hosting EJB / Java / J2EE / JBoss on VPS 24x7 Support from Java Experts www.Eapps.com/EJB-Hosting Java Database High Scalability, High Availability Large Data Sets, High Performance www.versant.com/objectdatabase
Java Calendar Componentmigcalendar.com Java Developer - Add a Best Selling Calendar View to your Application
Generation of Source Codewww.sparxsystems.com .NET, Java, C++, XSD, DDL, PHP, CORBA, Python & more. Free Trial!
DNA copy number analysiswww.infoquant.com Cross-platform array CGH software for Cytogenetic laboratories
Java Swing Componentswww.javaswingcomponents.com Your source of custom java swing components.
and then create a byte type buffer for buffering the contents of one file and write to another specified file from the first one specified file.
// For creating a byte type buffer byte[] buf = new byte[1024];
// For writing to another specified file from buffer buf out.write(buf, 0, len);
Code of the Program :
import java.io.*; public class CopyFile{
private static void copyfile(String srFile, String dtFile){ try{
File f1 = new File(srFile);
File f2 = new File(dtFile);
InputStream in = new FileInputStream(f1);
//For Append the file.
// OutputStream out = new FileOutputStream(f2,true);
//For Overwrite the file.
OutputStream out = new FileOutputStream(f2);
byte[] buf = new byte[1024]; int len;
while ((len = in.read(buf)) > 0){ out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("File copied."); }
catch(FileNotFoundException ex){
System.out.println(ex.getMessage() + " in the specified directory."); System.exit(0);
}
catch(IOException e){
System.out.println(e.getMessage());
} }
public static void main(String[] args){ switch(args.length){
case 0: System.out.println("File has not mentioned."); System.exit(0);
case 1: System.out.println("Destination file has not mentioned."); System.exit(0);
case 2: copyfile(args[0],args[1]); System.exit(0);
default : System.out.println("Multiple files are not allow."); System.exit(0);
} } }
Download File Copy Example
Related Links by Google
Java Sun, Sun Java Tutorials, Java Tutorials - Java Example C... java Beginner,java Beginners,Beginning java,Beginners Java ... JDBC - Java Database Connectivity Tutorials
JSP Tutorials,EJB Tutorial,JDBC Tutorials,Free Java Servlets ... Java Tutorials - JDK Tutorials, JAVA Examples, JDK Examples Java Interview Question,Interview questions Java,Java Intervi... Related Searches by Google
java
Recently Viewed
Java Script Code of Calendar and Date Picker or Popup Calendar
Constructor Exception in Java Struts 1.x Vs Struts 2.x
Preparing table for HQL Examples Java - Copying one file to another
Java Building a Simple Web Service ? A Tutorial Tutorial Java error java.lang.nullpointerexception
Compare string example
Java Exception - Exception Handling in Java J2EE Tutorial - Java Bean
Software Solutions
Software Solutions and Services Website Designing Services
Web Designing Packages! From $150! Flex Outsourcing
Hire Flex Developer
Website Designing Company Web Hosting Website Designing Quotation
Hire PHP Developer Hire PHP programmer PHP Outsourcing Java Project Outsourcing Hire Software Developer Hire Java Developer Hire iPhone developer
Outsourcing iPhone development Offshore iPhone Apps Development
Search Tutorials Ads by Google Java Swing Sample Java Java AntiVirus Java Class
java download java update copy file
» View all related
tutorials
Beginners Java Tutorial ... 573258 views
Randomizer ... 575840 views
Access Static Member Of The Class Through Object ... 576174 views
Date forms ... 576295 views
Forcing Updates to a File to the Disk ... 576441 views
Listing the File System Roots ... 576654 views
Copy a file to other destination and show the modification time of destination file ...
576764 views
Date Comparison ... 576818 views
Constructing a File Name path ... 576910 views
Extra Storage Merge Sort in Java ... 576913 views » V iew all r elated tutor ials
1 2 3 4 5
Related Tags:calgorithmidearraysortingdatadivmergesortstorageordervivaluenumberintthis idaielementcreate
Leave your comment: Name: Email: URL: http://w w w .roseindia.net/java/beginners/CopyFile.shtml Title: Comments: Enter Code: Submit Comment
Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or
Ads by Google Java Flash Java Guide Java Web
More in this series... Most Read Latest Search
deleted.
No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.
Add This Tutorial To:
Del.icio.us Digg Google Spurl Blink Furl Simpy Y! MyWeb
Current Comments
11 comments so far (post your own) View All Comments Latest 10 Comments: 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 View This Comment Separately
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
View This Comment Separately
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 View This Comment Separately
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 View This Comment Separately
i am proud of this website.
Posted by Yuran on Saturday, 10.13.07 @ 10:09am | #33464 View This Comment Separately
hi,
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 View This Comment Separately
Excellent and easily understandable tutorial
Posted by hari on Tuesday, 06.5.07 @ 17:15pm | #18284 View This Comment Separately
George,
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 View This Comment Separately
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 View This Comment Separately
very good website for begineers and its realy cool and simple
Posted by binu on Monday, 02.19.07 @ 17:17pm | #8386 View This Comment Separately
Home | JSP | EJB | JDBC | Java Servlets | WAP | Free JSP Hosting | Search Engine | New s Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs About Us | Advertising On RoseIndia.net | Site Map | Privacy Policy
Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com. Copyright © 2008. All rights reserved.