Chapter 25. Java IO


Chapter 25. Java I/O

So far in this book, we have demonstrated most of the classes we developed using simple driver programs that hard-coded the input arguments required by the class. This approach is okay for demonstration programs but not for real-life scientific and engineering applications. Ideally, you want to compile your analysis classes and driver programs once and have them be flexible enough to accept different input arguments every time the program is executed. What's more, you may wish to do different things with the output from your applications. Sometimes, printing the output to the screen may be sufficient. Other times you may want to write the output to a file. You may even wish to permanently save the data structure associated with an object.

Java provides a rich library of I/O classes, interfaces, and methods to meet a wide variety of data input and output needs. Java uses stream-based I/O. A stream can be thought of as a flow of data from a source to a destination. The data source might be a Java program, an input file, or the keyboard. The destination might be the console, an output file, or a Java program. In either case, the basic process is the same. A stream is opened between a source and destination and data is read from or written to the stream. Most of the traditional I/O classes can be found in the java.io package. The release of Java version 1.4 saw the introduction of the java.nio package and its subpackages that define additional I/O functionality. The contents of java.nio and its subpackages will be discussed briefly at the end of this chapter.

There are two general types of Java I/O streams ”byte and character. Byte streams are used to read or write 8-bit byte data, either a single byte or the contents of a byte array. Byte streams are useful for reading image or sound files or any other binary data set. Since reading and writing to text files is such a common programming activity, Java also provides a family of character I/O classes that read and write 16-bit character data. While byte streams can be used to read character data, you should generally use character streams for this purpose. Character streams can read any Unicode character, whereas byte streams can only read ISO-Latin-1 8-bit bytes.

There are five basic ways for a scientific or engineering program to read and write data ”through command line arguments, by using the standard I/O streams, by reading from or writing to a file, by putting a GUI front-end on the application, or by turning the application into a web-based program. In this chapter we will discuss the first three methods. An introduction to Java GUI development is provided in Chapter 26. A discussion of how to create Web-based applications using Java servlets is part of Chapter 27.

This chapter will be presented in a task-oriented manner. The Java I/O libraries are extensive and there isn't room to provide comprehensive coverage of all of them in this book. This chapter will provide a big-picture look at the I/O classes and methods you will most likely use in your scientific and engineering programming. It will also demonstrate three commonly used ways to read and write data. The topics covered in this chapter are ”

  • General concepts

  • Byte input streams

  • Byte output streams

  • Character input streams

  • Character output streams

  • Test case: an atmosphere modeling tool

  • Getting input from command line arguments

  • Using the standard I/O streams

  • Reading and writing to a file

  • Saving and restoring objects

  • The java.nio packages



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net