WebNFS Developer's Guide

Recursive Copy

This program takes a file or a directory hierarchy and copies it somewhere else.

Usage:

% java rcopy src dest

Example B-2

import java.io.*;
import com.sun.xfile.*;
class rcopy {    
     static void copy(XFile src, XFile dst) throws IOException {
          if (src.isDirectory()) {            
               dst.mkdir();            
               String[] dirList = src.list();           
               for (int i = 0; i < dirList.length; i++)
                    String entry = dirList[i];      
                    copy(new XFile(src, entry), new XFile(dst, entry));
               }        
          } else {	                 // assume it's a file            
               XFileInputStream in  = new XFileInputStream(src);
               XFileOutputStream out = new XFileOutputStream(dst);

               int c;
               byte[] buf = new byte[32768];

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

               in.close();            
               out.close();  
          }    
     }

     public static void main(String av[]) {
          try {            
               copy(new XFile(av[0]), new XFile(av[1])); 
          } catch (IOException e) {	    
               System.err.println(e);     
                    e.printStackTrace();       
          }    
     }
}