Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Copy multiple files 
 

In this section, you will learn how the data of multiple files is copied to another file. The java.io package provides this facility. For copping the data of multiple file, you need all files in a specified directory where the contents of all files are to

 

Copy multiple files 

                         

In this section, you will learn how the data of multiple files is copied to another file. The java.io package provides this facility. For copping the data of multiple file, you need all files in a specified directory where the contents of all files are to be copied to a specified file. 

Description of program:

The following program copies the data of two files (source files) in a specified file (target file). At the time of execution of this program, it takes the number of file with their names that have to be copied. The last file is a target file that contains all data of the given source files. The method copyfile() copies the contents of all given files to a specific file. When all data are copied to specified file, it will display a message "File copied" otherwise Exception is thrown and data will not be be copied.

import java.io.*;

public class CopyMultipleFiles{

  public static void main(String[] args)throws IOException {
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter Number of files that have to be copied:");
    int n = Integer.parseInt(bf.readLine());
    String fileName[] new String[n];

    for (int i=0; i<n; i++){
      System.out.println("Enter file name:");
      fileName[i= bf.readLine();
    }

    for (int j=0; j<n-1; j++){
      copyfile(fileName[j],fileName[j+1]);
    }
    System.out.println("File copied.");
  }

  public 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();
    }
    catch(FileNotFoundException ex){
      System.out.println(ex.getMessage() " in the specified directory.");
      System.exit(0);
    }
    catch(IOException e){
      System.out.println(e.getMessage());   
    }
  }

Output of program:

Here the data of "LAN.log" file and "CopyMultipleFiles.java" file are copied to the copy.txt file.

C:\vinod>javac CopyMultipleFiles.java

C:\vinod>java CopyMultipleFiles
Enter Number of files that have to be coppied:
3
Enter file name:
LAN.log
Enter file name:
CopyMultipleFiles.java
Enter file name:
copy.txt
File copied.

Download this example

                         

» View all related tutorials
Related Tags: c orm form time script object io objects help method sed system ip collection this opera create show for work

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

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 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

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

I understand the code for copying files from one directory to another but how do I use SHA-1 hashing algorithm to uniquely identify each file.

Posted by brian on Monday, 04.6.09 @ 23:53pm | #86575

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

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

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

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

Copyright © 2008. All rights reserved.