1 / 35

The java.io package

The java.io package. Session 11. Objectives. Discuss applets and I/O Discuss the standard input/output streams Explain the classes InputStream and OutputStream Describe the Byte array I/O Discuss Filtered and Buffered I/O operations Explore the class RandomAccessFile

dgriswold
Download Presentation

The java.io package

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The java.io package Session 11

  2. Objectives • Discuss applets and I/O • Discuss the standard input/output streams • Explain the classes InputStream and OutputStream • Describe the Byte array I/O • Discuss Filtered and Buffered I/O operations • Explore the class RandomAccessFile • Describe reader and writer classes • Discuss the concept of streams The java.io package / 2 of 35

  3. Introduction • The java.io package contains classes and interfaces that are relevant to input/output operations • Using these classes and interfaces we can store data that may be either in the form of primitives or objects • This session discusses the java.io package in detail The java.io package / 3 of 35

  4. Applets and File I/O • As they are downloaded on the client’s system, they can be the cause of potential attacks • Hence applets are not allowed to work with file related operations • We shall be discussing java.io with respect to applications only • Java is commonly used to create applet-based programs intended for the Internet The java.io package / 4 of 35

  5. Streams (1) • Input stream receives data from a source • Output stream sends data to a destination from the program • Standard input/output stream in Java is represented by three members of the System class : in, out and err • A stream is a continuous group of data or a channel through which data travels from one point to another The java.io package / 5 of 35

  6. Some Job Some Job IOException Some Job Stream read / write Streams(2) • When a stream is read or written, the other threads are blocked • While reading or writing a stream, if an error occurs, an IOException is thrown Threads Threads The java.io package / 6 of 35

  7. Example Output The java.io package / 7 of 35

  8. The java.io package • Two main categories of streams in Java : • Byte Streams – • these provide a way to handle byte oriented input/output operations • InputStream and OutputStream classes are at the top of their hierarchy • Character Streams – • these provide a way to handle character oriented input/output operations • they make use of Unicode and can be internationalized The java.io package / 8 of 35

  9. Hierarchy of classes and interfaces Object File FileDescriptor DataInput RandomAccessFile DataOutput InputStream OutputStream ByteArray InputStream FileInput Stream Filter OutputStream ByteArray OutputStream Filter InputStream FileOutput Stream DataInput Stream Buffered InputStream LineNumber InputStream PushBack InputStream DataOutput Stream Buffered OutputStream Print Stream The java.io package / 9 of 35

  10. DataInput interface • Allows us to convert data that is in Java modified Unicode Transmission Format (UTF-8) to string form • DataInput interface defines a number of methods including methods for reading Java primitive data types • Used to read bytes from a binary stream and reconstruct data in any of the java primitive types The java.io package / 10 of 35

  11. DataOutput interface • Allows us to convert a String into Java modified UTF-8 format and writing it into a stream • All methods under DataOutput interface throw an IOException in case of an error • Used to reconstruct data that is in any of the Java primitive types into a series of bytes and writes them onto a binary system The java.io package / 11 of 35

  12. InputStream class (1) • The basic purpose of this class is to read data from an input stream • An abstract class that defines how data is received The java.io package / 12 of 35

  13. InputStream class (2) Some of the methods for the InputStream class are: The java.io package / 13 of 35

  14. InputStream class (3) The java.io package / 14 of 35

  15. OutputStream class (1) • This class is used to write data to a stream • An abstract class that defines the way in which outputs are written to streams The java.io package / 15 of 35

  16. OutputStream class (2) Some of the methods for the OutputStream class are: The java.io package / 16 of 35

  17. FileInputStream • Commonly used constructors of this class : • FileInputStream(String filename) throws FileNotFoundException Creates an InputStream that we can use to read bytes from a file. filename is full pathname of a file • FileInputStream(File name) throws FileNotFoundException Creates an input stream that we can use to read bytes from a file. name is a File object • Used to read input from a file in the form of a stream The java.io package / 17 of 35

  18. Example Output The java.io package / 18 of 35

  19. FileOutputStream • Its constructors are : • FileOutputStream(String filename) throws FileNotFoundException : Creates an OutputStream that we can use to write bytes to a file • FileOutputStream(File name) throws FileNotFoundException : Creates an OutputStream that we can use to write bytes to a file • FileOutputStream(String filename, boolean flag) throws FileNotFoundException : Creates an OutputStream that we can use to write bytes to a file. If flag is true, file is opened in append mode • This class is used to write output to a file stream The java.io package / 19 of 35

  20. Example Output The java.io package / 20 of 35

  21. ByteArrayInputStream • Its constructors are : • ByteArrayInputStream(byte b[]) – creates a ByteArrayInputStream with b as the input source • ByteArrayInputStream(byte b[]), int start, int num) – creates a ByteArrayInputStream that begins with the character at start position and is num bytes long • Used to create an input stream using an array of bytes The java.io package / 21 of 35

  22. ByteArrayOutputStream • This class define two constructors • One takes an int argument • Second one does not take any argument • Additional methods like toByteArray() and toString() convert the stream to a byte array or String object respectively • Used to create an output stream using a byte array as the destination The java.io package / 22 of 35

  23. File • All common file and directory operations are performed using the access methods provided by the File class • Methods of this class allow the creating, deleting and renaming of files and directories • File class directly works with files on the file system. The java.io package / 23 of 35

  24. Example Output The java.io package / 24 of 35

  25. Filter Input and Output classes • FilterInputStream – parent of all filtered input stream classes • protected FilterInputStream(InputStream in) • FilterOutputStream – parent of all filtered output stream classes • public FilterOutputStream(OutputStream out) • These classes delegate filtering operations to their sub-classes such as BufferedInputStream or DataOutputStream The java.io package / 25 of 35

  26. Buffered I/O classes • By storing data in a buffer, we save time as we immediately get it from the buffer instead of going back to the original source of data • Java uses buffered input and output to temporarily cache data, read from or written to a stream • A temporary storage area for data The java.io package / 26 of 35

  27. BufferedInputStream • BufferedInputStream(InputStream is) – creates a buffered input stream for the specified InputStream instance • BufferedInputStream(InputStream is, int size) – creates a buffered input stream of a given size for the specified InputStream instance • This class defines two constructors – The java.io package / 27 of 35

  28. BufferedOutputStream • BufferedOutputStream(OutputStream os) – creates a buffered output stream for the specified OutputStream instance • BufferedOutputStream(OutputStream os, int size) – creates a buffered output stream of a given size for the specified OutputStream instance • This class defines two constructors – The java.io package / 28 of 35

  29. RandomAccessFile • Instead implements the DataInput and DataOutput interfaces • It supports reading/writing of all primitive types • This class does not extend either InputStream or OutputStream The java.io package / 29 of 35

  30. Example Output The java.io package / 30 of 35

  31. Character streams • Supports Unicode and can be internationalized • Reader and Writer are abstract classes at the top of the class hierarchy • They provide a way to handle character oriented input/output operations The java.io package / 31 of 35

  32. Reader class • Used for reading character streams • Some of the methods used are : The java.io package / 32 of 35

  33. Writer class • Some of the methods used are : • An abstract class that supports writing into streams The java.io package / 33 of 35

  34. PrintWriter class • Supports Unicode characters • Printed output is flushed and tested for any errors using checkError() method • Supports printing primitive data types, character arrays, strings and objects • Character based class useful for console output The java.io package / 34 of 35

  35. Character Array Input / Output CharArrayReader CharArrayWriter • Supports input and output from memory buffers • Supports 8-bit character input and output • CharArrayWriter adds the methods to the ones provided by class Writer; some of these are: The java.io package / 35 of 35

More Related