Solved

Copy a folder to another folder

Posted on 2002-07-11
3
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

726 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