Java Input/Output  «Prev   Next»

Lesson 1

Java Input and Output

Java has several useful class libraries, and you have already been exposed to a few. The AWT is the class library that allows you to do graphics in an independent way. Then there are classes such as String and Thread that are part of the core lang package, java.lang. There are utility class libraries that allow you to have tables and vectors. You have already seen pieces of the I/O library. System.out is a print string that comes from that package. There is a package for dealing with network concepts like URLs and sockets. These packages are part of the core Java set, which means that they are available on any system that has Java. You can rely on them when you write your Java code. This module is going to explain how to use those packages, where they live, and how to find out more about them so that you can use them to their fullest extent.

Prior to Java 7

The APIs prior to Java 7 still had a few limitations when you had to write applications that focused heavily on files and file manipulation. Trying to write a little routine listing all the files created in the past day within a directory tree would give you some headaches. There was no support for navigating directory trees, and just reading attributes of a file was also quite hard.
In Java 7, this whole routine is less than 15 lines of code. Now what to name yet another I/O API? The name "new I/O" was taken, and "new new I/O" would just sound silly. Since the Java 7 functionality was added to package names that begin with java.nio, the new name was NIO.2. For the purposes of this chapter and the exam, NIO is shorthand for NIO.2. Since NIO (or NIO.2 if you like) builds upon the original I/O, some of those concepts are still tested on the Java Certification exam in addition to the new parts. Fortunately, you will not have to become a total I/O or NIO expert to do well on the exam. The intention of the exam team was to include just the basic aspects of these technologies, and in this module, we cover more than you will need to get through these objectives on the exam.

First Relase of Java Streams

In this module, you will learn how to program using streams. You will work with a number of classes in the java.io package. These classes define the methods you will use when writing to and reading from streams, such as files and Internet resources.
By the end of this module, you should be able to:
  1. Describe the important input/output classes.
  2. Use Filter streams.
  3. Read from and write to files.

  1. When a file is deserialized into an object, the class's constructor and instance initializers are not called. So the fields for which no value is available in the serialized file, are initialized to their default values (i.e. number fields to 0, boolean to false, and references to null).
  2. serialVersionUID denotes the version number of the class. If you don't specify serialVersionUID for a class that implements Serializable, the Java compiler automatically adds this field. It computes a value based on the attributes of the class such as the fields and interfaces, and assigns that value to serialVersionUID.
    It is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException.
  3. If the serialVersionUID for the serialized object and the actual class is the same then the deserialization completes successfully but any additional fields that are not present in the serialized file are initialized to their default value (as per point 1. above). Any fields that are missing in the class but are present in the serialized file are ignored.