O'Reilly logo

Java Pocket Guide by Patricia Liguori, Robert Liguori

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 11. Basic Input and Output

Java provides several classes for basic input and output, a few of which are discussed in this chapter. The basic classes can be used to read and write to files, sockets, and the console. They also provide for working with files and directories and for serializing data. Java IO classes throw exceptions, including the IOException, which needs to be handled.

Java IO classes also support formatting data, compressing and decompressing streams, encrypting and decrypting, and communicating between threads using piped streams.

The new IO (NIO) APIs that were introduced in Java 1.4 provide additional IO capabilities, including buffering, file locking, regular expression matching, scalable networking, and buffer management.

Standard Streams in, out, and err

Java uses three standard streams: in, out, and err.

System.in is the standard input stream that is used to get data from the user to a program.

	byte teamName[] = new byte[200];
	int size = System.in.read(teamName);
	System.out.write(teamName,0,size);

System.out is the standard output stream that is used to output data from a program to the user.

	System.out.print("Team complete");

System.err is the standard error stream that is used to output error data from a program to the user.

	System.err.println("Not enough players");

Note that each of these methods can throw an IOException.

Class Hierarchy for Basic Input and Output

Figure 11-1 shows a class hierarchy for commonly used readers, writers, and input and output ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required