Transcript
  • 7/31/2019 Buffered Byte & Char Stream

    1/13

    Click to edit Master subtitle style

    8/12/12

    Presented by,A. Julian Sathyadason

    Buffered Byte Stream&Buffered Character Stream

  • 7/31/2019 Buffered Byte & Char Stream

    2/13

    8/12/12

    BufferedInputStream class is used forbuffering the input and

    it supports operation to re-read the files.

    BufferedOutputStream class is used to buffer the output and

    enhance the performance.

    Reading/Writing using Bufferedbyte stream classes

  • 7/31/2019 Buffered Byte & Char Stream

    3/13

    8/12/12

    Methods ofBufferedInputStream

  • 7/31/2019 Buffered Byte & Char Stream

    4/13

    8/12/12

    Methods ofBufferedInputStream

  • 7/31/2019 Buffered Byte & Char Stream

    5/13

    8/12/12

    import java.io.*;

    class BufferedInOutStreamDemo {

    public static void main(String args[]) throwsIOException{

    FileInputStream fis=new FileInputStream(args[0]);

    BufferedInputStream bis=newBufferedInputStream(fis);

    int n=fis.available();

    bis.mark(n);

    System.out.println(Marked the stream);

    byte b[]=new byte[n];

    byte b1[]=new byte[n];

    bis.read(b);

    Example

  • 7/31/2019 Buffered Byte & Char Stream

    6/13

    8/12/12

    bis.reset();

    System.out.println(Reading the stream againfrom the marked point);

    bis.read(b1);

    System.out.println(new String(b1));

    bis.close();

    fis.close();

    /*Line 20*/ System.out.println(Writing contents

    to : +args[1]);FileOutputStream fos=new

    FileOutputStream(args[1]);

    BufferedOutputStream out=newBufferedOutputStream(fos);

    /*Line 23*/ out.write(b);

    Example (contd.)

  • 7/31/2019 Buffered Byte & Char Stream

    7/13

    8/12/12

    C:\javabook\programs\chap09>javaBufferedInOutStreamDemo sample.txt Demo.txt

    Marked the stream

    Contents of sample.txt: This is my sample file

    Resetting the stream

    Reading the stream again from the marked point

    This is my sample file

    Writing contents to : Demo.txt

    Contents written

    The Output

  • 7/31/2019 Buffered Byte & Char Stream

    8/13

    8/12/12

    BufferedReader class is used for buffering the input and

    it supports operation to re-read the files

    just like BufferedInputStream

    BufferedWriter class is used to

    buffer the output and

    enhance the performance

    just like BufferedOutputStreamLet us take an example to show these two classes:

    Reading/Writing using Bufferedcharacter stream classes

  • 7/31/2019 Buffered Byte & Char Stream

    9/13

    8/12/12

    import java.io.*;

    class BufferedReadWriteDemo {

    public static void main(String args[]) throwsIOException{

    int n;FileReader fr=new FileReader(args[0]);

    System.out.println(File opened: +args[0]);

    BufferedReader br=new BufferedReader(fr);

    PrintWriter out=new PrintWriter(System.out,true);

    String str;

    FileWriter fw= new FileWriter(args[1]);

    BufferedWriter bw=new BufferedWriter(fw);

    Example

  • 7/31/2019 Buffered Byte & Char Stream

    10/13

    8/12/12

    while((str=br.readLine())!=null){

    out.println(str);

    bw.write(str+\n);

    }

    System.out.println(Contents copied to:+args[1]);

    br.close();

    br.close();

    fr.close();

    fw.close(); }}

    Example (contd.)

  • 7/31/2019 Buffered Byte & Char Stream

    11/13

    8/12/12

    C:\javabook\programs\CHAP09~1>javaBufferedReadWriteDemo Sample1.txtDemo2.txt

    File opened: Sample1.txt

    This is my sample file

    Contents copied to: Demo2.txt

    The Output

  • 7/31/2019 Buffered Byte & Char Stream

    12/13

    8/12/12

    Queries

  • 7/31/2019 Buffered Byte & Char Stream

    13/13

    8/12/12


Top Related