Title: Input Output Summary
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Input Output Summary

PrintStream Scanner Examples - basics

- System.out is a PrintStream, System.in is an InputStream ... Now we will look at output to Files.
- We should be using the Scanner class methods hasNextLine and hasNext when we call nextLine, and next.
- Comparing reading a file with reading from the keyboard

PrintStream Scanner Constructors

- Over all approach to opening a Scanner and PrintStream connected to files. Catching the necessary exceptions and a finally clause to close the Scanner and PrintStream. We start out using FileInputStream and FileOutputStream.
- Rework the previous example using the File class.
- PrintStream has a constructor that takes a String, but the Scanner class constructor that takes a String does not do what you might expect.

More complicated Scanner data

- Reading integers with nextInt and the possible exceptions. Guarding against these errors by catching the exception or by using hasNextInt.
- Reading data with mixed data types(double, int, String) : nextDouble, nextInt, next.
- Guarding against possible exceptions and other techniques for avoiding errors.

Buffering Performance

- Buffering classes: BufferedInputStream and BufferedOutputStream
- Reason why buffering is good and some actual timings using the Timer.java class.

ObjectOutputStream / ObjectInputStream

- Binary Input/Output usning the ObjectInputStream and ObjectOutputStream classes.
- Classes read and written need to be Serializable.
- Note the methods in ObjectOutputStream of writeByte, writeInt, writeLong, writeDouble writeUTF(for Strings), writeObject and matching methods for reading these entities back in with the ObjectInputStream class.
- Example of Dumping and Restoring a Zoo (from our cps161 homework).

Pausable Tic Tac Toe using Serializable

- Reworking the published answer to the cps161 Tic-Tac-Toe game to make it "Pausable" using ObjectOutputStream and ObjectInputStream. With this change, we can "Pause" our game and come back and resume it later byte writing out the whole TicTacToe class to a file and reading it back in.

File Class

- This video covers the File class and many of its useful methods: delete, renameTo, list, mkdir, lastModified, isDirectory, size, etc. This class provides us a way to work with your File System.

Recursion using File class

- In this video we create a program which will recursively walk through a directory and count the number of files and the total number of bytes associated with the directory.
- Not only is this a good way to see the File class in action, but this is also a good example of using Recursive programming in Java. In certain circumstances recursive programming can provide a powerful way to solve a problem.

Alternative to Recursion: Stack Approach

- If you didn't know how to write a recursive algorithm, you could use a Stack to solve the same problem. In this example, we solve the previous problem again using a Stack. Sometimes it is easier to see the solution when using the "Stack Algorithm" approach and sometimes it is easier to see the solution when using the "Recursive Algorithm" approach.

RandomAccessFile class

- Most Java Input/Output files tend to do either input or output in a sequential manner. The RandomAccessFile class allows you to open a file for both input and output at the same time. The RandomAccessFile class also provides binary options as well as the ability to move randomly around in the file using a "seek" method.
- Many of the methods of RandomAccessFile remind us of ObjectInputStream and ObjectOutputStream. However, the additional methods seek, and getFilePointer are useful to move around within the file in a non-sequential fashion.
- An example (RandomPeople.java) is worked to demonstrate the usefulness of RandomAccessFile. This example is patterned on some work that once read and wrote PDF files.

Character Stream Classes

- Internationalization issues and the character based classes
- Important classes: Reader, Writer, FileReader, FileWriter, PrintWriter, etc.
- Examples of simple copy routines using Reader/Writer based classes
- Note that without the Scanner class there is a need for a conversion from InputStream to the Reader class. See the MyInput class.

System Properties

- Obtaining system properties: java.version, os.name, user.name, user.home, line.separator, file.separator, user.dir

Last Modified: June 4, 2015 5:13 PM