java.io

Class BufferedReader

Implemented Interfaces:
Closeable, Readable
Known Direct Subclasses:
LineNumberReader

public class BufferedReader
extends Reader

Read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.

In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,

 BufferedReader in
   = new BufferedReader(new FileReader("foo.in"));
 
will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient.

Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader.

Since:
JDK1.1
See Also:
FileReader, InputStreamReader

Field Summary

Fields inherited from class java.io.Reader

lock

Constructor Summary

BufferedReader(Reader in)
Create a buffering character-input stream that uses a default-sized input buffer.
BufferedReader(Reader in, int sz)
Create a buffering character-input stream that uses an input buffer of the specified size.

Method Summary

void
close()
Close the stream.
void
mark(int readAheadLimit)
Mark the present position in the stream.
boolean
markSupported()
Tell whether this stream supports the mark() operation, which it does.
int
read()
Read a single character.
int
read(cbuf[] , int off, int len)
Read characters into a portion of an array.
String
readLine()
Read a line of text.
boolean
ready()
Tell whether this stream is ready to be read.
void
reset()
Reset the stream to the most recent mark.
long
skip(long n)
Skip characters.

Methods inherited from class java.io.Reader

close, mark, markSupported, read, read, read, read, ready, reset, skip

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

BufferedReader

public BufferedReader(Reader in)
Create a buffering character-input stream that uses a default-sized input buffer.
Parameters:
in - A Reader
Usages and Demos :

View More Examples of BufferedReader(Reader in)
   1: 
   2:       BufferedReader contentReader = new BufferedReader(body.getReader());
   3:       String record = "";
   4:       while ((record = contentReader.readLine()) != null) {
   5:         StringTokenizer st = new StringTokenizer(record, ",");

View Full Code Here
   1:             } else if (arg instanceof Reader){
   2:                 BufferedReader br = new BufferedReader((Reader)arg);
   3:                 return br.readLine();
   4:         ...
   5:             } else if (arg instanceof InputStream){
   6:                 BufferedReader reader = new BufferedReader(CharacterEncoding.getReader((InputStream)arg, context));
   7:                 return reader.readLine();
   8:             } else {
   9:                 throw new IllegalArgumentException();

View Full Code Here
   1: 
   2:       BufferedReader contentReader = new BufferedReader(body.getReader());
   3:       while ((record = contentReader.readLine()) != null) {
   4:         StringTokenizer st = new StringTokenizer(record, ",");
   5:         while (st.hasMoreTokens()) {

View Full Code Here
   1: 
   2:       BufferedReader contentReader = new BufferedReader(body.getReader());
   3:       String record = "";
   4:       while ((record = contentReader.readLine()) != null) {
   5:         if (record.trim().length() > 0) {

View Full Code Here
   1:         Reader r = bestTextFlavor.getReaderForText(transferable);
   2:         br = new BufferedReader(r);
   3:         line = br.readLine();
   4:         ...
   5:         println("Data is an InputStream:");
   6:         br = new BufferedReader((Reader)transferable.getTransferData(readerFlavor));
   7:         line = br.readLine();
   8:         while (line != null) {
   9:           println(line);

View Full Code Here

BufferedReader

public BufferedReader(Reader in,
                      int sz)
Create a buffering character-input stream that uses an input buffer of the specified size.
Parameters:
in - A Reader
sz - Input-buffer size
Throws:
IllegalArgumentException - If sz is <= 0
Usages and Demos :

View More Examples of BufferedReader(Reader in,int sz)
   1:     public BufferedReader getBufferedReader() {
   2:         return new BufferedReader(pipedReader, 1);
   3:     }
   4: }

View Full Code Here
   1:     if (reader instanceof BufferedReader) br = (BufferedReader) reader;
   2:     else br = new BufferedReader(reader,BUFFER_INCREMENT);
   3:     m_bufsize = BUFFER_INCREMENT;
   4:     m_index = -1;
   5:     next();

View Full Code Here
   1:     {
   2:         reader = new BufferedReader(createReader(fileName, encoding));
   3:     }
   4:     
   5:     private Reader createReader(String fileName, String defaultEncoding) throws IOException

View Full Code Here
   1:     {
   2:         reader = new BufferedReader(createReader(file, encoding));
   3:     }
   4:     
   5:     private Reader createReader(File file, String defaultEncoding) throws IOException

View Full Code Here

Method Details

close

public void close()
            throws IOException
Close the stream.
Specified by:
close in interface Closeable
Overrides:
close in interface Reader
Throws:
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of close()
   1:     public static void main(String[] args) throws Exception {
   2:     BufferedReader in = new BufferedReader(
   3:                 new InputStreamReader(
   4:         ...
   5: 
   6:     in.close();
   7:     }
   8: }

View Full Code Here
   1:     {
   2:        BufferedReader in = new BufferedReader(
   3:                 new InputStreamReader(
   4:         ...
   5:       System.out.println(inputLine);
   6:       in.close();
   7:     }
   8: }

View Full Code Here
   1: public class GraczPlikowy implements Gracz {
   2:     public BufferedReader rd;
   3: 
   4:         ...
   5:     public GraczPlikowy(String nazwaGracza) throws Exception {
   6:         rd=new BufferedReader(new FileReader(nazwaGracza));
   7:     }
   8:         ...
   9:     public void stop() throws Exception {
  10:         rd.close();
  11:     }

View Full Code Here
   1:         URLConnection yc = yahoo.openConnection();
   2:         BufferedReader in = new BufferedReader(
   3:             new InputStreamReader(
   4:         ...
   5:             System.out.println(inputLine);
   6:         in.close();
   7:     }
   8: }

View Full Code Here
   1:       try {
   2:          BufferedReader in =new BufferedReader(new FileReader("c:\\Java\\test.txt"));
   3:          BufferedWriter out =new BufferedWriter(new FileWriter("c:\\Java\\test2.txt"));
   4:         ...
   5:          out.flush();;
   6:          in.close();
   7:         ...
   8:          out.close();
   9:       }

View Full Code Here

mark

public void mark(int readAheadLimit)
            throws IOException
Mark the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point.
Overrides:
mark in interface Reader
Parameters:
readAheadLimit - Limit on the number of characters that may be read while still preserving the mark. After reading this many characters, attempting to reset the stream may fail. A limit value larger than the size of the input buffer will cause a new buffer to be allocated whose size is no smaller than limit. Therefore large values should be used with care.
Throws:
IllegalArgumentException - If readAheadLimit is <320
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of mark(int readAheadLimit)
   1: 
   2:     public Alignment read(BufferedReader br) {
   3:         try {
   4:         ...
   5:             SequenceIterator seqs = null;
   6:             br.mark(200);
   7:             String line = br.readLine();
   8:             line = br.readLine();
   9:             br.reset();

View Full Code Here
   1:             huc.connect();
   2:             BufferedReader data =
   3:         ...
   4:                 new BufferedReader(new InputStreamReader(huc.getInputStream()));
   5:         ...
   6:             data.mark(1000);

View Full Code Here
   1:   public boolean read(
   2:     BufferedReader reader,
   3:     TagValueParser parser,
   4:         ...
   5:           while(true) {
   6:             reader.mark(1);
   7:             int c = reader.read();
   8:             if(c == -1) {
   9:               eof = true;

View Full Code Here
   1:         try {
   2:             BufferedReader bufferedReader = new BufferedReader(fileReader);
   3: 
   4:         ...
   5:         throws IOException, ParseException {
   6:         bufferedReader.mark(1000);
   7: 
   8:         try {
   9:             StreamTokenizer tokenizer = new StreamTokenizer(bufferedReader);

View Full Code Here
   1:     };
   2:     BufferedReader in;
   3:     String linebuf;
   4:         ...
   5:     protected void setReader(Reader r) throws IOException {
   6:         setReader(new BufferedReader(r));
   7:     }
   8:         ...
   9:         try {
  10:             in.mark(1024);
  11:         } catch (IOException e) {
  12:         ...
  13:     }
  14:     protected void setReader(BufferedReader r) throws IOException {

View Full Code Here

markSupported

public boolean markSupported()
Tell whether this stream supports the mark() operation, which it does.
Overrides:
markSupported in interface Reader
Usages and Demos :

View More Examples of markSupported()
   1:     public DebugReader(Reader reader) {
   2:         this.reader = new BufferedReader(reader);
   3:         this.saved = new StringBuffer();
   4:         ...
   5:     public boolean markSupported() {
   6:         return reader.markSupported();
   7:     }
   8:         ...
   9: 
  10:     private BufferedReader reader;
  11:     private StringBuffer saved;

View Full Code Here

read

public int read()
            throws IOException
Read a single character.
Overrides:
read in interface Reader
Returns:
The character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached
Throws:
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of read()
   1:         throws IOException {
   2:         BufferedReader bufferedReader = new BufferedReader(reader);
   3:         StringBuffer contentBuffer = new StringBuffer();
   4:         ...
   5:         while (true) {
   6:             int readResult = bufferedReader.read();
   7:             if (readResult == -1) {
   8:                 break;
   9:             }

View Full Code Here
   1: 
   2:       BufferedReader requestReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
   3:       ByteArrayOutputStream requestBA = new ByteArrayOutputStream();
   4:         ...
   5:       while (rch-- > 0){
   6:     requestBA.write(requestReader.read());   
   7:              }      
   8:    
   9:              HTTPRequest request = new HTTPRequest(requestBA.toByteArray());

View Full Code Here
   1:     StringReader source = new StringReader ("0123456789ABC");
   2:     BufferedReader br = new BufferedReader (source, 5);
   3:     char buf[] = new char [READLEN];
   4:         ...
   5:         b.mark(3);
   6:         b.read();
   7:         ...
   8:         b.read();
   9:         ...
  10:         b.read();

View Full Code Here
   1: import java.io.PrintStream;
   2: import java.io.BufferedReader;
   3: import java.io.IOException;
   4:         ...
   5:     private PrintStream m_os;
   6:     private BufferedReader m_is;
   7:     boolean m_trace;
   8:         ...
   9:         m_os = new PrintStream(m_socket.getOutputStream());
  10:         m_is = new BufferedReader(new java.io.InputStreamReader(m_socket.getInputStream()));
  11:         m_trace = true;
  12:         ...
  13:         } while (! tmp.equals("</cogitantanswer>"));
  14:         m_is.read();

View Full Code Here
   1: import java.io.InputStreamReader;
   2: import java.io.BufferedReader;
   3: import java.io.InputStream;
   4:         ...
   5:     private boolean mInteractive;
   6:     private BufferedReader buffRead;
   7: 
   8:         ...
   9:     mInteractive   =    true;
  10:     buffRead = new BufferedReader(new InputStreamReader(mInputStr));
  11:     }
  12:         ...
  13:     {
  14:     return buffRead.read();

View Full Code Here

read

public int read(cbuf[] ,
                int off,
                int len)
            throws IOException
Read characters into a portion of an array.

This method implements the general contract of the corresponding read method of the Reader class. As an additional convenience, it attempts to read as many characters as possible by repeatedly invoking the read method of the underlying stream. This iterated read continues until one of the following conditions becomes true:

  • The specified number of characters have been read,
  • The read method of the underlying stream returns -1, indicating end-of-file, or
  • The ready method of the underlying stream returns false, indicating that further input requests would block.
If the first read on the underlying stream returns -1 to indicate end-of-file then this method returns -1. Otherwise this method returns the number of characters actually read.

Subclasses of this class are encouraged, but not required, to attempt to read as many characters as possible in the same fashion.

Ordinarily this method takes characters from this stream's character buffer, filling it from the underlying stream as necessary. If, however, the buffer is empty, the mark is not valid, and the requested length is at least as large as the buffer, then this method will read characters directly from the underlying stream into the given array. Thus redundant BufferedReaders will not copy data unnecessarily.

Overrides:
read in interface Reader
Parameters:
off - Offset at which to start storing characters
len - Maximum number of characters to read
Returns:
The number of characters read, or -1 if the end of the stream has been reached
Throws:
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of read(cbuf[] ,int off,int len)
   1: import java.io.BufferedOutputStream;
   2: import java.io.BufferedReader;
   3: import java.io.InputStreamReader;
   4:         ...
   5:     FileInputStream fis = new FileInputStream(file);
   6:     BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
   7:     String temp = reader.readLine();
   8:         ...
   9: 
  10:     reader.read(chars, 0, len);
  11:     cert = new String(chars);

View Full Code Here
   1: import java.io.BufferedOutputStream;
   2: import java.io.BufferedReader;
   3: import java.io.InputStreamReader;
   4:         ...
   5:     FileInputStream fis = new FileInputStream(file);
   6:     BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
   7:     String temp = reader.readLine();
   8:         ...
   9: 
  10:     reader.read(chars, 0, len);
  11:     cert = new String(chars);

View Full Code Here
   1: 
   2:     static MsgIndication fromText(BufferedReader r)
   3:     {
   4:         ...
   5:             char[] cBuf = new char[len];
   6:             r.read(cBuf,0,len);
   7:             
   8:             StringACLCodec codec =  new StringACLCodec (new StringReader(new String(cBuf)),null);
   9:             mi.msg = codec.decode();

View Full Code Here
   1:       {
   2:         BufferedReader reader = new BufferedReader(
   3:                                     new FileReader(file));
   4:         ...
   5: 
   6:   private void readStyles(BufferedReader reader, String currLine)
   7:     throws IOException
   8:         ...
   9:     char[] buffer = new char[contentLength];
  10:     reader.read(buffer, 0, contentLength);

View Full Code Here
   1: 
   2: import java.io.BufferedReader;
   3: import java.io.IOException;
   4:         ...
   5:                 InputStream in = s.getInputStream();
   6:                 BufferedReader BR = new BufferedReader(new InputStreamReader(in));
   7:                 String input = null;
   8:         ...
   9:                 char[] content = new char[length];
  10:                 BR.read(content, 0, length);
  11:                 contentAsString = new String(content);

View Full Code Here

readLine

public String readLine()
            throws IOException
Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
Throws:
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of readLine()
   1: public class GraczPlikowy implements Gracz {
   2:     public BufferedReader rd;
   3: 
   4:         ...
   5:     public GraczPlikowy(String nazwaGracza) throws Exception {
   6:         rd=new BufferedReader(new FileReader(nazwaGracza));
   7:     }
   8:         ...
   9:     public String wez() throws Exception {
  10:         return rd.readLine();
  11:     }

View Full Code Here
   1:      InputStream in=connection.getInputStream();
   2:      BufferedReader br=new BufferedReader(new InputStreamReader(in));
   3:         ...
   4:      return br.readLine();
   5:   }
   6:   public Object getContent(URLConnection connection,Class[] classes)throws IOException{
   7:     InputStream in=connection.getInputStream();

View Full Code Here
   1:         System.out.println("d.isDaemon()="+d.isDaemon());
   2:         BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
   3:         System.out.println("Wating for CR");
   4:         ...
   5:         {
   6:             stdin.readLine();
   7:         }
   8:         catch(IOException e){}
   9:     }

View Full Code Here
   1: 
   2: import java.io.BufferedReader;
   3: import java.io.InputStreamReader;
   4:         ...
   5: 
   6:     private static BufferedReader in =
   7:         ...
   8:             new BufferedReader(new InputStreamReader(System.in));
   9: 
  10:         ...
  11:     public static String readLine() throws IOException{
  12:         return in.readLine();

View Full Code Here
   1:     private Process proces;
   2:     private BufferedReader rd;
   3:     private PrintWriter wt;
   4:         ...
   5:         proces = Runtime.getRuntime().exec(nazwaGracza);
   6:         rd = new BufferedReader(new InputStreamReader(proces.getInputStream()));
   7:         wt = new PrintWriter(proces.getOutputStream());
   8:         ...
   9:     public String wez() throws Exception {
  10:         return rd.readLine();
  11:     }

View Full Code Here

ready

public boolean ready()
            throws IOException
Tell whether this stream is ready to be read. A buffered character stream is ready if the buffer is not empty, or if the underlying character stream is ready.
Overrides:
ready in interface Reader
Throws:
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of ready()
   1:   private String filename;
   2:   private BufferedReader input;
   3: 
   4:         ...
   5:         input.close();
   6:       input = new BufferedReader(new FileReader(filename));
   7:     } catch (IOException ex) {
   8:         ...
   9:     try {
  10:       return input.ready();
  11:     } catch (IOException ex) {

View Full Code Here
   1: 
   2: import java.io.BufferedReader;
   3: import java.io.InputStream;
   4:         ...
   5:     public ASStreamIterator(InputStream in) {
   6:         inStream = new BufferedReader(new InputStreamReader(in));
   7:     }
   8:         ...
   9:     public ASStreamIterator(Reader in) {
  10:         inStream = new BufferedReader(in);
  11:     }
  12:         ...
  13:     public boolean hasMoreLines() {
  14:         try { return inStream != null && inStream.ready(); }

View Full Code Here
   1:     public DebugReader(Reader reader) {
   2:         this.reader = new BufferedReader(reader);
   3:         this.saved = new StringBuffer();
   4:         ...
   5:     public boolean ready() throws IOException {
   6:         return reader.ready();
   7:     }
   8:         ...
   9: 
  10:     private BufferedReader reader;
  11:     private StringBuffer saved;

View Full Code Here
   1: 
   2: import java.io.BufferedReader;
   3: import java.io.BufferedWriter;
   4:         ...
   5:         PipedReader pr = new PipedReader();
   6:         BufferedReader br = new BufferedReader(pr);
   7:         BufferedWriter bw = new BufferedWriter(pw);
   8:         ...
   9:         boolean ready() throws IOException {
  10:             return br.ready();
  11:         }

View Full Code Here
   1:   {
   2:     BufferedReader rin=new BufferedReader
   3:       (new InputStreamReader(fiji_.getInput()));
   4:         ...
   5: 
   6:     while(!rin.ready())
   7:     {
   8:       try { Thread.sleep(100); }
   9:       catch(InterruptedException ex) { }

View Full Code Here

reset

public void reset()
            throws IOException
Reset the stream to the most recent mark.
Overrides:
reset in interface Reader
Throws:
IOException - If the stream has never been marked, or if the mark has been invalidated
Usages and Demos :

View More Examples of reset()
   1: 
   2: import java.io.BufferedReader;
   3: import java.io.File;
   4:         ...
   5: public class CodeTemplate {
   6:     private BufferedReader input;
   7: 
   8:         ...
   9:     public CodeTemplate (BufferedReader reader) {
  10:     input = reader ;
  11:         ...
  12:         try {
  13:         input.reset();

View Full Code Here
   1:     StringReader source = new StringReader ("0123456789ABC");
   2:     BufferedReader br = new BufferedReader (source, 5);
   3:     char buf[] = new char [READLEN];
   4:         ...
   5:         System.out.println ("read " + new String(buf));
   6:         br.reset();
   7:     }
   8:         ...
   9:         b.read();
  10:         b.reset();
  11:         b.mark(-1);
  12:         ...
  13:       if (read2 <= mark) {
  14:         bis.reset();

View Full Code Here
   1:     public DebugReader(Reader reader) {
   2:         this.reader = new BufferedReader(reader);
   3:         this.saved = new StringBuffer();
   4:         ...
   5:     public void reset() throws IOException {
   6:         reader.reset();
   7:     }
   8:         ...
   9: 
  10:     private BufferedReader reader;
  11:     private StringBuffer saved;

View Full Code Here
   1: import java.io.Reader;
   2: import java.io.BufferedReader;
   3: import java.io.IOException;
   4:         ...
   5: 
   6:   private final BufferedReader br;
   7:   private int m_index, m_end, m_bufsize;
   8:         ...
   9:   CharIndexedReader(Reader reader, int index) {
  10:     if (reader instanceof BufferedReader) br = (BufferedReader) reader;
  11:     else br = new BufferedReader(reader,BUFFER_INCREMENT);
  12:         ...
  13:       if (m_index != -1) {
  14:     br.reset();

View Full Code Here
   1: 
   2: import java.io.BufferedReader;
   3: import java.io.IOException;
   4:         ...
   5:     
   6:     public static Properties oldParseLog(BufferedReader reader) throws IOException {
   7:         Properties props = new Properties();
   8:         ...
   9:         }
  10:         reader.reset();
  11:         if (headerList.size() > 4) {

View Full Code Here

skip

public long skip(long n)
            throws IOException
Skip characters.
Overrides:
skip in interface Reader
Parameters:
n - The number of characters to skip
Returns:
The number of characters actually skipped
Throws:
IllegalArgumentException - If n is negative.
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of skip(long n)
   1:     public DebugReader(Reader reader) {
   2:         this.reader = new BufferedReader(reader);
   3:         this.saved = new StringBuffer();
   4:         ...
   5:     public long skip(long n) throws IOException {
   6:         return reader.skip(n);
   7:     }
   8:         ...
   9: 
  10:     private BufferedReader reader;
  11:     private StringBuffer saved;

View Full Code Here
   1: import java.io.Reader;
   2: import java.io.BufferedReader;
   3: import java.io.IOException;
   4:         ...
   5: 
   6:   private final BufferedReader br;
   7:   private int m_index, m_end, m_bufsize;
   8:         ...
   9:   CharIndexedReader(Reader reader, int index) {
  10:     if (reader instanceof BufferedReader) br = (BufferedReader) reader;
  11:     else br = new BufferedReader(reader,BUFFER_INCREMENT);
  12:         ...
  13:     br.mark(m_bufsize);
  14:     br.skip(index-1);

View Full Code Here
   1: 
   2:         BufferedReader br = new BufferedReader(new PseudoReader());
   3:         try {
   4:         ...
   5:         StringReader sr = new StringReader("sr");
   6:         br = new BufferedReader(sr);
   7: 
   8:         ...
   9:         try {
  10:             br.skip(1);
  11:         }
  12:         ...
  13:         try {
  14:             System.out.println(br.skip(5));

View Full Code Here
   1:             
   2:             BufferedReader reader = new BufferedReader(
   3:                 new InputStreamReader(new FileInputStream(f))
   4:         ...
   5:             
   6:             reader.skip(beginIndex);
   7:             if(endIndex == EOF) {
   8:                 StringBuffer data = new StringBuffer();
   9:                 int c = reader.read(); 

View Full Code Here