Solved

Copy a folder to another folder

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 covers a step-by-step guide to install VisualVM launcher in eclipse.

744 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

12 Experts available now in Live!

Get 1:1 Help Now