Solved

Copy a folder to another folder

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 96
replace a word with other 1 44
Oracle SQL syntax check  without executing 6 51
servlet example 17 30
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

776 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