Maysoon
Could you just spend some time to look what the problem could be for your self ?
Here is your problem is, You do :
try
{
FileReader fr = new FileReader("d:/image.jpg");
FileWriter pw = new FileWriter("d:/copyimage.jpg",true);
char c[] = new char[4096];
int read = 0;
while ((read = fr.read(c)) != -1)
pw.write( c);
fr.close();
pw.close();
}catch(Exception e)
{}
while in fact the loop should be :
char[] c = new char[BUFFER_SIZE];
char[] tempChar;
int read = 0;
while ((read = fr.read (c)) != -1)
{
tempChar = new char [read];
// This is the important part
System.arraycopy (c, 0, tempChar, 0, read);
log.info ("Read :" + read + " bytes.");
fw.write(tempChar);
log.info ("Wrote :" + read + " bytes.");
c = new char[BUFFER_SIZE];
}
In other word, you read a buffer of 4096 & you write it ! The proble is that it's okay as long as the file size is a multiple of the BUFFER_SIZE. Otherwise the file is written & increased to be a multiple of 4096.
Rgds,
Thomas SMETS
SCJP - Brussels
p.s. :
Here is the complete source:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import org.apache.log4j.Category;
import org.apache.log4j.BasicConfigurator;
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 = "UTF-8";
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
{
FileReader fr = new FileReader(args[0]);
FileWriter fw = new FileWriter(args[1], true);
char[] c = new char[BUFFER_SIZE];
char[] tempChar;
int read = 0;
while ((read = fr.read (c)) != -1)
{
tempChar = new char [read];
System.arraycopy (c, 0, tempChar, 0, read);
log.info ("Read :" + read + " bytes.");
fw.write(tempChar);
log.info ("Wrote :" + read + " bytes.");
c = new char[BUFFER_SIZE];
}
fw.flush ();
fr.close();
fw.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;
}
}
}