lec4 - Lecture 4 Java I/O Some useful classes in the...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Page 1 of 40 CSE 100, UCSD: LEC 4 Lecture 4 Java I/O Some useful classes in the java.io package I/O buffering Bit-by-bit I/O Reading: online documentation of the java.io package
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Page 2 of 40 CSE 100, UCSD: LEC 4 A quick tour of the java.io package The java.io package defines classes used for I/O We will look at some of the most important classes in that package, to understand how and why to use them (For more details, see the online documentation for the java.io package) The main “top-level” classes in this package are InputStream , OutputStream , Reader , and Writer These are all abstract classes (you cannot create an object of an abstract class; it is used to define a type with methods, which will be a superclass for other classes) These are sublcasses of Object , and in turn have subclasses, which we’ll look at soon ... First, let’s look at these four abstract classes Object InputStream OutputStream Reader Writer
Background image of page 2
Page 3 of 40 CSE 100, UCSD: LEC 4 InputStream, OutputStream, Reader, Writer As their names suggest, InputStream and Reader (and their subclasses) are used to read input from some source Depending on how you set things up, the source can be a keyboard, or a file, or a Unix pipe from another process, or a socket connection to another computer on the net, etc., etc. And as their names suggest, OutputStream and Writer (and their subclasses) are used to write output to some destination Depending on how you set things up, the destination can be a terminal screen, or a file, or a Unix pipe to another process, or a socket connection to another computer on the net, etc. An important difference: Reader and Writer are for character I/O (“text”) InputStream and OutputStream are for byte I/O (“binary”) ...Though keep in mind that fundamentally all I/O is done byte-by-byte
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Page 4 of 40 CSE 100, UCSD: LEC 4 Reader and Writer Reader and Writer are abstract classes, so you can’t create any Reader or Writer objects directly However, they have many nonabstract subclasses; you can create objects of these subclasses Subclasses of Reader and Writer should be used when you want to do character- oriented I/O In Java, the char data type is 16 bits, and can represent any Unicode character In Java, String objects consist of a sequence of chars So if you want to do I/O involving text, i.e. String s or single chars possibly organized into lines of text, use Reader (for input) or Writer (for output) classes Writer and Reader classes ‘know how’ to convert 16-bit Unicode chars to or from a stream of bytes, according to an encoding scheme (sometimes called a ‘charset’) Depending on the encoding scheme, one 16-bit char can be represented by one, two, three, or four bytes java.nio.charset.Charset.defaultCharset() returns the default encoding for a JVM; some Reader and Writer constructors let you specify a particular encoding when creating the object
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 40

lec4 - Lecture 4 Java I/O Some useful classes in the...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online