Introduction
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.io
package. 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:
- An optional system-dependent prefix string,
such as a disk-drive specifier, "/" for the UNIX root
directory, or "\\" for a Win32 UNC pathname, and
-
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 specified file from the first one specified file.
byte[] buf = new byte[1024];
// For creating a byte type buffeR
out.write(buf, 0, len); // For writing to another specified file from buffer
buf
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);
}
}
}
|
Output of program:
Here the data of "Hello.java" file is copied to the
Filterfile.txt file.
C:\nisha>javac CopyFile.java
C:\nisha>java CopyFile a.java Filterfile.txt
File copied.
C:\nisha>
|
You can even use this program for copying files from one hard drive to
another hard drive.
Download File Copy Example

Current Comments
6 comments so far (post your own) View All Comments Latest 10 Comments:i have an xam on day after tommorow.pls tell me the important topics about core java.
Posted by swati on Monday, 06.9.08 @ 23:42pm | #62751
Can any one tell how to write a file from server to client's hard disk from a servlet.
Posted by Pranabesh on Tuesday, 06.19.07 @ 16:54pm | #19703
Hi,
Ashitha!
I have received your request to copy the multiple files at a time. You may visit the following URL to know the code for the same.
Copy multiple files
Thanks for visiting our site.
Vinod kumar
Posted by Vinod kumar on Monday, 05.14.07 @ 12:07pm | #15833
can you tell me how to copy muliple files at a time. I am trying to copy the file to another file dynamically.but I didn't get.error:null pointer exception displayed.
here my code:can u tel what's the error in this programme & how can i correct it..
<%@ page language="java"%>
<%@ page import="java.io.*"%>
<%@ page import="java.text.*"%>
<html>
<head>
<title>
</title>
</head>
<body>
<%
try
{
String cname=request.getParameter("cname");
String category=request.getParameter("category");
String submit=request.getParameter("submit");
int len;
File f1=new File(request.getParameter("file"));
File f2=new File("ee/file1.txt");
InputStream is=new FileInputStream(f1);
OutputStream os=new FileOutputStream(f2);
byte buf[]=new byte[1024];
%>
Company Name is:<%out.println(cname);%>
Category :<%out.println(category);%>
<%
while((len=is.read(buf))>0)
{
os.write(buf,0,len);
out.println("file copied");
}
is.close();
os.close();
out.println("file copied");
}
catch(FileNotFoundException ex)
{
out.println(ex);
}
catch(IOException eo)
{
out.println(eo);
}
catch(Exception e)
{
out.println(e);
}
finally
{
out.println("");
}
%>
</body>
</html>
thank u..
Posted by ashitha on Saturday, 05.12.07 @ 16:42pm | #15740
Thanks for the code!!
Posted by Ajay on Wednesday, 04.11.07 @ 01:58am | #13786
This is fine, but can you tell me how to copy muliple files from server to client side at a time. I reached upto copy one file from server to client side by using servlet but I didn't get how to download multiple files.
If you know something about this, please tell me. I am kindly waiting for your reply.
Thank you.....
Posted by Harish on Tuesday, 03.6.07 @ 11:06am | #10812