Solved

Copy a folder to another folder

Posted on 2002-07-11
3
265 Views
Last Modified: 2010-03-31
Can you write a function that would copy all the contents of a folder, files and subfolders included, to another folder?


Something like:
copyFolder("\\test1","\\test2"); <==>Copy the content of directory test1 to test2.
0
Comment
Question by:yes4me
  • 2
3 Comments
 
LVL 1

Expert Comment

by:klf
ID: 7148198
below is some code to get you started. I tested this on Linux.

import java.lang.*;
import java.io.*;

public class Copy
{
    public static void main(String[] argv)
     throws Exception
    {
     if (argv.length != 2) {
         System.err.println("usage: Copy from_file to_file\n");
     }
     copy(new File(argv[0]), new File(argv[1]));
    }

    public static void copy(File from, File to)
     throws Exception
    {
     if (from.equals(to)) {
         throw new Exception(from.getName() + " and " + to.getName() +
                    " are the same file");
     }
     if (from.isDirectory()) {
         copyDir(from, to);
     } else {
         copyFile(from, to);
     }
    }

    public static void copyDir(File from, File to)
     throws Exception
    {
     System.err.println("creating dir: " + to.getPath());
     to.mkdirs();
     File[] files = from.listFiles();
     for (int i = 0; i < files.length; i++) {
         File tmpTo = new File(to, files[i].getName());
         copy(files[i], tmpTo);
     }
    }

    public static void copyFile(File from, File to)
     throws Exception
    {
     System.err.println("copy: " + from.getPath() + " -> " + to.getPath());
     byte[] buf = new byte[4096];
     FileInputStream fis = new FileInputStream(from);
     FileOutputStream fos = new FileOutputStream(to);
     int i = 0;
     do {
         i = fis.read(buf);
         if (i != -1) {
          fos.write(buf, 0, i);
         }
     } while (i != -1);
     fis.close();
     fos.close();
    }
}










0
 
LVL 1

Accepted Solution

by:
klf earned 100 total points
ID: 7148233
  changed the copyFile to test to see if the from and to are the same.

 public static void copyFile(File from, File to)
     throws Exception
    {
     if (from.equals(to)) {
         throw new Exception(from.getName() + " and " + to.getName() +
                    " are the same file");
     }
     System.err.println("copy: " + from.getPath() + " -> " + to.getPath());
     byte[] buf = new byte[4096];
     FileInputStream fis = new FileInputStream(from);
     FileOutputStream fos = new FileOutputStream(to);
     int i = 0;
     do {
         i = fis.read(buf);
         if (i != -1) {
          fos.write(buf, 0, i);
         }
     } while (i != -1);
     fis.close();
     fos.close();
    }
0
 

Author Comment

by:yes4me
ID: 7149837
Thanks a lot.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now