Download presentation
Presentation is loading. Please wait.
Published byAsle Borgen Modified over 5 years ago
1
Streams and Readers The stream hierarchy is for reading bytes, the reader/writer hierarchy is for reading characters Unicode characters can be used internationally, variety of languages/characters, the future beyond ASCII bytes are used on most machines for external storage, convenient also for storing raw objects/values, e.g., int stored as 0x000000ff vs “255” More than 50 input/output streams readers/writers many are specialized, good to know that they’re there, but how can you pick which one to use? Examples: raw, RandomAccess, Buffered, PushBack, Zip, StringBuffer, Object, File, ...
2
Streams InputStream get raw bytes from a source
source could be URL, File, Pipe, String, … one abstract method, must be implemented, returns byte or -1 if no more data, this method blocks public abstract int read() throws IOException other read methods implemented in terms of read(), e.g., read(byte b[]) and read(byte b[], int offset, int length) OutputStream is similar, writes raw bytes one abstract method, two others similar to InputStream public abstract void write(int b) throws IOException Parameters and variables can be bound to concrete implementations of these abstract classes
3
Chaining Streams Read doubles, use buffering, from a file
DataInputStream input = new DataInputStream( new BufferedInputStream( new FileInputStream(“numbers.dat”))); double d = input.readDouble(); What about reading from a gzip’d file, use GZIPInputStream, from java.util.zip What about reading from a URL?, use URL class in java.net (URL.openStream()).
4
Readers/Writers for Unicode
Writing text (as opposed to bytes) can be done with the PrintWriter class has print(Foo f) method for all built-in types, for String, and for Object wrapper for other Writer objects and for OutputStreams Reading text can be done with a BufferedReader (for line oriented text) bind the reader to a FileReader or an InputStream of appropriate type use InputStreamReader as bridge between streams and readers specify character encoding here, e.g., 8859_7, which is ISO Latin/Arabic
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.