Object Oriented Programming: File I/O
Object Oriented Programming: File I/O
File I/O
Lecture 18
By
Irfan Latif Memon
Outline
• File I/O
• Streams
• Simple file I/O
• Create a file
• Add the record to the file
• Read from existing file
File I/O
•1)Useful
public methods
void of OutputStream
write(int)throws is used to write a byte to the current
IOException output stream.
2) public void write(byte[])throws is used to write an array of byte to
IOException the current output stream.
3) public void flush()throws flushes the current output stream.
IOException
4) public void close()throws is used to close the current output
IOException stream.
OutputStream Hierarchy
InputStream class
1) public abstract int read()throws reads the next byte of data from the
IOException input stream. It returns -1 at the end
of file.
2) public int available()throws returns an estimate of the number of
IOException bytes that can be read from the
current input stream.
3) public void close()throws is used to close the current input
IOException stream.
Java FileOutputStream Class
• Java FileOutputStream is an output stream
used for writing data to a file.
• If you have to write primitive values into a file,
• use FileOutputStream class. You can write
byte-oriented as well as character-oriented
data through FileOutputStream class. But, for
character-oriented data, it is preferred to use
FileWriter than FileOutputStream.
Method Description
• public class FileInputStream extends InputStre
am
Java FileInputStream class methods
Method Description