Author: saqibkhan

  • BufferedWriter 

    Introduction

    The Java.io.BufferedWriter class writes text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.Following are the important points about BufferedWriter −

    • The buffer size may be specified, or the default size may be used.
    • A Writer sends its output immediately to the underlying character or byte stream.

    Class declaration

    Following is the declaration for Java.io.BufferedWriter class −

    public class BufferedWriter
       extends Writer
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for Java.io.BufferedWriter class −

    • protected Object lock − This is the object used to synchronize operations on this stream.

    Class constructors

    Sr.No.Constructor & Description
    1BufferedWriter(Writer out)This creates a buffered character-output stream that uses a default-sized output buffer.
    2BufferedWriter(Writer out, int sz)This creates a new buffered character-output stream that uses an output buffer of the given size.

    Class methods

    Sr.No.Method & Description
    1void close()This method closes the stream, flushing it first.
    2void flush()This method flushes the stream.
    3void newLine()This method writes a line separator.
    4void write(char[] cbuf, int off, int len)This method writes a portion of an array of characters.
    5void write(int c)This method writes a single character.
    6void write(String s, int off, int len)This method writes a portion of a String.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.Writer
    • Java.io.Object
  • BufferedReader 

    Introduction

    The Java.io.BufferedReader class reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.Following are the important points about BufferedReader −

    • The buffer size may be specified, or the default size may be used.
    • Each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream.

    Class declaration

    Following is the declaration for Java.io.BufferedReader class −

    public class BufferedReader
       extends Reader
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for Java.io.BufferedReader class −

    • protected Object lock − This is the object used to synchronize operations on this stream.

    Class constructors

    Sr.No.Constructor & Description
    1BufferedReader(Reader in)This creates a buffering character-input stream that uses a default-sized input buffer.
    2BufferedReader(Reader in, int sz)This creates a buffering character-input stream that uses an input buffer of the specified size.

    Class methods

    Sr.No.Method & Description
    1void close()This method closes the stream and releases any system resources associated with it.
    2void mark(int readAheadLimit)This method marks the present position in the stream.
    3boolean markSupported()This method tells whether this stream supports the mark() operation, which it does.
    4int read()This method reads a single character.
    5int read(char[] cbuf, int off, int len)This method reads characters into a portion of an array.
    6String readLine()This method reads a line of text.
    7boolean ready()This method tells whether this stream is ready to be read.
    8void reset()This method resets the stream.
    9long skip(long n)This method skips characters.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.Reader
    • Java.io.Object
  • BufferedOutputStream 

    Introduction

    The Java.io.BufferedOutputStream class implements a buffered output stream. By setting up such an output stream, an application can write bytes to the underlying output stream without necessarily causing a call to the underlying system for each byte written.

    Class declaration

    Following is the declaration for Java.io.BufferedOutputStream class −

    public class BufferedOutputStream
       extends FilterOutputStream
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for Java.io.BufferedOutputStream class −

    • protected byte[] buf − This is the internal buffer where data is stored.
    • protected int count − This is the number of valid bytes in the buffer.
    • protected OutputStream out − This is the underlying output stream to be filtered.

    Class constructors

    Sr.No.Constructor & Description
    1BufferedOutputStream(OutputStream out)This creates a new buffered output stream to write data to the specified underlying output stream.
    2BufferedOutputStream(OutputStream out, int size)This creates a new buffered output stream to write data to the specified underlying output stream with the specified buffer size.

    Class methods

    Sr.No.Method & Description
    1void flush()This method flushes this buffered output stream.
    2void write(byte[] b, int off, int len)This method writes len bytes from the specified byte array starting at offset off to this buffered output stream.
    3void write(int b)This method writes the specified byte to this buffered output stream.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.FilterOutputStream
    • Java.io.Object
  • BufferedInputStream Class

    Introduction

    The Java.io.BufferedInputStream class adds functionality to another input stream, the ability to buffer the input and to support the mark and reset methods. Following are the important points about BufferedInputStream −

    • When the BufferedInputStream is created, an internal buffer array is created.
    • As bytes from the stream are read or skipped, the internal buffer is refilled as necessary from the contained input stream, many bytes at a time.

    Class declaration

    Following is the declaration for Java.io.BufferedInputStream class −

    public class BufferedInputStream
       extends FilterInputStream
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for Java.io.BufferedInputStream class −

    • protected byte[] buf − This is the internal buffer array where the data is stored.
    • protected int count − This is the index one greater than the index of the last valid byte in the buffer.
    • protected int marklimit − This is the maximum read ahead allowed after a call to the mark method before subsequent calls to the reset method fail.
    • protected int markpos − This is the value of the pos field at the time the last mark method was called.
    • protected int pos − This is the current position in the buffer.
    • protected InputStream in − This is the input stream to be filtered.

    Class constructors

    Sr.No.Constructor & Description
    1BufferedInputStream(InputStream in)This creates a BufferedInputStream and saves its argument, the input stream in, for later use.
    2BufferedInputStream(InputStream in, int size)This creates a BufferedInputStream with the specified buffer size, and saves its argument, the input stream in, for later use.

    Class methods

    Sr.No.Method & Description
    1int available()This method returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.
    2void close()This method closes this input stream and releases any system resources associated with the stream.
    3void mark(int readlimit)This method see the general contract of the mark method of InputStream.
    4boolean markSupported()This method tests if this input stream supports the mark and reset methods.
    5int read()This method reads the next byte of data from the input stream.
    6int read(byte[] b, int off, int len)This method reads bytes from this byte-input stream into the specified byte array, starting at the given offset.
    7void reset()This method repositions this stream to the position at the time the mark method was last called on this input stream.
    8long skip(long n)This method skips over and discards n bytes of data from this input stream.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.FilterInputStream
    • Java.io.Object
  • Implementing Decade Using Ennead Class

    Problem Description

    How to implement Decade class using Ennead class?

    Example

    Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

    Create a java class file named TupleTester in C:\>JavaTuples.

    File: TupleTester.java

    package com.tutorialspoint;
    import org.javatuples.Decade;
    import org.javatuples.Ennead;
    public class TupleTester {
       public static void main(String args[]){
    
      Ennead<Integer, Integer, Integer, Integer, Integer, Integer,
         Integer, Integer, Integer> ennead = Ennead.with(5,6,7,8,9,10,11,12,13);
      System.out.println(ennead);
      Decade<Integer, Integer, Integer, Integer, Integer, Integer, 
         Integer, Integer, Integer, String> decade = ennead.add("test");
      
      Decade<String, Integer, Integer, Integer, Integer, Integer, 
         Integer, Integer, Integer, Integer> decade1 = ennead.addAt0("test");
      
      System.out.println(decade);
      System.out.println(decade1);
    } }

    Verify the result

    Compile the classes using javac compiler as follows −

    C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java
    

    Now run the TupleTester to see the result −

    C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Output

    Verify the Output

    [5, 6, 7, 8, 9, 10, 11, 12, 13]
    [5, 6, 7, 8, 9, 10, 11, 12, 13, test]
    [test, 5, 6, 7, 8, 9, 10, 11, 12, 13]
    
  • Implementing Ennead Using Octet Class

    Problem Description

    How to implement Ennead class using Octet class?

    Example

    Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

    Create a java class file named TupleTester in C:\>JavaTuples.

    File: TupleTester.java

    package com.tutorialspoint;
    import org.javatuples.Ennead;
    import org.javatuples.Octet;
    public class TupleTester {
       public static void main(String args[]){
    
      Octet<Integer, Integer, Integer, Integer, Integer, Integer,
         Integer, Integer> octet = Octet.with(5,6,7,8,9,10,11,12);
      System.out.println(octet);
      Ennead<Integer, Integer, Integer, Integer, Integer, Integer, 
         Integer, Integer, String> ennead = octet.add("test");
      Ennead<String, Integer, Integer, Integer, Integer, Integer, 
         Integer, Integer, Integer> ennead1 = octet.addAt0("test");
      System.out.println(ennead);
      System.out.println(ennead1);
    } }

    Verify the result

    Compile the classes using javac compiler as follows −

    C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java
    

    Now run the TupleTester to see the result −

    C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Output

    Verify the Output

    [5, 6, 7, 8, 9, 10, 11, 12]
    [5, 6, 7, 8, 9, 10, 11, 12, test]
    [test, 5, 6, 7, 8, 9, 10, 11, 12]
    
  • Implementing Octet using Septet Class

    Problem Description

    How to implement Octet class using Septet class?

    Example

    Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

    Create a java class file named TupleTester in C:\>JavaTuples.

    File: TupleTester.java

    package com.tutorialspoint;
    import org.javatuples.Octet;
    import org.javatuples.Septet;
    public class TupleTester {
       public static void main(String args[]){
    
      Septet<Integer, Integer, Integer, Integer, Integer, Integer,
         Integer> septet = Septet.with(5,6,7,8,9,10,11);
      System.out.println(septet);
      Octet<Integer, Integer, Integer, Integer, Integer, Integer, 
         Integer, String> octet = septet.add("test");
      Octet<String, Integer, Integer, Integer, Integer, Integer, 
         Integer, Integer> octet1 = septet.addAt0("test");
      System.out.println(octet);
      System.out.println(octet1);
    } }

    Verify the result

    Compile the classes using javac compiler as follows −

    C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java
    

    Now run the TupleTester to see the result −

    C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Output

    Verify the Output

    [5, 6, 7, 8, 9, 10, 11]
    [5, 6, 7, 8, 9, 10, 11, test]
    [test, 5, 6, 7, 8, 9, 10, 11]
  • Implementing Septet using Sextet Class

    Problem Description

    How to implement Septet class using Sextet class?

    Example

    Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

    Create a java class file named TupleTester in C:\>JavaTuples.

    File: TupleTester.java

    package com.tutorialspoint;
    import org.javatuples.Septet;
    import org.javatuples.Sextet;
    public class TupleTester {
       public static void main(String args[]){
    
      Sextet<Integer, Integer, Integer, Integer, Integer, Integer> sextet 
         = Sextet.with(5,6,7,8,9,10);
      System.out.println(sextet);
      Septet<Integer, Integer, Integer, Integer, Integer, Integer, String> 
         septet = sextet.add("test");
      Septet<String, Integer, Integer, Integer, Integer, Integer, Integer> 
         septet1 = sextet.addAt0("test");
      System.out.println(septet);
      System.out.println(septet1);
    } }

    Verify the result

    Compile the classes using javac compiler as follows −

    C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java
    

    Now run the TupleTester to see the result −

    C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Output

    Verify the Output

    [5, 6, 7, 8, 9, 10]
    [5, 6, 7, 8, 9, 10, test]
    [test, 5, 6, 7, 8, 9, 10]
  • Implementing Sextet Using Quintet Class

    Problem Description

    How to implement Sextet class using Quintet class?

    Example

    Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

    Create a java class file named TupleTester in C:\>JavaTuples.

    File: TupleTester.java

    package com.tutorialspoint;
    import org.javatuples.Quintet;
    import org.javatuples.Sextet;
    public class TupleTester {
       public static void main(String args[]){
    
      Quintet<Integer, Integer, Integer, Integer, Integer> quintet 
         = Quintet.with(5,6,7,8,9);
      System.out.println(quintet);
      Sextet<Integer, Integer, Integer, Integer, Integer, String> sextet 
         = quintet.add("test");
      Sextet<String, Integer, Integer, Integer, Integer, Integer> sextet1 
         = quintet.addAt0("test");
      System.out.println(sextet);
      System.out.println(sextet1);
    } }

    Verify the result

    Compile the classes using javac compiler as follows −

    C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java
    

    Now run the TupleTester to see the result −

    C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Output

    Verify the Output

    [5, 6, 7, 8, 9]
    [5, 6, 7, 8, 9, test]
    [test, 5, 6, 7, 8, 9]
  • Implementing Quintet Using Quartet Class

    Problem Description

    How to implement Quintet class using Quartet class?

    Example

    Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

    Create a java class file named TupleTester in C:\>JavaTuples.

    File: TupleTester.java

    package com.tutorialspoint;
    import org.javatuples.Quintet;
    import org.javatuples.Quartet;
    public class TupleTester {
       public static void main(String args[]){
    
      Quartet<Integer, Integer, Integer, Integer> quartet = Quartet.with(5,6,7,8);
      System.out.println(quartet);
      Quintet<Integer, Integer, Integer, Integer, String> quintet = quartet.add("test");
      Quintet<String, Integer, Integer, Integer, Integer> quintet1 = quartet.addAt0("test");
      System.out.println(quintet);
      System.out.println(quintet1);
    } }

    Verify the result

    Compile the classes using javac compiler as follows −

    C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java
    

    Now run the TupleTester to see the result −

    C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Output

    Verify the Output

    [5, 6, 7, 8]
    [5, 6, 7, 8, test]
    [test, 5, 6, 7, 8]