KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > io > BufferedReader

java.io
Class BufferedReader

java.lang.Object
  extended by java.io.Reader
      extended by java.io.BufferedReader
All Implemented Interfaces:
Closeable, Readable
Direct Known Subclasses:
LineNumberReader
See Also:
Top Examples, Source Code, FileReader, InputStreamReader

public BufferedReader(Reader in)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[131]Process Buffered Read input stream
By ollie { at } appsaspeers { dot } com on 2004/09/08 17:10:40  Rate
// ... process your input stream 
 String resultStr = ""; 
 InputStream is = new InputStream ( System.in ) ; 
 InputStreamReader isr = new InputStreamReader ( is ) ; 
 java.io.BufferedReader br = new java.io.BufferedReader ( isr ) ;   
        
 System.out.println ( "Reading stream" ) ; 
 do  {  
     try  {  
   line =  ( String )  br.readLine (  ) ; 
      }  catch  ( IOException e )   {  
    System.out.println ( "IO Exception on Buffered Read" ) ; 
      } ; 
     resultStr += line + "\r\n"; 
   }  while  ( line != null ) ; 
         
  System.out.println ( "resultStr" ) ; 
 


[1120]_
By Anonymous on 2005/08/12 05:46:29  Rate
I was reading the example code for Buffered Readers here: 
   
 I could be wrong but I think the line that reads: 
   System.out.println ( \"resultStr\" ) ; 
  
  
 Should actually read: 
   System.out.println ( resultStr ) ; 
  
  
 The first example will actually print -- >     resultStr 
 instead of printing the results from the buffer. 
  
  
 --Jeffery Brannon 
 


[1680]An Input class for reading in primitive values like int, double, etc.
By Melvin Ninal { at } SWU, Cebu, Philippines on 2005/11/12 11:15:02  Rate
import java.io.*; 
  
  
 public class Input 
  {  
   /**Method for reading String inputs.*/ 
   public String readString (  )  
    {  
     BufferedReader buf = new BufferedReader ( new InputStreamReader ( System.in )  ) ; 
     String data=null; 
     try 
      {  
       data = buf.readLine (  ) ; 
      }  
     catch ( Exception e )    
      {  
       System.out.print ( "An error occured..." ) ; 
      }  
     return data; 
    }  
    
   /**Method for reading byte inputs.*/ 
   public byte readByte (  )  
    {  
     byte data = 0; 
     try 
      {  
       data = Byte.parseByte ( readString (  )  ) ; 
      }  
     catch ( NumberFormatException nfe )    
      {  
       System.out.print ( "You inputted suspicious data!\nTry again: " ) ; 
       data = readByte (  ) ; 
      }  
     return data; 
    }  
    
   /**Method for reading short integer inputs.*/ 
   public short readShort (  )  
    {  
     short data = 0; 
     try 
      {  
       data = Short.parseShort ( readString (  )  ) ; 
      }  
     catch ( NumberFormatException nfe )    
      {  
       System.out.print ( "You inputted suspicious data!\nTry again: " ) ; 
       data = readShort (  ) ; 
      }  
     return data; 
    }  
    
   /**Method for reading integer inputs.*/ 
   public int readInt (  )  
    {  
     int data = 0; 
     try 
      {  
       data = Integer.parseInt ( readString (  )  ) ; 
      }  
     catch ( NumberFormatException nfe )    
      {  
       System.out.print ( "You inputted suspicious data!\nTry again: " ) ; 
       data = readInt (  ) ; 
      }  
     return data; 
    }  
    
   /**Method for reading long integer inputs.*/ 
   public Long readLong (  )  
    {  
     long data = 0; 
     try 
      {  
       data = Long.parseLong ( readString (  )  ) ; 
      }  
     catch ( NumberFormatException nfe )    
      {  
       System.out.print ( "You inputted suspicious data!\nTry again: " ) ; 
       data = readLong (  ) ; 
      }  
     return data; 
    }  
    
   /**Method for reading single floating point number inputs.*/ 
   public float readFloat (  )  
    {  
     float data = 0; 
     try 
      {  
       data = Float.parseFloat ( readString (  )  ) ; 
      }  
     catch ( NumberFormatException nfe )    
      {  
       System.out.print ( "You inputted suspicious data!\nTry again: " ) ; 
       data = readFloat (  ) ; 
      }  
     return data; 
    }  
    
   /**Method for reading double floating point number inputs.*/ 
   public double readDouble (  )  
    {  
     double data = 0; 
     try 
      {  
       data = Double.parseDouble ( readString (  )  ) ; 
      }  
     catch ( NumberFormatException nfe )    
      {  
       System.out.print ( "You inputted suspicious data!\nTry again: " ) ; 
       data = readDouble (  ) ; 
      }  
     return data; 
    }  
    
   /**The main method that demostrates the use of each preceding method*/ 
   public static void main ( String args [  ]  )  
    {  
     Input in = new Input (  ) ; 
     System.out.print ( "Input a byte: " ) ; 
     System.out.println ( "The number you inputted times 2 equals "+in.readByte (  ) *2 ) ; 
      
     System.out.print ( "Input a short: " ) ; 
     System.out.println ( "The number you inputted times 2 equals "+in.readShort (  ) *2 ) ; 
      
     System.out.print ( "Input an int: " ) ; 
     System.out.println ( "The number you inputted times 2 equals "+in.readInt (  ) *2 ) ; 
      
     System.out.print ( "Input a long: " ) ; 
     System.out.println ( "The number you inputted times 2 equals "+in.readLong (  ) *2 ) ; 
      
     System.out.print ( "Input a float: " ) ; 
     System.out.println ( "The number you inputted times 2 equals "+in.readFloat (  ) *2 ) ; 
      
     System.out.print ( "Input a double: " ) ; 
     System.out.println ( "The number you inputted times 2 equals "+in.readDouble (  ) *2 ) ; 
      
     System.out.print ( "Input a string: " ) ; 
     System.out.println ( "The string you inputted has "+in.readString (  ) .length (  ) +" character ( s ) ." ) ; 
    }  
  } 


public BufferedReader(Reader in,
                      int sz)
See Also:
IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void close()
           throws IOException
See Also:
Reader, Closeable
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void mark(int readAheadLimit)
          throws IOException
See Also:
IllegalArgumentException, Reader
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[778]Mark the present position in the input stream
By tedy_sl { at } yahoo { dot } com on 2004/05/13 03:52:55  Rate
BufferedReader bf = new BufferedReader (  new FileReader (  "c:/temp.txt" )   ) ; 
 bf.mark (  ) ;


public boolean markSupported()
See Also:
Reader
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int read()
         throws IOException
See Also:
Reader
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int read(char[] cbuf,
                int off,
                int len)
         throws IOException
See Also:
Reader
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String readLine()
                throws IOException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[124]File in out
By akbari { at } linux { dot } co { dot } kr on 2002/11/20 01:00:48  Rate
import java.io.*; 
  
  
 public class fileinout  {  
  public static void main ( String args [  ]  )   {  
    try  {  
      // ???????????? ?????? 
       FileInputStream fis = new FileInputStream ( "text.txt" ) ; 
      BufferedReader br = new BufferedReader ( new InputStreamReader ( fis )  ) ; 
      while (  ( readline=br.readLine (  )  )  != null )  
      System.out.println ( readline ) ; 
  
  
      br.close (  ) ; 
     }  catch  ( IOException e )   {  
      System.out.println ( e ) ; 
     }  
   }  
  }  
 


[1397]Execute a shell command and capture the output
By nidhisaxena83 { at } hotmail { dot } com on 2005/04/19 05:58:49  Rate
I m trying to get part of the string in variabe,store it in a variable and use it as the return value for the mehtod this code is in.Any suggestions would be much appreciated.. 
  
  
 public String executeService ( String command,String url )  
    {  
     BufferedReader in = null; 
      
   try  {  
     Runtime r = Runtime.getRuntime (  ) ; 
     Process p = r.exec ( command + " " + url ) ; 
  
  
     if  ( p == null )   {  
     System.out.println ( "Could not connect" ) ; 
      }  
      
     in = new BufferedReader ( new InputStreamReader ( p.getInputStream (  )  )  ) ; 
  
  
     while (  ( line=in.readLine (  )  ) !=null )  {  
  
  
           System.out.println ( line ) ; 
          }  
    
   in.close (  ) ; 
        
   }  
     catch  ( IOException io )   {  
       System.err.println ( io.toString (  )  ) ; 
      }  
      return address; 
    }  
  
  
 


public boolean ready()
              throws IOException
See Also:
Reader
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void reset()
           throws IOException
See Also:
Reader
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public long skip(long n)
          throws IOException
See Also:
IllegalArgumentException, Reader
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags