SlideShare a Scribd company logo
File IO in Java

       Prof. AshishSingh Bhatia, ast.bhatia@gmail.com,
     ashish@asbspace.in, Web: asbspace.in, M:9879009551


                                 September 21, 2012




Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   1
AGENDA
Overview of IO Classes
OutputStream of IO
InputStream of IO
Writer of IO
Reader of IO
Methods of OutputStream Class
Methods of Writer Class
Methods of Writer Class Extra
Methods of InputStream Class
Methods of Reader Class
InputStream Usage
OutputStream Usage
Bridge Classes
OutputStreamWriter
InputStream Reader
FileInput and FileOutput Stream
  Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   2
Overview of IO Classes




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   3
OutputStream of IO




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   4
InputStream of IO




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   5
Writer of IO




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   6
Reader of IO




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   7
Methods of OutputStream Class

       public void write(int b)
       Writes a single byte to an output stream. Why we need int as
       argument?




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   8
Methods of OutputStream Class

       public void write(int b)
       Writes a single byte to an output stream. Why we need int as
       argument?
       public void write(byte[] b)
       Writes complete byte array in to an outputstream.




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   9
Methods of OutputStream Class

       public void write(int b)
       Writes a single byte to an output stream. Why we need int as
       argument?
       public void write(byte[] b)
       Writes complete byte array in to an outputstream.
       public void write(byte[] b, int offset, int len)
       Write a subrange from offset to specified length[number of
       bytes].




    Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   10
Methods of OutputStream Class

       public void write(int b)
       Writes a single byte to an output stream. Why we need int as
       argument?
       public void write(byte[] b)
       Writes complete byte array in to an outputstream.
       public void write(byte[] b, int offset, int len)
       Write a subrange from offset to specified length[number of
       bytes].
       public void flush()
       Flushes the output buffer. Ensures the contents are written to
       the destination.




    Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   11
Methods of OutputStream Class

       public void write(int b)
       Writes a single byte to an output stream. Why we need int as
       argument?
       public void write(byte[] b)
       Writes complete byte array in to an outputstream.
       public void write(byte[] b, int offset, int len)
       Write a subrange from offset to specified length[number of
       bytes].
       public void flush()
       Flushes the output buffer. Ensures the contents are written to
       the destination.
       public void close()
       Closes the outputstream. Further write will generate
       IOException
    Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   12
Methods of Writer Class


        public void write(int ch)
        Writes a single character to an output stream. Why we need
        int as argument?




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   13
Methods of Writer Class


        public void write(int ch)
        Writes a single character to an output stream. Why we need
        int as argument?
        public void write(char[] ch)
        Writes complete character array in to an outputstream.




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   14
Methods of Writer Class


        public void write(int ch)
        Writes a single character to an output stream. Why we need
        int as argument?
        public void write(char[] ch)
        Writes complete character array in to an outputstream.
        public void write(char[] b, int offset, int len)
        Write a subrange from offset to specified length[number of
        character].




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   15
Methods of Writer Class


        public void write(int ch)
        Writes a single character to an output stream. Why we need
        int as argument?
        public void write(char[] ch)
        Writes complete character array in to an outputstream.
        public void write(char[] b, int offset, int len)
        Write a subrange from offset to specified length[number of
        character].
        public void flush()
        Flushes the output buffer. Ensures the contents are written to
        the destination.




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   16
Methods of Writer Class


        public void write(int ch)
        Writes a single character to an output stream. Why we need
        int as argument?
        public void write(char[] ch)
        Writes complete character array in to an outputstream.
        public void write(char[] b, int offset, int len)
        Write a subrange from offset to specified length[number of
        character].
        public void flush()
        Flushes the output buffer. Ensures the contents are written to
        the destination.
        public void close()
        Closes the writer. Further write will generate IOException

     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   17
Methods of Writer Class Extra




        String is a sequence of Character.
        public void write(String s)




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   18
Methods of Writer Class Extra




        String is a sequence of Character.
        public void write(String s)
        public void write(String s, int offset, int len)




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   19
Methods of Writer Class Extra




        String is a sequence of Character.
        public void write(String s)
        public void write(String s, int offset, int len)
        public void append(char ch)




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   20
Methods of Writer Class Extra




        String is a sequence of Character.
        public void write(String s)
        public void write(String s, int offset, int len)
        public void append(char ch)
        public void append(CharSequence cs)




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   21
Methods of Writer Class Extra




        String is a sequence of Character.
        public void write(String s)
        public void write(String s, int offset, int len)
        public void append(char ch)
        public void append(CharSequence cs)
        public void append(CharSequence cs, int offset,
        int len)




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   22
Methods of InputStream Class
        public int read()
        Read the integer representation of the next available byte of
        input. Returns -1 when the end of file is encountered.




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   23
Methods of InputStream Class
        public int read()
        Read the integer representation of the next available byte of
        input. Returns -1 when the end of file is encountered.
        public int read(byte[] b)
        Attempt to read up to b.length bytes in the the b array.
        Returns the number of actual bytes read. -1 is returned when
        the end of file is encountered.




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   24
Methods of InputStream Class
        public int read()
        Read the integer representation of the next available byte of
        input. Returns -1 when the end of file is encountered.
        public int read(byte[] b)
        Attempt to read up to b.length bytes in the the b array.
        Returns the number of actual bytes read. -1 is returned when
        the end of file is encountered.
        public int read(byte[] b, int offset, int len)




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   25
Methods of InputStream Class
        public int read()
        Read the integer representation of the next available byte of
        input. Returns -1 when the end of file is encountered.
        public int read(byte[] b)
        Attempt to read up to b.length bytes in the the b array.
        Returns the number of actual bytes read. -1 is returned when
        the end of file is encountered.
        public int read(byte[] b, int offset, int len)
        public long skip(long count)
        count indicates number of bytes to be skipped and return the
        number of byte skipped or -1 in case of end of file.




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   26
Methods of InputStream Class
        public int read()
        Read the integer representation of the next available byte of
        input. Returns -1 when the end of file is encountered.
        public int read(byte[] b)
        Attempt to read up to b.length bytes in the the b array.
        Returns the number of actual bytes read. -1 is returned when
        the end of file is encountered.
        public int read(byte[] b, int offset, int len)
        public long skip(long count)
        count indicates number of bytes to be skipped and return the
        number of byte skipped or -1 in case of end of file.
        public int available()
        Number of bytes available for reading.




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   27
Methods of InputStream Class
        public int read()
        Read the integer representation of the next available byte of
        input. Returns -1 when the end of file is encountered.
        public int read(byte[] b)
        Attempt to read up to b.length bytes in the the b array.
        Returns the number of actual bytes read. -1 is returned when
        the end of file is encountered.
        public int read(byte[] b, int offset, int len)
        public long skip(long count)
        count indicates number of bytes to be skipped and return the
        number of byte skipped or -1 in case of end of file.
        public int available()
        Number of bytes available for reading.
        public void mark(int readlimit)



     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   28
Methods of InputStream Class
        public int read()
        Read the integer representation of the next available byte of
        input. Returns -1 when the end of file is encountered.
        public int read(byte[] b)
        Attempt to read up to b.length bytes in the the b array.
        Returns the number of actual bytes read. -1 is returned when
        the end of file is encountered.
        public int read(byte[] b, int offset, int len)
        public long skip(long count)
        count indicates number of bytes to be skipped and return the
        number of byte skipped or -1 in case of end of file.
        public int available()
        Number of bytes available for reading.
        public void mark(int readlimit)
        public void reset()

     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   29
Methods of InputStream Class
        public int read()
        Read the integer representation of the next available byte of
        input. Returns -1 when the end of file is encountered.
        public int read(byte[] b)
        Attempt to read up to b.length bytes in the the b array.
        Returns the number of actual bytes read. -1 is returned when
        the end of file is encountered.
        public int read(byte[] b, int offset, int len)
        public long skip(long count)
        count indicates number of bytes to be skipped and return the
        number of byte skipped or -1 in case of end of file.
        public int available()
        Number of bytes available for reading.
        public void mark(int readlimit)
        public void reset()
        public boolean markSupported()
     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   30
Methods of InputStream Class
        public int read()
        Read the integer representation of the next available byte of
        input. Returns -1 when the end of file is encountered.
        public int read(byte[] b)
        Attempt to read up to b.length bytes in the the b array.
        Returns the number of actual bytes read. -1 is returned when
        the end of file is encountered.
        public int read(byte[] b, int offset, int len)
        public long skip(long count)
        count indicates number of bytes to be skipped and return the
        number of byte skipped or -1 in case of end of file.
        public int available()
        Number of bytes available for reading.
        public void mark(int readlimit)
        public void reset()
        public boolean markSupported()
        public void close()
     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 31
Methods of Reader Class



        public int read()




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   32
Methods of Reader Class



        public int read()
        public int read(char[] b)




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   33
Methods of Reader Class



        public int read()
        public int read(char[] b)
        public int read(char[] b, int offset, int len)




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   34
Methods of Reader Class



        public int read()
        public int read(char[] b)
        public int read(char[] b, int offset, int len)
        public long skip(long count)




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   35
Methods of Reader Class



        public int read()
        public int read(char[] b)
        public int read(char[] b, int offset, int len)
        public long skip(long count)
        public boolean ready()




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   36
Methods of Reader Class



        public int read()
        public int read(char[] b)
        public int read(char[] b, int offset, int len)
        public long skip(long count)
        public boolean ready()
        public void mark(int readlimit)




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   37
Methods of Reader Class



        public int read()
        public int read(char[] b)
        public int read(char[] b, int offset, int len)
        public long skip(long count)
        public boolean ready()
        public void mark(int readlimit)
        public void reset()




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   38
Methods of Reader Class



        public int read()
        public int read(char[] b)
        public int read(char[] b, int offset, int len)
        public long skip(long count)
        public boolean ready()
        public void mark(int readlimit)
        public void reset()
        public boolean markSupported()




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   39
Methods of Reader Class



        public int read()
        public int read(char[] b)
        public int read(char[] b, int offset, int len)
        public long skip(long count)
        public boolean ready()
        public void mark(int readlimit)
        public void reset()
        public boolean markSupported()
        public void close()




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   40
InputStream Usage




    Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   41
OutputStream Usage




    Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   42
Bridge Classes



        OutputStreamWriter and InputStreamReader
        Character Base Stream Classes which inturns work on bytes.
        Handles unicode character and bytes both.
        Encoding and Decoding of unicode character from and to
        various character set.
        java.nio.charset package has classes which handle
        encoding and decoding.
        class Charset represents different character set.




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   43
OutputStreamWriter




    Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   44
InputStream Reader




    Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   45
FileInput and FileOutput Stream




        Output and input of binary files.




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   46
FileInput and FileOutput Stream




        Output and input of binary files.
        FileOutputStream create and write bytes in to physical file.




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   47
FileInput and FileOutput Stream




        Output and input of binary files.
        FileOutputStream create and write bytes in to physical file.
        If file exists it overwrites it until append flag is used.




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   48
FileInput and FileOutput Stream




        Output and input of binary files.
        FileOutputStream create and write bytes in to physical file.
        If file exists it overwrites it until append flag is used.
        Inherits all the methods from the OutputStream.




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   49
FileInput and FileOutput Stream




        Output and input of binary files.
        FileOutputStream create and write bytes in to physical file.
        If file exists it overwrites it until append flag is used.
        Inherits all the methods from the OutputStream.
        Additional methods are
        public FileDescriptor getFD()
        public FileChannel getChannel()




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   50
FileDescriptor




        Whenever any file is opened for input or output, it uses a file
        descriptor.




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   51
FileDescriptor




        Whenever any file is opened for input or output, it uses a file
        descriptor.
        0=Standard Input




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   52
FileDescriptor




        Whenever any file is opened for input or output, it uses a file
        descriptor.
        0=Standard Input
        1=Standard Output




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   53
FileDescriptor




        Whenever any file is opened for input or output, it uses a file
        descriptor.
        0=Standard Input
        1=Standard Output
        2=Standard Error




     Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551   54

More Related Content

What's hot (19)

PDF
11.the novel lossless text compression technique using ambigram logic and huf...
Alexander Decker
 
PDF
Acm aleppo cpc training eighth session
Ahmad Bashar Eter
 
PDF
Acm aleppo cpc training ninth session
Ahmad Bashar Eter
 
PPTX
Introduction to c programming
SaranyaK68
 
PPTX
Huffman analysis
Abubakar Sultan
 
PPT
Chapter Three(2)
bolovv
 
PDF
Aspects of software naturalness through the generation of IdentifierNames
Oleksandr Zaitsev
 
PDF
Introduction To Programming with Python
Sushant Mane
 
PPTX
Huffman codes
Nargis Ehsan
 
PDF
Acm aleppo cpc training second session
Ahmad Bashar Eter
 
PDF
Acm aleppo cpc training introduction 1
Ahmad Bashar Eter
 
PDF
Designing a Software Application for Fiber Fano Planes
inventionjournals
 
PDF
Functional Programming in Pattern-Match-Oriented Programming Style <Programmi...
Rakuten Group, Inc.
 
PPTX
Huffman coding || Huffman Tree
SatishKumarInumarthi
 
PDF
The Egison Programming Language
Rakuten Group, Inc.
 
PPTX
Huffman Coding
Muhammad Saqib Rehan
 
PPTX
Regex posix
sana mateen
 
PPTX
11 Unit 1 Chapter 03 Data Handling
Praveen M Jigajinni
 
11.the novel lossless text compression technique using ambigram logic and huf...
Alexander Decker
 
Acm aleppo cpc training eighth session
Ahmad Bashar Eter
 
Acm aleppo cpc training ninth session
Ahmad Bashar Eter
 
Introduction to c programming
SaranyaK68
 
Huffman analysis
Abubakar Sultan
 
Chapter Three(2)
bolovv
 
Aspects of software naturalness through the generation of IdentifierNames
Oleksandr Zaitsev
 
Introduction To Programming with Python
Sushant Mane
 
Huffman codes
Nargis Ehsan
 
Acm aleppo cpc training second session
Ahmad Bashar Eter
 
Acm aleppo cpc training introduction 1
Ahmad Bashar Eter
 
Designing a Software Application for Fiber Fano Planes
inventionjournals
 
Functional Programming in Pattern-Match-Oriented Programming Style <Programmi...
Rakuten Group, Inc.
 
Huffman coding || Huffman Tree
SatishKumarInumarthi
 
The Egison Programming Language
Rakuten Group, Inc.
 
Huffman Coding
Muhammad Saqib Rehan
 
Regex posix
sana mateen
 
11 Unit 1 Chapter 03 Data Handling
Praveen M Jigajinni
 

Viewers also liked (20)

PPTX
Reading and writting
andreeamolnar
 
PPT
Common errors
andreeamolnar
 
PPT
WebDynPro for Java Common Error v5
Jodi Johnson
 
PPTX
Java byte code & virtual machine
Laxman Puri
 
PPTX
For loop java
andreeamolnar
 
PDF
Illia shestakov - The Future of Java JDK #9
Anna Shymchenko
 
PPTX
Making Java more dynamic: runtime code generation for the JVM
Rafael Winterhalter
 
PDF
From Java code to Java heap: Understanding and optimizing your application's ...
Chris Bailey
 
PPTX
The Java memory model made easy
Rafael Winterhalter
 
PPT
BPO Services | Inbound Call Center | Telemarketing Outbound Call Center Outso...
Mark wolzer
 
PDF
Java EE Revisits GoF Design Patterns
Murat Yener
 
PPT
Network programming in Java
Tushar B Kute
 
PPT
Most Asked Java Interview Question and Answer
TOPS Technologies
 
PPS
Java Exception handling
kamal kotecha
 
PPT
Computer languages 11
Muhammad Ramzan
 
PDF
Java programming-examples
Mumbai Academisc
 
PDF
Introduction to Java Programming
Ravi Kant Sahu
 
PDF
Top 100 SQL Interview Questions and Answers
iimjobs and hirist
 
PPTX
Java byte code in practice
Rafael Winterhalter
 
PPTX
An introduction to JVM performance
Rafael Winterhalter
 
Reading and writting
andreeamolnar
 
Common errors
andreeamolnar
 
WebDynPro for Java Common Error v5
Jodi Johnson
 
Java byte code & virtual machine
Laxman Puri
 
For loop java
andreeamolnar
 
Illia shestakov - The Future of Java JDK #9
Anna Shymchenko
 
Making Java more dynamic: runtime code generation for the JVM
Rafael Winterhalter
 
From Java code to Java heap: Understanding and optimizing your application's ...
Chris Bailey
 
The Java memory model made easy
Rafael Winterhalter
 
BPO Services | Inbound Call Center | Telemarketing Outbound Call Center Outso...
Mark wolzer
 
Java EE Revisits GoF Design Patterns
Murat Yener
 
Network programming in Java
Tushar B Kute
 
Most Asked Java Interview Question and Answer
TOPS Technologies
 
Java Exception handling
kamal kotecha
 
Computer languages 11
Muhammad Ramzan
 
Java programming-examples
Mumbai Academisc
 
Introduction to Java Programming
Ravi Kant Sahu
 
Top 100 SQL Interview Questions and Answers
iimjobs and hirist
 
Java byte code in practice
Rafael Winterhalter
 
An introduction to JVM performance
Rafael Winterhalter
 
Ad

Similar to I/O In Java Part 2 (20)

PPT
Java stream
Arati Gadgil
 
PPTX
Java I/O
Jayant Dalvi
 
PPT
Using Input Output
raksharao
 
PDF
UNIT4-IO,Generics,String Handling.pdf Notes
SakkaravarthiS1
 
PDF
0301502 ADVANCED JAVA_UNIT_1.pdf
jaydesai523137
 
PPT
Java Streams
M Vishnuvardhan Reddy
 
PDF
Java IO Stream, the introduction to Streams
ranganadh6
 
PPS
Java session14
Niit Care
 
PPTX
IOStream.pptx
HindAlmisbahi
 
PDF
java.io - streams and files
Marcello Thiry
 
PDF
Java IO
UTSAB NEUPANE
 
PPTX
Java Tutorial Lab 6
Berk Soysal
 
PDF
Monhocvecaujahetvagiuplaptunhhayhonha.pdf
cuchuoi83ne
 
PDF
Java Day-6
People Strategists
 
PDF
Java I/O Part 1
AshishSingh Bhatia
 
PDF
I/O in java Part 1
ashishspace
 
PPTX
IO Programming.pptx all informatiyon ppt
nandinimakwana22cse
 
PDF
Java I/O
Jussi Pohjolainen
 
PPTX
Stream In Java.pptx
ssuser9d7049
 
PPTX
Input & output
SAIFUR RAHMAN
 
Java stream
Arati Gadgil
 
Java I/O
Jayant Dalvi
 
Using Input Output
raksharao
 
UNIT4-IO,Generics,String Handling.pdf Notes
SakkaravarthiS1
 
0301502 ADVANCED JAVA_UNIT_1.pdf
jaydesai523137
 
Java Streams
M Vishnuvardhan Reddy
 
Java IO Stream, the introduction to Streams
ranganadh6
 
Java session14
Niit Care
 
IOStream.pptx
HindAlmisbahi
 
java.io - streams and files
Marcello Thiry
 
Java IO
UTSAB NEUPANE
 
Java Tutorial Lab 6
Berk Soysal
 
Monhocvecaujahetvagiuplaptunhhayhonha.pdf
cuchuoi83ne
 
Java Day-6
People Strategists
 
Java I/O Part 1
AshishSingh Bhatia
 
I/O in java Part 1
ashishspace
 
IO Programming.pptx all informatiyon ppt
nandinimakwana22cse
 
Stream In Java.pptx
ssuser9d7049
 
Input & output
SAIFUR RAHMAN
 
Ad

More from ashishspace (6)

PDF
Remote Method Invocation
ashishspace
 
PDF
Understanding Date Handling and Collection Framework
ashishspace
 
PDF
Nested and Enum in Java
ashishspace
 
PDF
HTTP Basic and Servlet Basic
ashishspace
 
PDF
Java packages and access specifiers
ashishspace
 
PDF
Data types and operators and statements
ashishspace
 
Remote Method Invocation
ashishspace
 
Understanding Date Handling and Collection Framework
ashishspace
 
Nested and Enum in Java
ashishspace
 
HTTP Basic and Servlet Basic
ashishspace
 
Java packages and access specifiers
ashishspace
 
Data types and operators and statements
ashishspace
 

Recently uploaded (20)

PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 

I/O In Java Part 2

  • 1. File IO in Java Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 September 21, 2012 Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 1
  • 2. AGENDA Overview of IO Classes OutputStream of IO InputStream of IO Writer of IO Reader of IO Methods of OutputStream Class Methods of Writer Class Methods of Writer Class Extra Methods of InputStream Class Methods of Reader Class InputStream Usage OutputStream Usage Bridge Classes OutputStreamWriter InputStream Reader FileInput and FileOutput Stream Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 2
  • 3. Overview of IO Classes Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 3
  • 4. OutputStream of IO Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 4
  • 5. InputStream of IO Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 5
  • 6. Writer of IO Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 6
  • 7. Reader of IO Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 7
  • 8. Methods of OutputStream Class public void write(int b) Writes a single byte to an output stream. Why we need int as argument? Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 8
  • 9. Methods of OutputStream Class public void write(int b) Writes a single byte to an output stream. Why we need int as argument? public void write(byte[] b) Writes complete byte array in to an outputstream. Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 9
  • 10. Methods of OutputStream Class public void write(int b) Writes a single byte to an output stream. Why we need int as argument? public void write(byte[] b) Writes complete byte array in to an outputstream. public void write(byte[] b, int offset, int len) Write a subrange from offset to specified length[number of bytes]. Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 10
  • 11. Methods of OutputStream Class public void write(int b) Writes a single byte to an output stream. Why we need int as argument? public void write(byte[] b) Writes complete byte array in to an outputstream. public void write(byte[] b, int offset, int len) Write a subrange from offset to specified length[number of bytes]. public void flush() Flushes the output buffer. Ensures the contents are written to the destination. Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 11
  • 12. Methods of OutputStream Class public void write(int b) Writes a single byte to an output stream. Why we need int as argument? public void write(byte[] b) Writes complete byte array in to an outputstream. public void write(byte[] b, int offset, int len) Write a subrange from offset to specified length[number of bytes]. public void flush() Flushes the output buffer. Ensures the contents are written to the destination. public void close() Closes the outputstream. Further write will generate IOException Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 12
  • 13. Methods of Writer Class public void write(int ch) Writes a single character to an output stream. Why we need int as argument? Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 13
  • 14. Methods of Writer Class public void write(int ch) Writes a single character to an output stream. Why we need int as argument? public void write(char[] ch) Writes complete character array in to an outputstream. Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 14
  • 15. Methods of Writer Class public void write(int ch) Writes a single character to an output stream. Why we need int as argument? public void write(char[] ch) Writes complete character array in to an outputstream. public void write(char[] b, int offset, int len) Write a subrange from offset to specified length[number of character]. Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 15
  • 16. Methods of Writer Class public void write(int ch) Writes a single character to an output stream. Why we need int as argument? public void write(char[] ch) Writes complete character array in to an outputstream. public void write(char[] b, int offset, int len) Write a subrange from offset to specified length[number of character]. public void flush() Flushes the output buffer. Ensures the contents are written to the destination. Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 16
  • 17. Methods of Writer Class public void write(int ch) Writes a single character to an output stream. Why we need int as argument? public void write(char[] ch) Writes complete character array in to an outputstream. public void write(char[] b, int offset, int len) Write a subrange from offset to specified length[number of character]. public void flush() Flushes the output buffer. Ensures the contents are written to the destination. public void close() Closes the writer. Further write will generate IOException Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 17
  • 18. Methods of Writer Class Extra String is a sequence of Character. public void write(String s) Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 18
  • 19. Methods of Writer Class Extra String is a sequence of Character. public void write(String s) public void write(String s, int offset, int len) Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 19
  • 20. Methods of Writer Class Extra String is a sequence of Character. public void write(String s) public void write(String s, int offset, int len) public void append(char ch) Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 20
  • 21. Methods of Writer Class Extra String is a sequence of Character. public void write(String s) public void write(String s, int offset, int len) public void append(char ch) public void append(CharSequence cs) Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 21
  • 22. Methods of Writer Class Extra String is a sequence of Character. public void write(String s) public void write(String s, int offset, int len) public void append(char ch) public void append(CharSequence cs) public void append(CharSequence cs, int offset, int len) Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 22
  • 23. Methods of InputStream Class public int read() Read the integer representation of the next available byte of input. Returns -1 when the end of file is encountered. Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 23
  • 24. Methods of InputStream Class public int read() Read the integer representation of the next available byte of input. Returns -1 when the end of file is encountered. public int read(byte[] b) Attempt to read up to b.length bytes in the the b array. Returns the number of actual bytes read. -1 is returned when the end of file is encountered. Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 24
  • 25. Methods of InputStream Class public int read() Read the integer representation of the next available byte of input. Returns -1 when the end of file is encountered. public int read(byte[] b) Attempt to read up to b.length bytes in the the b array. Returns the number of actual bytes read. -1 is returned when the end of file is encountered. public int read(byte[] b, int offset, int len) Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 25
  • 26. Methods of InputStream Class public int read() Read the integer representation of the next available byte of input. Returns -1 when the end of file is encountered. public int read(byte[] b) Attempt to read up to b.length bytes in the the b array. Returns the number of actual bytes read. -1 is returned when the end of file is encountered. public int read(byte[] b, int offset, int len) public long skip(long count) count indicates number of bytes to be skipped and return the number of byte skipped or -1 in case of end of file. Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 26
  • 27. Methods of InputStream Class public int read() Read the integer representation of the next available byte of input. Returns -1 when the end of file is encountered. public int read(byte[] b) Attempt to read up to b.length bytes in the the b array. Returns the number of actual bytes read. -1 is returned when the end of file is encountered. public int read(byte[] b, int offset, int len) public long skip(long count) count indicates number of bytes to be skipped and return the number of byte skipped or -1 in case of end of file. public int available() Number of bytes available for reading. Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 27
  • 28. Methods of InputStream Class public int read() Read the integer representation of the next available byte of input. Returns -1 when the end of file is encountered. public int read(byte[] b) Attempt to read up to b.length bytes in the the b array. Returns the number of actual bytes read. -1 is returned when the end of file is encountered. public int read(byte[] b, int offset, int len) public long skip(long count) count indicates number of bytes to be skipped and return the number of byte skipped or -1 in case of end of file. public int available() Number of bytes available for reading. public void mark(int readlimit) Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 28
  • 29. Methods of InputStream Class public int read() Read the integer representation of the next available byte of input. Returns -1 when the end of file is encountered. public int read(byte[] b) Attempt to read up to b.length bytes in the the b array. Returns the number of actual bytes read. -1 is returned when the end of file is encountered. public int read(byte[] b, int offset, int len) public long skip(long count) count indicates number of bytes to be skipped and return the number of byte skipped or -1 in case of end of file. public int available() Number of bytes available for reading. public void mark(int readlimit) public void reset() Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 29
  • 30. Methods of InputStream Class public int read() Read the integer representation of the next available byte of input. Returns -1 when the end of file is encountered. public int read(byte[] b) Attempt to read up to b.length bytes in the the b array. Returns the number of actual bytes read. -1 is returned when the end of file is encountered. public int read(byte[] b, int offset, int len) public long skip(long count) count indicates number of bytes to be skipped and return the number of byte skipped or -1 in case of end of file. public int available() Number of bytes available for reading. public void mark(int readlimit) public void reset() public boolean markSupported() Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 30
  • 31. Methods of InputStream Class public int read() Read the integer representation of the next available byte of input. Returns -1 when the end of file is encountered. public int read(byte[] b) Attempt to read up to b.length bytes in the the b array. Returns the number of actual bytes read. -1 is returned when the end of file is encountered. public int read(byte[] b, int offset, int len) public long skip(long count) count indicates number of bytes to be skipped and return the number of byte skipped or -1 in case of end of file. public int available() Number of bytes available for reading. public void mark(int readlimit) public void reset() public boolean markSupported() public void close() Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 31
  • 32. Methods of Reader Class public int read() Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 32
  • 33. Methods of Reader Class public int read() public int read(char[] b) Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 33
  • 34. Methods of Reader Class public int read() public int read(char[] b) public int read(char[] b, int offset, int len) Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 34
  • 35. Methods of Reader Class public int read() public int read(char[] b) public int read(char[] b, int offset, int len) public long skip(long count) Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 35
  • 36. Methods of Reader Class public int read() public int read(char[] b) public int read(char[] b, int offset, int len) public long skip(long count) public boolean ready() Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 36
  • 37. Methods of Reader Class public int read() public int read(char[] b) public int read(char[] b, int offset, int len) public long skip(long count) public boolean ready() public void mark(int readlimit) Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 37
  • 38. Methods of Reader Class public int read() public int read(char[] b) public int read(char[] b, int offset, int len) public long skip(long count) public boolean ready() public void mark(int readlimit) public void reset() Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 38
  • 39. Methods of Reader Class public int read() public int read(char[] b) public int read(char[] b, int offset, int len) public long skip(long count) public boolean ready() public void mark(int readlimit) public void reset() public boolean markSupported() Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 39
  • 40. Methods of Reader Class public int read() public int read(char[] b) public int read(char[] b, int offset, int len) public long skip(long count) public boolean ready() public void mark(int readlimit) public void reset() public boolean markSupported() public void close() Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 40
  • 41. InputStream Usage Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 41
  • 42. OutputStream Usage Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 42
  • 43. Bridge Classes OutputStreamWriter and InputStreamReader Character Base Stream Classes which inturns work on bytes. Handles unicode character and bytes both. Encoding and Decoding of unicode character from and to various character set. java.nio.charset package has classes which handle encoding and decoding. class Charset represents different character set. Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 43
  • 44. OutputStreamWriter Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 44
  • 45. InputStream Reader Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 45
  • 46. FileInput and FileOutput Stream Output and input of binary files. Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 46
  • 47. FileInput and FileOutput Stream Output and input of binary files. FileOutputStream create and write bytes in to physical file. Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 47
  • 48. FileInput and FileOutput Stream Output and input of binary files. FileOutputStream create and write bytes in to physical file. If file exists it overwrites it until append flag is used. Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 48
  • 49. FileInput and FileOutput Stream Output and input of binary files. FileOutputStream create and write bytes in to physical file. If file exists it overwrites it until append flag is used. Inherits all the methods from the OutputStream. Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 49
  • 50. FileInput and FileOutput Stream Output and input of binary files. FileOutputStream create and write bytes in to physical file. If file exists it overwrites it until append flag is used. Inherits all the methods from the OutputStream. Additional methods are public FileDescriptor getFD() public FileChannel getChannel() Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 50
  • 51. FileDescriptor Whenever any file is opened for input or output, it uses a file descriptor. Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 51
  • 52. FileDescriptor Whenever any file is opened for input or output, it uses a file descriptor. 0=Standard Input Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 52
  • 53. FileDescriptor Whenever any file is opened for input or output, it uses a file descriptor. 0=Standard Input 1=Standard Output Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 53
  • 54. FileDescriptor Whenever any file is opened for input or output, it uses a file descriptor. 0=Standard Input 1=Standard Output 2=Standard Error Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 54