Hi all,

I have to write a synchronization utility which synchronizes a target folder with a source folder:

i.e. I have the following source directory structure and would like to synchronize it.

C:\source\
 |
 +----Folder_A
 |    |
 |    +---Folder_A1
 |    |     |
 |    |     +---File_AA1.txt
 |    |
 |    +---FileA1.txt
 |        
 +----Folder_B
 |    | 
 |    +---Folder_B1
 |    |   |
 |    |   +---File_BB1.txt
 |    |
 |    +---File_B1.txt
 |
 +-----File_A.txt

When I run my program it will create the follwoing structure but it doesn't copy the files

D:\target\
 |
 +----Folder_A
 |    |
 |    +---Folder_A1
 |          |
 |          +---File_AA1
 |
 |
 |        
 +----Folder_B
      | 
      +---Folder_B1
          |
          +---File_BB1

My program is as follow:

public class Sync {

    private static String strSource = "C:\\source";
    private static String strTarget = "D:\\target";
    
  public static void main( String[] args )  throws IOException {
      Sync h = new Sync ();      
      h.doSync (strSource, strTarget);      
  }
  
  public void doSync(String src, String trgt) throws IOException {
      File source = new File (src);
      File target = new File (trgt);
      String Src = "";
      String Trg = "";
      File[] listFile = source.listFiles();
      for (int i=0; i < listFile.length; i++) {
          if (listFile[i].isDirectory()) {
              if (!target.exists()) {
                  target.mkdir();
              }
              Src = listFile[i].getCanonicalPath();
              Trg = target.getCanonicalPath() + "\\" + listFile[i].getName();
              doSync(Src, Trg);                            
          } else {
              if (!target.exists()) {
                  FileInputStream from = new FileInputStream(listFile[i]);
                  target.createNewFile();
                  FileOutputStream to = new FileOutputStream(target.getName());
                  
                  byte[] buffer = new byte[4096];
                  int bytesRead;
                  while ((bytesRead = from.read(buffer)) != -1) {
                      to.write(buffer, 0, bytesRead);
                  }
                  from.close();
                  to.close();                  
              }
              Src = listFile[i].getCanonicalPath();
              Trg = target.getCanonicalPath();
          }
      }
  }
}

Notice the out put folder structure: it doesn't create File_A1.txt, File_B1.txt and File_A.txt also notice the extention is missing.

The other files which are created, but it's not copied form the source directory, instead, it is just a file with that name, but not the content of the file.

Can someone please take a look and comment as where am I going wrong.


Thanks,

check you code, variable 'target' is always pointing to a FOLDER name, it would never
be assigned to a pure file name.

Src = listFile.getCanonicalPath(); Trg = target.getCanonicalPath();

for these 2 lines, what the 'Src' and 'Trg' are supposed to be used for ?
seems that program is doing nothing except setting values to them

i tried the following, but still same result

Src = listFile[i].getCanonicalPath() + listFile[i].getName();
Trg = target.getCanonicalPath();

These two line is supposed to assign the next file/directory to be synched.

Can you give me a hint as what/how should it be?

A couple of System.out.println() statements at a few key points could probably illuminate exactly what it is trying to copy where in short order. If you are using an IDE with a debugger, stepping though would be even faster.

package dw_synch;
import java.io.*;   
public class Synch {
    private static String strSource = "C:\\source";
    private static String strTarget = "D:\\target";
    public static void main( String[] args ) throws IOException 
    {
        Synch h = new Synch ();
        h.doSync (new File(strSource),new File(strTarget));
    }

    public void doSync(File source, File target) throws IOException 
    {
        File[] listFile = source.listFiles();
        for (int i=0; i < listFile.length; i++) {
            File trgt = new File(target.getCanonicalPath() + File.separator + listFile[i].getName());
            source = listFile[i];
            if (source.isDirectory()) 
            {
                trgt.mkdir();
                doSync(source, trgt);
            }
            else 
            {//if it is a file this part works
                if (!trgt.exists()) 
                {
                    FileInputStream from = new FileInputStream(source);
                    target.createNewFile();
                    FileOutputStream to = new FileOutputStream(trgt);
                    byte[] buffer = new byte[4096];
                    int bytesRead;
                    try{
                        while ((bytesRead = from.read(buffer)) != -1) 
                            to.write(buffer, 0, bytesRead);
                    }//try
                    finally
                    {
                    from.close();
                    to.close();
                    }//finally
                 }//if
            }//else
          }//for
        }//doSync
     }//class

I just change some part of your existing code. I used File.seperator to get \ character. please check the specifications to learn why.

Thanks yilmazhuseyin,

It did the trick.

I only need to add abit to it so, if a file changes, it will update it.

boolean sameFile = trgt.equals(target);
if (!trgt.exists() || !sameFile) 
     // the rest of the code ...

Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.