The Artima Developer Community
Sponsored Link

Java Answers Forum
Copy files...

1 reply on 1 page. Most recent reply: May 20, 2002 2:57 PM by Thomas SMETS

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Marco

Posts: 2
Nickname: marcos
Registered: May, 2002

Copy files... Posted: May 20, 2002 4:07 AM
Reply to this message Reply
Advertisement
Can I copy a file from a directory to another one with Java?


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Copy files... Posted: May 20, 2002 2:57 PM
Reply to this message Reply
yeap !
Just query the forum & you will find all you need in there ! I gave some time back a solution to the problem to some body having problem with buffered Reader / Witers

Thomas,

import java.io.*;
// From jakarta.apache.org/log4J
// The minimum version is 1.2
import org.apache.log4j.Category;
import org.apache.log4j.BasicConfigurator;
 
/**
  * This code is free of any copyrights (not even GPL).
  * 
  * It would just be nice if you could mention the author as follow :
  * @author <a href="mailto:tsmets@altern.org">Thomas SMETS</a>, Copyright Free.
  *
  */
public class TestFileReader
{
  public static final int BUFFER_SIZE = 4096;
  public static final int ERR = 1;
  public static final int EOF = -1;
  public static final int FILE_ENDED = 0;
  
  public static final String ENCODING = "US-ASCII"; 
  public static final String USAGE 
  
    = "Usage :\njava TestFileReader <file_source> <file_destination>\n\n That's it ...\n";
 
  public static Category log = null;
 
/** 
  *
  */
  public static void main (String[] args)
  {
    BasicConfigurator.resetConfiguration ();
    BasicConfigurator.configure (); 
    log = Category.getInstance (TestFileReader.class);   
    
    if (args.length != 2)
    {
      usage();
      System.exit (ERR);
    }
    
    try
    {
      
      DataInputStream in = new DataInputStream(new FileInputStream(args[0]));      
      DataOutputStream out = new DataOutputStream (new FileOutputStream(args[1]));
      int read;
      byte[] b;
      while (( read = in.available ()) != FILE_ENDED)
      {
        b = new byte[read];
        in.read (b);
        out.write (b);
        
      }
      
      out.flush ();
      out.close ();
      in.close ();
      
      
      log.info ("File writer / Reader closed");
      
      if (check(args[0], args[1]))
        log.info ("Copy : OK");
      else 
        log.warn ("Copy : NOK");
        
    }catch(Exception e)
    {
      log.fatal ("Exception :" + e.getClass ().getName ());
      log.fatal ("Message :" + e.getMessage () );
    }  
  }
  
  
  public static void usage ()
  {
    log.error (USAGE);
  }
  
  public static boolean check(String srcFileName, String destFileName)
  {
    try 
    {
      log.info ("Checking ...");
      DataInputStream srcData 
        = new DataInputStream (new FileInputStream (srcFileName));
      DataInputStream destData 
        = new DataInputStream (new FileInputStream (destFileName));
      
      BufferedInputStream srcBIS = new BufferedInputStream (srcData);
      BufferedInputStream destBIS = new BufferedInputStream (destData);            
      log.info ("Stream created");      
      
      StringBuffer srcBuf = new StringBuffer(), destBuf = new StringBuffer ();
      String s = null;
      int read = 0;
      byte [] b;
      while ((read = srcBIS.available ()) != FILE_ENDED)
      {
        b = new byte [read];
        srcBIS.read (b);        
        srcBuf.append ( new String (b, ENCODING ));
        log.info ("Reading " + read + " bytes");
      }
      
      read = 0;
      while (( read = destBIS.available ()) != FILE_ENDED)
      {        
        b = new byte [read];
        destBIS.read (b);
        destBuf.append ( new String (b, ENCODING) );
        log.info ("Reading " + read + " bytes");
      } //       
            
      if (destBuf.toString ().equals (srcBuf.toString ()))
        return true;
      else 
        return false;        
    }    
    catch (Exception ex) 
    {
      log.warn ("Exception " + ex);
      return false;
    }        
  }  
}  

Flat View: This topic has 1 reply on 1 page
Topic: EJB with WebLoic Server 6.1 Previous Topic   Next Topic Topic: Draw rectangle with dotted lined

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use