The Artima Developer Community
Sponsored Link

Java Answers Forum
file reader

4 replies on 1 page. Most recent reply: Apr 30, 2002 3:12 AM by Maysoon

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 4 replies on 1 page
Maysoon

Posts: 64
Nickname: hm
Registered: Mar, 2002

file reader Posted: Apr 25, 2002 2:11 PM
Reply to this message Reply
Advertisement
hello all..

when i copy an image file (bmp) to another file

then display a copied file it appeares distorted?why?

here is the code.


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){}


and when i copy a pdf or jpg file ,the file is copied

and the original and copied file have the same size ,
but when i display the copied one the following message
appears "can not read the <file name>"


what is the problem..
thanks


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: file reader Posted: Apr 26, 2002 4:09 AM
Reply to this message Reply
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;
    }        
  }  
}  

Maysoon

Posts: 64
Nickname: hm
Registered: Mar, 2002

Re: file reader Posted: Apr 28, 2002 12:06 AM
Reply to this message Reply
hi thomas..

thank you very much for the code but did you try it?

i have tried the code and run it but the following occurs:


1- all (bmp) images copied ,but they are distorted.

2- all(jpg) images could not copied and while running
the program the following message in which in your code
appears copy : No

knowing that i have seen that the copied file and the original image
have the same size but the copied one can not open
when opening it in (paint or imaging) program ,error messages
appears


3- when coping pdf files ,they copied but they open
without any thing in them???


so what is the problem?!!

please help me,becuase i sould to do an attachment function
between my client/server application using sockets programming


thanks alot..

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: file reader Posted: Apr 28, 2002 8:46 AM
Reply to this message Reply
Maysoon,

I tried the program on a few documents types I had available !
Did you run the program on your BMP's / JPEG's with the equality check turned "on" ?
What's the output ?

Also, in
      while ((read = srcBIS.available ()) != FILE_ENDED)
      {
        b = new byte [read];
        srcBIS.read (b);        
        srcBuf.append ( new String (b, ENCODING ) );
        log.info ("Reading " + read + " bytes");
      }


Have you tried to change the value of the ENCODING ?
What JVM version are you running on ?

This was test on 1.3.
Otherwise try this !
import java.io.*;
 
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;
    }        
  }  
}  


It works with PDF's & BMP's
Just say when you want working examples :-D


Thomas,

Maysoon

Posts: 64
Nickname: hm
Registered: Mar, 2002

Re: file reader Posted: Apr 30, 2002 3:12 AM
Reply to this message Reply
hello Thomas..
thank you very much for your code ,it works fine (the second one)
for all types of images .

but i do not know why the first one does not work fine although i have jdk1.3
jdk1.3?


i have another question ?

how can i convert the byte[] that is read from the file to string
becuase i want to transfer the bytes to the server ,the server accepts
string only,
so i have to convert the byte to string then the server will convert the
string to bytes then to store it to the file


byte[] b;
while (( read = in.available ()) != FILE_ENDED)
{ b = new byte[read];
in.read (b);
here i have toconvert b to string
sendData(string);
//out.write (b);
}


thanks very much.

Flat View: This topic has 4 replies on 1 page
Topic: Bubblesort Previous Topic   Next Topic Topic: useful web sites interface java with PHP

Sponsored Links



Google
  Web Artima.com   

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