40
Prepared by : Prepared by : A.Alzubair A.Alzubair Hassan Hassan Kassala Kassala university university Dept. Computer Dept. Computer Science Lecture 2 I/O Streams I/O Streams 1

Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Embed Size (px)

Citation preview

Page 1: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Prepared by :Prepared by :

A.Alzubair HassanA.Alzubair HassanKassala university Kassala university Dept. Computer Dept. Computer Science Science

Lecture 2

I/O StreamsI/O Streams

1

Page 2: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

In this Lecture

• Introduction• Java I/O• Types Of Streams • Byte Streams • Character Streams

1-2

Page 3: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Introduction

• So far we have used variables and arrays for sorting data inside the programs.

• This approach poses the following problems

1. The data is lost either when a variable goes out of scope or when the program is terminated.

2. It is difficult to handle large volumes of data using variables and arrays.

• We can overcome these problems by storing data on secondary storage devices.

• The data is stored in these devices using the concept of files.

3

Page 4: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Overview

• Java I/O– The java.io package– Streams, Readers and Writers

• Files– Working with Files

4

Page 5: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Java I/O – The Basics

• Java I/O is based around the concept of a stream– Ordered sequence of information (bytes) coming from a

source, or going to Desteation

– Simplest stream reads/writes only a single byte, or an array of bytes at a time

• Designed to be platform-independent • The stream concept is very generic

– Can be applied to many different types of I/O

– Files, Network, Memory, Processes, etc

5

Page 6: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Java I/O – The Basics

• The java.io package contains all of the I/O classes.– Many classes specialised for particular kinds of stream

operations, e.g. file I/O

• Reading/writing single bytes is quite limited– So, it includes classes which provide extra functionality– e.g. buffering, reading numbers and Strings (not bytes),

etc.

• Results in large inheritance hierarchy, with separate trees for input and output stream classes

6

Page 7: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Types Of Streams • Byte Streams handle I/O of raw binary data.

• Character Streams handle I/O of character data, automatically handling translation to and from the local character set.

• Buffered Streams optimize input and output by reducing the number of calls to the native API.

• I/O from the Command Line describes the Standard Streams and the Console object.

• Data Streams handle binary I/O of primitive data type and String values.

• Object Streams handle binary I/O of objects.

File I/O

• File Objects help you to write platform-independent code that examines and manipulates files.

• Random Access Files handle non-sequential file access.

7

Page 8: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Byte Streams

• Programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from InputStream and OutputStream.

• There are many byte stream classes. To demonstrate how byte streams work, we'll focus on the file I/O byte streams, FileInputStream and FileOutputStream. Other kinds of byte streams are used in much the same way; they differ mainly in the way they are constructed.

8

Page 9: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Java I/O - InputStream

9

Page 10: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Java I/O – InputStreams

10

Page 11: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Java I/O – Using InputStreams

• Basic pattern for I/O programming is as follows:

Open a stream

While there’s data to read

Process the data

Close the stream

11

Page 12: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Java I/O – Using InputStreams

• I/O in Java:InputStream in = new FileInputStream(“c:\\temp\\

myfile.txt”);

int b = in.read();

//EOF is signalled by read() returning -1

while (b != -1)

{

//do something…

b = in.read();

}

in.close();

12

Page 13: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Java I/O – Using InputStreams

• But using buffering is more efficient, therefore we always nest our streams…

InputStream inner = new FileInputStream(“c:\\temp\\myfile.txt”);InputStream in = new BufferedInputStream(inner);int b = in.read();//EOF is signalled by read() returning -1while (b != -1){ //do something… b = in.read();}in.close();

13

Page 14: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Java I/O – Using InputStreams

• We’ve omitted exception handling in the previous examples

• Almost all methods on the I/O classes (including constructors) can throw an IOException or a subclass.

• Always wrap I/O code in try…catch blocks to handle errors.

14

Page 15: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Java I/O – Using InputStreams

InputStream in = null;try{ InputStream inner = new FileInputStream(“c:\\temp\\

myfile.txt”); in = new BufferedInputStream(inner); //process file} catch (IOException e){ e.printStackTrace();}finally{ try { in.close(); } catch (Exception e) {}}

15

Page 16: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Java I/O – OutputStream

16

Page 17: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Java I/O – OutputStreams

17

Page 18: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Java I/O – Using InputStreams

• Basic pattern for output is as follows:

Open a stream

While there’s data to write

Write the data

Close the stream

18

Page 19: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Java I/O – Using OutputStreams

• Output in Java:OutputStream out = new FileOutputStream(“c:\\temp\\

myfile.txt”);

while (…)

{

out.write(…);

}

out.close();

19

Page 20: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Java I/O – Using OutputStreams

OutputStream out = null;try{ OutputStream inner = new FileOutputStream(“c:\\temp\\

myfile.txt”); out = new BufferedOutputStream(inner); //write data to the file} catch (IOException e){ e.printStackTrace();}finally{ try { out.close(); } catch (Exception e) {}}

20

Page 21: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Using Byte Streams

• We'll explore FileInputStream and FileOutputStream by examining an example program named CopyBytes, which uses byte streams to copy xanadu.txt, one byte at a time.

•  import java.io.FileInputStream;

• import java.io.FileOutputStream;

• import java.io.IOException;

• public class CopyBytes {

• public static void main(String[] args) throws IOException { FileInputStream in = null;

• FileOutputStream out = null;

21

Page 22: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Cont ..• try

• {

• in = new FileInputStream("xanadu.txt");

• out = new FileOutputStream("outagain.txt");

• int c; 

• while ((c = in.read()) != -1)

• { out.write(c);

• }

• }

• Finally

• {

• if (in != null) {

• in.close();}

• if (out != null) {

• out.close();

• } } } }

22

Page 23: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

CopyBytes spends most of its time in a simple loop

that reads the input stream and writes the output stream, one byte at a time, as shown in the following figure.

23

Page 24: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Note

• Notice that read() returns an int value. If the input is a stream of bytes, why doesn't read() return a byte value? Using a int as a return type allows read() to use -1 to indicate that it has reached the end of the stream.

24

Page 25: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

But That’s Not All!

• Input/OutputStream and sub-classes were part of Java 1.1. • Java 1.2 adds more classes specialised for character based

I/O– The stream classes are for data I/O.

• Classes for character I/O are called Readers and Writers• Why have specialised classes?

– To support foreign languages

25

Page 26: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Unicode

• Each character in the ASCII character set fits into a single byte– …but that’s not enough for chinese, and other complex

alphabets– Need more than a single byte– A Java character (char) is 2 bytes

• Java handles text using Unicode– International standard character set, containing

characters for almost all known languages

• Inside the JVM all text is held as Unicode

26

Page 27: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Java Text I/O

• Because byte != character for all languages, you have to turn bytes into chars using a Input/OutputStream

• Java provides Readers and Writers to save you this work.

• These classes deal with streams of characters– Read/write single character or array of characters

– Again there are classes specialised for particular purposes

27

Page 28: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Character Streams

• The Java platform stores character values using Unicode conventions. Character stream I/O automatically translates this internal format to and from the local character set. In Western locales, the local character set is usually an 8-bit superset of ASCII.

• For most applications, I/O with character streams is no more complicated than I/O with byte streams. Input and output done with stream classes automatically translates to and from the local character set. A program that uses character streams in place of byte streams automatically adapts to the local character set and is ready for internationalization — all without extra effort by the programmer.

28

Page 29: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Java I/O – Reader

29

Page 30: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Java I/O – Readers

30

Page 31: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Using Readers

Reader in = null;try{ Reader inner = new FileReader(“c:\\temp\\myfile.txt”); in = new BufferedReader(inner); //process file} catch (IOException e){ e.printStackTrace();}finally{ try { in.close(); } catch (Exception e) {}}

31

Page 32: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Java I/O – Writer

32

Page 33: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Java I/O – Writers

33

Page 34: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Using Writers

Writer out = null;try{ Writer inner = new FileWriter(“c:\\temp\\myfile.txt”); out = new BufferedWriter(inner); //write data to the file} catch (IOException e){ e.printStackTrace();}finally{ try { out.close(); } catch (Exception e) {}}

34

Page 35: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Using Character Streams

• import java.io.FileReader;

• import java.io.FileWriter;

• import java.io.IOException;

• public class CopyCharacters {

• public static void main(String[] args) throws IOException { FileReader in = null;

• FileWriter out = null; 

• try

• {

• in = new FileReader("xanadu.txt");

• out = new FileWriter("characteroutput.txt"); 

35

Page 36: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Cont ..

• int c;

• while ((c = in.read()) != -1)

• {

• out.write(c);

• }

• }

• finally

• {

• if (in != null)

• { in.close();

• }

• if (out != null)

• { out.close();

• } } }} 36

Page 37: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

CopyBytes VS CopyCharacters

• CopyCharacters is very similar to CopyBytes. The most important difference is that CopyCharacters uses FileReader and FileWriter for input and output in place of FileInputStream and FileOutputStream. Notice that both CopyBytes and CopyCharacters use an int variable to read to and write from. However, in CopyCharacters, the int variable holds a character value in its last 16 bits; in CopyBytes, the int variable holds a byte value in its last 8 bits.

37

Page 38: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Bridging the Gap

• Sometimes you need to bridge across the two hierachies– Use InputStreamReader or OutputStreamWriter

• InputStreamReader– Reads bytes from an InputStream, and turns them into characters

using a character encoding

• OutputStreamWriter– Turns characters sent to the Writer into bytes written by the

OutputStream, again using a character encoding.

38

Page 39: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

Questions ???!!

39

Page 40: Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1

I hope this was entertaining .

40