Chapter 3 IO Stream_Linh.pptx - JAVA Nu00c2NG CAO(Advanced...

This preview shows page 1 - 12 out of 48 pages.

JAVA NÂNG CAO (Advanced Java) IO Stream
CONTENTS 1.IO Streams 2.File Streams 3.Filter Streams 4.Buffered Streams 5.Reader class và Writer class 6.CharArrayReader class and CharArrayWriter class 7.StringReader class and StringWriter class
Java I/O is based on the notation of streams. Streams are sequences of data. Advantages of streams: No need to learn inner working of each device separately. The program will work for different input and output devices without any code changes. IO Streams Console Devices File Network - simple bytes, - primitive data types, - localized characters, - and objects
In a program, we read information from an input stream and write information to an output stream. IO Streams
A stream can represent many different kinds of sources and destinations: Disk files Devices Programs Network socket Memory arrays Streams support many different kinds of data: Simple bytes Primitive data types Localized characters Objects A program can manage multiple streams at a time. IO Streams
The stream classes are divided into two types, based on the data type: Byte Stream: For binary data Containing 8 - bit information Character Stream: For Unicode characters I M A S T R I N G \n Program Device Device 11101101 00000000 01101001 Program IO Streams
The java.io package contains a collection of stream classes that supports for reading and writing. Classes for byte streams: The InputStream Class The OutputStream Class Both classes are abstract Classes for character streams: The Reader class The Writer class Both classes are abstract o class java.lang. Object o class java.io. InputStream o class java.io. ByteArrayInputStream o class java.io. FileInputStream o class java.io. FilterInputStream o class java.io. OutputStream o class java.io. ByteArrayOutputStream o class java.io. FileOutputStream o class java.io. FilterOutputStream o class java.io. Reader o class java.io. BufferedReader o o class java.io. InputStreamReader o class java.io. Writer o class java.io. BufferedWriter o o class java.io. OutputStreamWriter IO Streams
Exception: while reading/writing in stream, errors may be occurs. IOException is thrown. Stream - exception try { copy(fi, fo); } catch (IOException ex){ System.err.println(ex); } public static void main(String args[]) throws IOException Example: Example: Using try/ catch block:
To read and write 8-bit bytes, programs should use the byte streams, descendents of InputStream and OutputStream. InputStream and OutputStream provide the API and partial implementation for input streams (streams that read 8-bit bytes) and output streams (streams that write 8-bit bytes). Byte Streams
InputStream class The basic purpose: read data from an input stream. Many subclasses: FileInputStream ByteArrayInputStream FilterInputStream ObjectInputStream InputStream FileInputStream ByteArrayInputStream FilterInputStream ObjectInputStream
read: Read every byte from file (in byte): int read() Return value (8 low bits of int): 0-255; end of stream: -1 int read(byte[] b)

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture