Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Buffer and scanner


Published on

this presentation uploaded by arif mohmand
of Abasyn university peshawar

Published in: Software, Technology, Education
  • Be the first to comment

Buffer and scanner

  1. 1. Presented By: Arif Ullah(2630) Presented To: Sir Fasee Ullah ABASYN UNIVERSITY PESHAWAR
  2. 2. • Buffer : • Introduction • Stream • Input & output buffered • Example • Scanner: • Introduction • Scanner class • Methods • Example
  3. 3.  The 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 Buffered Reader:  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.
  4. 4.  A stream is a sequence of data. In Java a stream is composed of bytes. It's called a stream because it's like a stream of water that continues to flow.  Three streams are created for us automatically:  1) System.out: standard output stream  2) standard input stream  3) System.err: standard error  Input and Output in Java Input and Output (I/O) is used to process the input and produce the output based on the input. Java uses the concept of stream to make I/O operations fast. package contains all the classes required for input and output operations.
  5. 5.  Java application uses an output stream to write data to a destination, it may be a file,an array,peripheral device or socket.
  6. 6.  Java application uses an input stream to read data from a source, it may be a file,an array,peripheral device or socket.
  7. 7.  BufferedOutputStream used an internal buffer. It adds more efficiency than to write data directly into a stream. So, it makes the performance fast.  In the following example, we are writing the textual information in the Buffered Output Stream object which is connected to the File Output Stream object. The flush() flushes the data of one stream and send it into another. It is required if you have connected the one stream with another.
  8. 8. • import*; • class Test{ • public static void main(String args[]) • throws Exception{ • FileOutputStream fout=new FileOutputStream("f1.txt"); • BufferedOutputStream bout=new BufferedOutputStream(fout); • • String s="Sachin is my favourite player"; • byte b[]=s.getBytes(); • bout.write(b); • • bout.flush(); • bout.close(); • System.out.println("success"); • } • }
  9. 9. • import*; • class SimpleRead{ • public static void main(String args[]){ • try{ • FileInputStream fin=new FileInputStream("f1.txt"); • BufferedInputStream bin=new BufferedInputStream(fin); • int i; • while((!=-1) • System.out.println((char)i); • • fin.close(); • } • catch(Exception e){system.out.println(e);} • } • } • Output:Sachin is my favourite player • • Output:Sachin is my favourite player
  10. 10.  There are various ways to read input from the keyboard, the java.util.Scanner class is one of them. The Scanner class breaks the input into tokens using a delimiter which is whitespace bydefault. It provides many methods to read and parse various primitive values.  Allows the user to input the values of various types.  Defined within a package java.util.
  11. 11.  import java.util.Scanner; Creating scanner object  Scanner sc = new Scanner(;
  12. 12.  nextInt(): • receives the next token from scanner object which can be expressed as an integer and stored in integer type  nextFloat(): • receives the next token from scanner object which can be expressed as an floating and stored in float type
  13. 13.  nextDouble(): • receives the next token from scanner object which can be expressed as an double and stored in double type  nextLine(): • receives the next line of the string
  14. 14. • package scanner; • import java.util.Scanner; • public class scannerDemo { • void sum(int a, float b){ • double result=a+b; • System.out.println("the sum is = "+result); • } • public static void main(String[] args){ • Scanner scanner=new Scanner(; • System.out.println("input integer value:"); • int num1=scanner.nextInt(); • System.out.println("input double value:"); • float num2=scanner.nextFloat(); • scannerDemo scannerDemo=new scannerDemo(); • scannerDemo.sum(num1, num2); • } • }
  15. 15. • • • •