This document discusses Java I/O streaming, including input streams, output streams, and filter streams. Input streams read data from sources using methods like read(), while output streams write data using methods like write(). Filter streams can modify the data as it is read from or written to underlying input/output streams, providing additional functionality. Common stream classes include FileInputStream, FileOutputStream, and DataOutputStream.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
40 views
Unit 1-Java Fundamentals
This document discusses Java I/O streaming, including input streams, output streams, and filter streams. Input streams read data from sources using methods like read(), while output streams write data using methods like write(). Filter streams can modify the data as it is read from or written to underlying input/output streams, providing additional functionality. Common stream classes include FileInputStream, FileOutputStream, and DataOutputStream.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12
UNIT 1-JAVA FUNDAMENTALS
Java I/O streaming – filter and pipe
streams – Byte Code interpretation - Threading – Swing JAVA I/O STREAMING • I/O in Java is built on streams • Input streams read data. Output streams write data. Different fundamental stream classes such as java.io.FileInputStream and sun.net.TelnetOutputStream read and write particular sources of data. • output streams have the same basic methods to write data • input streams use the same basic methods to read data • Filter streams can be chained to either an input stream or an output stream • Filters can modify the data as it's read or written • It provides additional methods for converting the data that's read or written into other formats java.io.DataOutputStream- converts an int to four bytes and writes those bytes onto its underlying output stream. Output Streams • Java's basic output class is java.io.OutputStream • This class provides the fundamental methods – public abstract void write(int b) throws IOException – public void write(byte[] data) throws IOException – public void write(byte[] data, int offset, int length) throws IOException – public void flush( ) throws IOException – public void close( ) throws IOException • Subclasses of OutputStream use these methods to write data onto particular media Example: • FileOutputStream uses these methods to write data into a file • TelnetOutputStream uses these methods to write data onto a network connection • ByteArrayOutputStream uses these methods to write data into an expandable byte array • OutputStream's fundamental method is write(int b) • This method takes as an argument an integer from to 255 and writes the corresponding byte to the output stream Input Streams • Java's basic input class is java.io.InputStream • This class provides the fundamental methods needed to read data as raw bytes – public abstract int read( ) throws IOException – public int read(byte[] input) throws IOException – public int read(byte[] input, int offset, int length) throws IOException – public long skip(long n) throws IOException – public int available( ) throws IOException – public void close( ) throws IOException • subclasses of InputStream use these methods to read data from particular media Example: • FileInputStream reads data from a file • TelnetInputStream reads data from a network connection • ByteArrayInputStream reads data from an array of bytes • The basic method of InputStream is read( ) method • This method reads a single byte of data from the input stream's source and returns it as a number from to 255 • The following code fragment reads 10 bytes from the InputStream in and stores them in the byte array input byte[] input = new byte[10]; for (int i = 0; i < input.length; i++) { int b = in.read( ); if (b == -1) break; input[i] = (byte) b; } Filter Streams • InputStream and OutputStream are fairly raw classes. They allow you to read and write bytes, either singly or in groups • The filters come in two versions – the filter streams and – the readers and writers The flow of data through a chain of filters