Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,335 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,831 people online right now. Registration is fast and FREE... Join Now!




java server copying files from a directory to another

 
Reply to this topicStart new topic

java server copying files from a directory to another

franckesh007
25 Aug, 2008 - 06:15 AM
Post #1

New D.I.C Head
*

Joined: 25 Aug, 2008
Posts: 4

hi,
I'm trying to program a server , that will copy files from a repository to another. I have 2 repositories that contain several files. An update in one repository should be paste in the repository via my server. I'm a beginner in java. I have tried this, but the code doesn't recognize my repositories :

CODE
import java.io.*;

  public class CopyDirectory{

public static void main(String[] args) throws IOException{

   CopyDirectory cd = new CopyDirectory();
    BufferedReader in = new BufferedReader
                        (new InputStreamReader(System.in));
    System.out.println("Enter the source directory or file name : ");

      String source = in.readLine();

    File src = new File(source);
  
    System.out.println("Enter the destination directory or file name : ");
    String destination = in.readLine();
  
      File dst = new File(destination);

    cd.copyDirectory(src, dst);

  }
  

  public void copyDirectory(File srcPath, File dstPath)
                               throws IOException{
  
  if (srcPath.isDirectory()){

      if (!dstPath.exists()){

        dstPath.mkdir();

     }


     String files[] = srcPath.list();
  
    for(int i = 0; i < files.length; i++){
        copyDirectory(new File(srcPath, files[i]),
                     new File(dstPath, files[i]));
  
      }

    }

   else{
  
      if(!srcPath.exists()){

        System.out.println("File or directory does not exist.");

       System.exit(0);

      }
      
else
  
      {

       InputStream in = new FileInputStream(srcPath);
       OutputStream out = new FileOutputStream(dstPath);
                     // Transfer bytes from in to out
            byte[] buf = new byte[1024];

              int len;

           while ((len = in.read(buf)) > 0) {
  
          out.write(buf, 0, len);

        }

       in.close();

           out.close();

      }

   }
  
System.out.println("Directory copied.");

}

}


Thanks,
Franck

User is offlineProfile CardPM
+Quote Post

JeroenFM
RE: Java Server Copying Files From A Directory To Another
25 Aug, 2008 - 09:14 AM
Post #2

D.I.C Head
Group Icon

Joined: 30 Jun, 2008
Posts: 186



Thanked: 9 times
Dream Kudos: 100
My Contributions
What exactly do you mean by a repository?

Also, one rather obvious thing I noticed is that you're not flushing your OutputStream. Before you close it:

java

out.flush();

User is offlineProfile CardPM
+Quote Post

franckesh007
RE: Java Server Copying Files From A Directory To Another
27 Aug, 2008 - 01:23 PM
Post #3

New D.I.C Head
*

Joined: 25 Aug, 2008
Posts: 4

Hi,
Repository is like a fileserver where a client makes the information available to others or receives information from others.
My program seems to copy just a file, but not a whole repository. I don't know how to program a server that will able to handle folders-copy and paste between my 2 repositories.
Thank u
Franck
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 07:58AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month