Thomas SMETS
Posts: 307
Nickname: tsmets
Registered: Apr, 2002
|
|
Re: Copy files...
|
Posted: May 20, 2002 2:57 PM
|
|
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;
}
}
}
|
|