Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views18 pages

9.2 FileIO Part2

The document covers File Input/Output in Object Oriented Development with Java, focusing on Binary I/O and its associated classes such as FileInputStream, FileOutputStream, DataInputStream, and DataOutputStream. It explains the differences between Text I/O and Binary I/O, emphasizing that Binary I/O does not require encoding/decoding and directly writes bytes to files. Additionally, it discusses the use of BufferedInputStream and BufferedOutputStream to enhance performance by reducing the number of read/write operations.

Uploaded by

Mark
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views18 pages

9.2 FileIO Part2

The document covers File Input/Output in Object Oriented Development with Java, focusing on Binary I/O and its associated classes such as FileInputStream, FileOutputStream, DataInputStream, and DataOutputStream. It explains the differences between Text I/O and Binary I/O, emphasizing that Binary I/O does not require encoding/decoding and directly writes bytes to files. Additionally, it discusses the use of BufferedInputStream and BufferedOutputStream to enhance performance by reducing the number of read/write operations.

Uploaded by

Mark
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Object Oriented Development

with Java
(CT038-3-2)

File Input/Output
Object Oriented Modeling

Prepared by: Lee Kim Keong First Prepared on: June 13 Last Modified on: April 19
Quality checked by: null
Copyright 2019 Asia Pacific University of Innovation and Technology
Topic & Structure of the lesson
• Binary I/O
• FileInputStream & FileOutputStream
• DataInputStream & DataOutputStream
• BufferedInputStream & BufferedOutputStream

CT038-3-2 Object Oriented Development with Java File Input and Output 2
Learning outcomes
• At the end of this lecture you should be
able to:
– Understand IO processing
– Explain Binary I/O

CT038-3-2 Object Oriented Development with Java File Input and Output 3
Binary I/O

• Text I/O requires encoding and decoding


• JVM does this conversion between
Unicode to file-specific and vise versa
• Binary I/O does not require conversions
• Writing bytes to the file, the original bytes
is copied into the file

CT038-3-2 Object Oriented Development with Java File Input and Output 4
Binary I/O

Text I/O

Encoding/decoding
Unicode of Encoded character stored
character

Binary I/O

Byte is Same byte stored


read/written

CT038-3-2 Object Oriented Development with Java File Input and Output 5
Binary I/O
FileInputStream

DataInputStream
FilterInputStream

BufferedInputStream
InputStream
ObjectInputStream

Object
FileOutputStream
BufferedOutputStream
OutputStream
FilterOutputStream
DataOutputStream

ObjectOutputStream
CT038-3-2 Object Oriented Development with Java File Input and Output
FileInputStream and
FileOutputStream
• For reading/writing bytes from/to files.
• Constructors:
public FileInputStream(String filename)
public FileInputStream(File file)

public FileOutputStream(String filename)


public FileOutputStream(File file)
public FileOutputStream(String filename,
boolean append)
public FileOutputStream(File file,
boolean append)
CT038-3-2 Object Oriented Development with Java File Input and Output 7
FileInputStream and
FileOutputStream
public static void main(String []args) throws
IOException {
//create an output stream to the file
FileOutputStream output = new
FileOutputStream("temp.dat");

//output values to the file


for(int i = 1; i <= 10; i++){
output.write(i);
}//for
output.close();
contd. to next slide
CT038-3-2 Object Oriented Development with Java File Input and Output 8
FileInputStream and
FileOutputStream
//create an input stream to the file
FileInputStream input = new
FileInputStream("temp.dat");

//read values from the file


int value;
while((value = input.read()) != -1){
System.out.print(value + " ");
}//while
input.close();
}

CT038-3-2 Object Oriented Development with Java File Input and Output
DataInputStream and
DataOutputStream
• DataInputStream reads bytes from the stream
and converts them into appropriate primitive
type values or strings
• DataOutputStream converts primitive type
values or strings into bytes and output the bytes
to the stream
• Constructors:
public DataInputStream(InputStream
instream)
public DataOutputStream(OutputStream
outstream)
CT038-3-2 Object Oriented Development with Java File Input and Output
DataInputStream and
DataOutputStream
public static void main(String [] args) throws
IOException {
//create an output stream for the file
temp.dat
DataOutputStream output = new
DataOutputStream(new
FileOutputStream("temp.dat"));

contd. to next slide


CT038-3-2 Object Oriented Development with Java File Input and Output 11
DataInputStream and
DataOutputStream

//write student test scores


output.writeUTF("John");
output.writeDouble(86.5);
output.writeUTF("Jim");
output.writeDouble(95.5);
output.writeUTF("George");
output.writeDouble(100.0);
//close output stream
output.close();

contd. to next slide


CT038-3-2 Object Oriented Development with Java File Input and Output 12
DataInputStream and
DataOutputStream
//create an input stream for file temp.dat
DataInputStream input = new
DataInputStream(new
FileInputStream("temp.dat"));
//read student test scores
System.out.println(input.readUTF() + " " +
input.readDouble());
System.out.println(input.readUTF() + " " +
input.readDouble());
System.out.println(input.readUTF() + " " +
input.readDouble());
input.close(); }

CT038-3-2 Object Oriented Development with Java File Input and Output 13
BufferedInputStream and
BufferedOutputStream
• Can be used to speed up input and output
by reducing the number of reads and
writes, just like
BufferedReader/BufferedWriter
• BufferedReader/BufferedWriter is for
reading/writing characters
• BufferedInputStream/
BufferedOutputStream is for
reading/writing bytes
CT038-3-2 Object Oriented Development with Java File Input and Output
BufferedInputStream and
BufferedOutputStream
• Constructors:
//create a BufferedInputStream
public BufferedInputStream(InputStream in)
public BufferedInputStream(InputStream in,
int bufferSize)

//create a BufferedOutputStream
public BufferedOutputStream(OutputStream in)
public BufferedOutputStream(OutputStream in,
int bufferSize)

CT038-3-2 Object Oriented Development with Java File Input and Output
BufferedInputStream and
BufferedOutputStream
• You can improve the previous program by:
DataOutputStream output = new
DataOutputStream(new
BufferedOutputStream(new
FileOutputStream("temp.dat")));

DataInputStream input = new


DataInputStream(new
BufferedInputStream(new
FileInputStream("temp.dat")));
CT038-3-2 Object Oriented Development with Java File Input and Output
Review

• What is Text I/O?


• What is Binary I/O?
• List all Text I/O and Binary I/O classes

CT038-3-2 Object Oriented Development with Java File Input and Output 17
Q&A

CT038-3-2 Object Oriented Development with Java File Input and Output 18

You might also like