Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1."— Presentation transcript:

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

2 In this Lecture Introduction Java I/O Types Of Streams Byte Streams Character Streams 1-2

3 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

4 Overview Java I/O –The java.io package –Streams, Readers and Writers Files –Working with Files 4

5 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

6 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

7 Types Of Streams Byte Streams handle I/O of raw binary data.Byte Streams Character Streams handle I/O of character data, automatically handling translation to and from the local character set.Character Streams Buffered Streams optimize input and output by reducing the number of calls to the native API.Buffered Streams I/O from the Command Line describes the Standard Streams and the Console object.I/O from the Command Line Data Streams handle binary I/O of primitive data type and String values.Data Streams Object Streams handle binary I/O of objects.Object Streams File I/O File Objects help you to write platform-independent code that examines and manipulates files.File Objects Random Access Files handle non-sequential file access.Random Access Files 7

8 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. InputStreamOutputStream 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. FileInputStreamFileOutputStream 8

9 Java I/O - InputStream 9

10 Java I/O – InputStreams 10

11 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

12 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

13 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 -1 while (b != -1) { //do something… b = in.read(); } in.close(); 13

14 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

15 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

16 Java I/O – OutputStream 16

17 Java I/O – OutputStreams 17

18 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

19 Java I/O – Using OutputStreams Output in Java: OutputStream out = new FileOutputStream(“c:\\temp\\myfile.txt”); while (…) { out.write(…); } out.close(); 19

20 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

21 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.CopyBytes 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

22 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

23 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

24 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

25 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

26 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

27 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

28 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

29 Java I/O – Reader 29

30 Java I/O – Readers 30

31 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

32 Java I/O – Writer 32

33 Java I/O – Writers 33

34 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

35 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

36 Cont.. int c; while ((c = in.read()) != -1) { out.write(c); } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } }} 36

37 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

38 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

39 Questions ???!! 39

40 I hope this was entertaining. 40


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

Similar presentations


Ads by Google