Skip to Content
Java Pocket Guide, 4th Edition
book

Java Pocket Guide, 4th Edition

by Robert Liguori, Patricia Liguori
August 2017
Intermediate to advanced
290 pages
4h 10m
English
O'Reilly Media, Inc.
Content preview from Java Pocket Guide, 4th Edition

Chapter 12. 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 I/O classes throw exceptions, including the IOException, which needs to be handled.

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

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

NIO.2 was introduced with Java SE 7 and is covered in the next chapter. NIO.2 extends NIO and provides a new filesystem API.

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java 8 Pocket Guide

Java 8 Pocket Guide

Robert Liguori, Patricia Liguori
Java 11 Cookbook - Second Edition

Java 11 Cookbook - Second Edition

Nick Samoylov, Mohamed Sanaulla

Publisher Resources

ISBN: 9781491938683Errata Page