package copyfile;
import java.io.*;
public class copyDirectory {
public static void main(String[] args)throws IOException {
String url1,url2;
if(args.length!=0){
if(args[0].length()==0||args[0].equals("")){
url1="d:/java";
}else{
//args[0].r
args[0].replace("//","/");
url1=args[0];
}
if(args.length<2){
///if(args[1].length()==0||args[1].equals("")){
url2="./";//"d:/javacopy";
// }
}
else
{
args[1].replace("//","/");
url2=args[1];
}
}
else
{
url1="d:/java";
url2="./";
System.out.println((new File(url2)).getAbsolutePath());
}
//System.out.println(url1);
//System.out.println(url2);
if((new File(url1)).exists()&&(new File(url1)).isFile()){
//(new File(url2)).;
//if((new File(url1)).exists()&&(new File(url2).isFile())){
// url2= url2+"/"+(new File(url1)).getName();
//}else
// {
System.out.println(args[1]);
System.out.println(args[1].indexOf("."));
if(args[1].indexOf(".")==0) {
//if(!(new File(url2).isFile())){
(new File(url2)).mkdirs();
url2=url2+"/"+(new File(url1)).getName();
}
else
{
//System.out.println(
(new File(url2)).createNewFile();
//);
}
//}
System.out.println(url2);
//(new File(url1)).mkdir();
FileInputStream tempIn=new FileInputStream(url1);
FileOutputStream tempOut=new FileOutputStream(url2);
byte[] tempb=new byte[1024*10];
int tempLen;
while((tempLen=tempIn.read(tempb))!=-1){
tempOut.write(tempb,0,tempLen);
}
tempOut.flush();
tempOut.close();
tempIn.close();
}
else
{
(new File(url2)).mkdirs();
File[] file= (new File(url1)).listFiles();
for(int i=0;i<file.length;i++){
if(file[i].isFile()){
FileInputStream input=new FileInputStream(file[i]);
FileOutputStream output=new FileOutputStream(url2+"/"+file[i].getName());
byte[] b=new byte[1024*5];
int len;
while((len=input.read(b))!=-1){
output.write(b,0,len);
System.out.println(i);
}
output.flush();
output.close();
input.close();
}
if(file[i].isDirectory()){
copyDirectory(url2+"/"+file[i].getName(),url1+"/"+file[i].getName());
}
}
}
}
public static void copyDirectory(String file1,String file2) throws IOException{
(new File(file1)).mkdirs();
File[] file=(new File(file2)).listFiles();
for(int i=0;i<file.length;i++){
if(file[i].isFile()){
FileInputStream input=new FileInputStream(file[i]);
FileOutputStream output=new FileOutputStream(file1+"/"+file[i].getName());
byte[] b=new byte[1024*5];
int len;
while((len=input.read(b))!=-1){
output.write(b,0,len);
//System.out.println(i);
}
output.flush();
output.close();
input.close();
}
if(file[i].isDirectory()){
copyDirectory(file1+"/"+file[i].getName(),file2+"/"+file[i].getName());
}
}
}
}