Solved

Copy a folder to another folder

Posted on 2002-07-11
3
270 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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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.

756 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