30 likes | 156 Vues
This chapter explores the concept of streams introduced in JDK 1.4, which provide a new way to handle primitive data types through the java.nio and java.nio.channels packages. Streams are sequences of bytes that ensure program independence from the device, divided into binary and character types. The java.io package includes InputStream and OutputStream classes, with InputStream featuring seven direct classes, including BufferedInputStream for efficient I/O. We also cover stream readers and writers, showcasing examples like BufferedReader for keyboard input and utilizing StreamTokenizer for structured data retrieval.
E N D
Understanding Streams • JDK 1.4 introduce a new way of handling primitive data types defined in java.nio & java.nio.channels packages • A stream is a sequence of bytes • The main reason for using streams is to make your program independent of the device involved • Two types • Binary • Character • The package java.io contains two main classes InputStream and OutputStream • InputStream has seven direct classes (Audio, File, Object, Sequence, ByteArray, Pipelined, Filter) • The FilterInputStream has further nine direct classes. One of them is BufferedInputStream (for efficient I/O)
Understanding Streams (cont.) • OutputStream has five direct classes (File, Object, ByteArray, Piped, Filter). • Stream readers and Writers are objects that can read and write byte streams as character streams • Reader has sic derived classes (InputSteam, Buffered, Filter, Piped, CharArray, String) • Ex: BufferedReader keyboard = new BufferedReader (new InputStreamReader(System.in)) • Getting data from the keyboard using the StreamTokenizer class • Methods suck as nextToken() returns int value and it can be TT_NUMBER, TT_WORD, TT_EOF, TT_EOL • Ex: FormatedInput.java & TestFormattedInput.java • Ex: FormattedWriter & TestedFormattedWriter