FileDocCategorySizeDatePackage
SafeFileCopier.javaAPI DocExample915Tue Feb 14 12:45:18 GMT 2006None

SafeFileCopier

public class SafeFileCopier extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidcopy(java.io.File inFile, java.io.File outFile)


    if (inFile.getCanonicalPath().equals(outFile.getCanonicalPath())) {
      // inFile and outFile are the same;
      // hence no copying is required.
      return;
    }

    InputStream in = null; 
    OutputStream out = null;
    
    try {
      in = new BufferedInputStream(new FileInputStream(inFile)); 
      out = new BufferedOutputStream(new FileOutputStream(outFile));
      for (int c = in.read(); c != -1; c = in.read()) {
        out.write(c);
      }
    }
    finally {
      if (in != null) in.close();
      if (out != null) out.close();
    }
  
public static voidmain(java.lang.String[] args)

    if (args.length != 2) {
      System.err.println("Usage: java FileCopier infile outfile");
    }
    else copy(new File(args[0]), new File(args[1]));