Copy a directory
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Patrick_Perli
Posted On:   Thursday, January 10, 2002 07:38 AM

How can I copy a directory recursively (like cp -r or xcopy /s)?

Re: Copy a directory

Posted By:   uwe_geercken  
Posted On:   Wednesday, January 23, 2002 04:06 AM

I have written an app that synchronizes two directories. a little bit like rsync but with a limited set of options.

if the destination directory is empty, it behaves like a directory copy app.

if somebody is interested in trying/testing it (has been tested on Windows NT/2000 and Linux) then please let me know.

regards,

uwe geercken

Re: Copy a directory

Posted By:   David_Saintloth  
Posted On:   Friday, January 11, 2002 06:50 PM

Patrick,

I've copied below code from some methods I wrote that do what you want. The only thing is they don't copy binary files correctly (images) so if you need them to copy non ascii datafiles you'll have to find out how to copy images. I just discovered I need them to copy binary files so I am working on a solution, I'll post it as soon as I figure it out.


import library: java.io.*;

Classes you should research: File, BufferedReader,BufferedWriter,FileReader,FileWriter

import library: gnu.regexp.*; (this is not a standard lib. you can get it from here: Open Source RegExp package )

Classes you should research: RE


The complete readFile method.
*****************************
comment: Straightforward use of File , FileReader and BufferedReader classes along with the RE class in the Utilities.replaceString method. (which you may not need)



public static String fileRead(String readfile) {
String line = "";
String contents = "";
// StringBuffer out = new StringBuffer();



//Read a files contents. the Utilities.replaceString static method is a regular
// expression method that just normalizes the input readfile path.
String filenormal = Utilities.replaceString(readfile,"\\","\\","ALL");
File file = new File(filenormal);
//Utilities.fileExists is just a wrapper method for File.
if (Utilities.fileExists(filenormal)) {
try {
BufferedReader br = new BufferedReader(new FileReader(readfile));

while ((line = br.readLine()) != null) {
// out.append(line + "
");
if (br.readLine() == null) {
contents += line;
} else {
contents += line + "
";
}
}
// return contents
// return out.toString();
return contents;

} catch (IOException e) {
// catch possible io errors from readLine()
e.printStackTrace();
return "Couldn't read the file or it doesn't exist now, but it did before I started reading.!";
}
} else {
return "This file doesn't exist.";
}
}



The complete fileWrite method.
******************************
Comment: Pretty straight forward and uses only the File, BufferedWriter and FileWriter classes.

  public static void fileWrite(String writefile,String data,boolean erasefirst,boolean makeifnotexist) {

//Write contents to a file.
if ((!Utilities.fileExists(writefile)) && (makeifnotexist)) Utilities.fileMakeFile(writefile);
//Erase the files contents.
if (erasefirst) Utilities.fileErase(writefile);

try {
//Create buffered writer, with the FileWriter in append mode.
BufferedWriter bw = new BufferedWriter(new FileWriter(writefile,true));
bw.write(data);
bw.newLine();
bw.close();

} catch (IOException ioe) {
//I couldn't do it.
System.out.println("I couldn't write the file.");
}

}



The complete copy method(s).
****************************
Comment: I have one "fileCopy" method that copies only files, this is called from a "Copy" method recursively when directories are encountered. The attribute lists of each method is self explanatory. You'll notice the Utility methods used for checking, you might want to create your own versions of these to make your code as unbreakable as possible. The "Utilities.fileList()" method used in "Copy" comes straight from the "list" method of the File Class.




public static boolean fileCopy(String copyfile,String copyto, boolean overwriteifexists) {
String filename = "";
copyto = Utilities.replaceString(copyto,"\\","\\","ALL");
String destination = "";
if (Utilities.fileExists(copyfile)) {
if (Utilities.matchString(copyfile,".")) {
filename = copyfile.substring(copyfile.lastIndexOf("\") + 1);
// System.out.println(filename + "
");
} else {
return false;
}
Utilities.fileMakeDirs(copyto);



if (Utilities.fileExists(copyto) && overwriteifexists) {
destination = copyto + "\" + filename;
Utilities.fileMakeFile(destination);
Utilities.fileErase(copyto);
// System.out.println(destination + "
");
Utilities.fileWrite(destination,Utilities.fileRead(copyfile),true,true);
return true;
} else {
// System.out.println("The File exists in destination, but I don't have permission to overwrite it." + "
");
return false;
}


} else {

//File doesn't exist or the path provided is a directory path instead of a file path.
// System.out.println("Source copy file doesn't exist, so obviously I can't copy it.");
return false;
}
}

/*
* Copies files AND directories using the Utility methods fileCopy() and fileList(), overwrites and recurses directory trees.
*/

public static boolean Copy(String copyfile, String copyto, boolean overwriteifexists, boolean recursefolders) {
//Members
boolean isdirectory = true;
String directory = "";
//Check the file is it a file or a directory? Set a boolean accordingly.
if (Utilities.matchString(copyfile,".")) {
directory = copyfile.substring(copyfile.lastIndexOf("\"));
isdirectory = false;
}


if (isdirectory) {
//It's a directory, get all the files inside the directory.
String[] holdem;
if (Utilities.fileList(copyfile) != null) {
holdem = Utilities.fileList(copyfile);
} else {
//Get out, the file path specified is invalid.
return false;
}
boolean val = false;

//For each file in the list, we will test the path if its a directory we recurse if its
// a file we call fileCopy. If "recursefolders" is not set we copy only one level into
// the dir structure.(I wanted this funtionality in my app. you may ommit it.)
for(int k=0;k < holdem.length;k++) {
String subpath = copyfile + "\" + holdem[k];
String recursepath = copyto + "\" + holdem[k];

if (recursefolders && Utilities.fileisDir(subpath)) {
val = Utilities.Copy(subpath,recursepath,overwriteifexists,recursefolders);
} else if (recursefolders && Utilities.fileisFile(subpath)) {
val = Utilities.fileCopy(subpath,copyto,overwriteifexists);
} else {
if (Utilities.matchString(subpath,".")) val = Utilities.fileCopy(subpath,copyto,overwriteifexists);

}
// System.out.println("Here is the directory I am at:: " + holdem[k] + " ::" + "
");
// System.out.print("Subpath is: " + subpath + " :" + "
");

}
return val;
} else {
//It's a regular file, copy it using the fileCopy method.
return Utilities.fileCopy(copyfile,copyto,overwriteifexists);
}
}



It shouldn't take too long for you to figure this out, I wrote all the code above in an afternoon(maybe a little longer)

Enjoy!
Regards

Dsl

Re: Copy a directory

Posted By:   Anonymous  
Posted On:   Thursday, January 10, 2002 08:16 AM

There are no methods to do that, so you have to write your own.

Shouldn't be a big problem, and maybe you can search on the net for some code.

Mainly, you need to create the new directory, then iterate through all the files in your original directory and copy them in the new one. When you find a subdirectory, you have to do the same thing, and so on.


Regards

About | Sitemap | Contact