Java Input/Output  «Prev   Next»

Lesson 1

Java Input and Output: From Classis 1.1 to Java SE 17

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 Java 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.

Java "Input and Output" changes between the releases of Java SE 8 and Java SE 17

Here are some of the changes to Java Input/Output between Java SE 8 and Java SE 17.
  1. Compact String: Java 9 introduced Compact String, a performance enhancement to optimize the memory consumption of String objects.
  2. Text Block: Java 15 introduced Text Block, which allows us to embed code snippets and text sequences more or less as-is.
  3. New String Methods: Java 11 and Java 12 introduced many convenient functions so that we can rely on inbuilt functions for regular String operations.
  4. Records: Record classes are a special kind of class that can define immutable data objects in a much more compact way.
  5. Helpful NullPointerExceptions: Starting with Java 14, we can instruct the compiler with an additional VM argument to get helpful NPE messages.
  6. Pattern Matching: Pattern matching addresses a common logic in a program, namely the conditional extraction of components from objects, to be expressed more concisely and safely.
  7. Sealed Classes: Java 17 offers sealed classes as a standard feature. In short, a sealed class or interface can only be extended or implemented by those classes and interfaces which are permitted to do so.
  8. Restore Always-Strict Floating-Point Semantics: This JEP is mainly for scientific applications, and it makes floating-point operations consistently strict.
  9. Enhanced Pseudo-Random Number Generators: JEP 356 provides new interfaces and implementations for Pseudo-Random Number Generators (PRNG).

SEMrush Software