Evo pošto nisam imao šta drugo da radim, pa reko da se malo pozabavim, napisao sam nešto što bi trebalo da radi. Inače, kopira folder i sve pod foldere unutar njega. Takođe i ja mislim da nema funkcije ( metode ) koja sama kopira folder.
Code:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class CopyFolder {
File source, dest;
public CopyFolder ( String sour, String dest) {
source = new File( sour );
this.dest = new File( dest );
if( !(source.isDirectory() && source.exists()) )
error();
if( !this.dest.exists() )
this.dest.mkdir();
copy();
}
void copy() {
copy( source.getPath() );
}
void copy( String from ) {
File dir[] = new File( from + "/." ).listFiles();
for( int i = 0; i < dir.length; i++ ) {
if( dir[i].isDirectory() ) {
new File( dest.getPath() + dir[i].getPath().substring( dest.getPath().length() ) ).mkdir();
copy( dir[i].getPath() );
}
else {
File fileFrom = new File( dir[i].getPath() );
File fileTo = new File( dest.getPath() + dir[i].getPath().substring( dest.getPath().length() ) );
try {
FileInputStream in = new FileInputStream( fileFrom );
FileOutputStream out = new FileOutputStream( fileTo );
byte data[] = new byte[(int)fileFrom.length()];
in.read( data );
out.write( data, 0, data.length );
in.close();
out.close();
}
catch( FileNotFoundException e ) {
e.printStackTrace();
System.exit(0);
}
catch( IOException e ) {
e.printStackTrace();
System.exit(0);
}
}
}
}
void error() {
System.err.println( "Neregularni parametri!\n" );
System.exit(0);
}
public static void main( String[] arg ) {
new CopyFolder( "/Prvi", "/Drugi" );
}
}
Ne bi trebalo da ima nikakvih grešaka, mada uvek je moguće. Treba ga više puta testirati. Normalno umesto
"/Prvi" i
"/Drugi", treba staviti odgovarajuće foldere, prvi je onaj koji se kopira, a drugi onaj gde se kopira.
Pozdrav.