The Artima Developer Community
Sponsored Link

Java Answers Forum
BufferedReader - readLine Does not work

1 reply on 1 page. Most recent reply: Jun 2, 2002 1:29 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
Antonas

Posts: 11
Nickname: tony
Registered: May, 2002

BufferedReader - readLine Does not work Posted: Jun 2, 2002 9:49 AM
Reply to this message Reply
Advertisement
Hi,

I have problem with bufferedReader readLine(). It does not work.

So I did check if buferedStream is ready (ready()) method). So it says that input stream is not ready. What itmeans . Why it happens ?

Thank You

ToNy


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: BufferedReader - readLine Does not work Posted: Jun 2, 2002 1:29 PM
Reply to this message Reply
Here is two code samples :
First one is for text files
package test.io;
 
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.io.FileWriter;
 
import java.io.IOException;
 
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 ReadingLineAndProcessing
{
  public static Category log = Category.getInstance (ReadingLineAndProcessing.class);
  public static final String USAGE 
    = "java test.io.ReadingLineAndProcessing <aFileNameToProcess>";  
  public static final String PROPERTY_LINE_SEPARATOR = "line.separator";
  public static final String LINE_SEPARATOR = System.getProperty (PROPERTY_LINE_SEPARATOR);
  public static final String TEMP = "Temp_";
  
 
  public static void main (String[] args)  
    throws Exception
  {
    BasicConfigurator.resetConfiguration ();
    BasicConfigurator.configure ();
    
    if (args.length < 1)
      printUsage ();
    
    BufferedReader in = new BufferedReader(new FileReader( args[0] ));
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(TEMP + args[0])));
    
    String S, pS;
    while ((S = in.readLine ()) != null )
    {
      log.info ("Reading a line : " + S);
      pS = processString (S);
      out.write (pS);
      out.write (LINE_SEPARATOR);
    }  
    
    out.flush ();
    out.close ();
    in.close ();
    
    log.info ("Re-copying");
    
    in = new BufferedReader(new FileReader(TEMP + args[0] ));
    out = new PrintWriter(new BufferedWriter(new FileWriter(args[0])));
    
    while ((S = in.readLine ()) != null )
    {     
      out.write (S);
      out.write (LINE_SEPARATOR);
    }
        
    out.flush ();
    out.close ();
    in.close ();
    
    log.info ("Copied ...");
  }
  
  public static String processString (String s)
  {
    String processedS;        
    return processedS = s + s;
  }
  
  public static void printUsage ()
  {
    System.out.println (USAGE);
  }
}


Second one works with any type of Data :
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;
 
/** 
  * The entry point
  */
  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 () );
      usage ();
    }  
  }
  
  
  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;
    }        
  }  
}  

In both cases there is Buffering & it works fine !

Note that the java enlighter did not do all the character conversions right :-)

Rgds,

Thomas SMETS,
SCJP2 - Brussels

Flat View: This topic has 1 reply on 1 page
Topic: HOW TO INSTALL JTAPI Previous Topic   Next Topic Topic: where do I place utilies class assicated with a servlet

Sponsored Links



Google
  Web Artima.com   

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