ebook img

Java I/O PDF

479 Pages·1999·2.123 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Java I/O

Java I/O Elliotte Rusty Harold Publisher: O'Reilly First Edition March 1999 ISBN: 1-56592-485-1, 596 pages All of Java's Input/Output (I/O) facilities are based on streams, which provide simple ways to read and write data of different types. Java™ I/O tells you all you need to know about the four main categories of streams and uncovers less-known features to help make your I/O operations more efficient. Plus, it shows you how to control number formatting, use characters aside from the standard ASCII character set, and get a head start on writing truly multilingual software Table of Contents Preface ..................................................... 1 Correcting Misconceptions ....................................... 1 Organization of the Book ........................................ 3 Who You Are ................................................ 8 Versions ................................................... 8 Security Issues ............................................... 9 Conventions Used in This Book .................................... 9 Request for Comments .......................................... 11 Acknowledgments ............................................. 12 I: Basic I/O .................................................. 13 1. Introducing I/O .............................................. 14 1.1 What Is a Stream? ........................................... 14 1.2 Numeric Data ............................................. 17 1.3 Character Data ............................................. 20 1.4 Readers and Writers ......................................... 24 1.5 The Ubiquitous IOException ................................... 25 1.6 The Console: System.out, System.in, and System.err .................... 26 1.7 Security Checks on I/O ....................................... 32 2. Output Streams ............................................. 34 2.1 The OutputStream Class ...................................... 34 2.2 Writing Bytes to Output Streams ................................. 34 2.3 Writing Arrays of Bytes ....................................... 36 2.4 Flushing and Closing Output Streams .............................. 37 2.5 Subclassing OutputStream ..................................... 38 2.6 A Graphical User Interface for Output Streams ........................ 39 3. Input Streams ............................................... 42 3.1 The InputStream Class ........................................ 42 3.2 The read( ) Method .......................................... 42 3.3 Reading Chunks of Data from a Stream ............................ 44 3.4 Counting the Available Bytes ................................... 45 3.5 Skipping Bytes ............................................. 46 3.6 Closing Input Streams ........................................ 46 3.7 Marking and Resetting ........................................ 47 3.8 Subclassing InputStream ...................................... 47 3.9 An Efficient Stream Copier .................................... 48 II: Data Sources ............................................... 50 4. File Streams ................................................ 51 4.1 Reading Files .............................................. 51 4.2 Writing Files .............................................. 53 4.3 File Viewer, Part 1 .......................................... 56 5. Network Streams ............................................ 60 5.1 URLs ................................................... 60 5.2 URL Connections ........................................... 62 5.3 Sockets .................................................. 65 5.4 Server Sockets ............................................. 68 5.5 URLViewer ............................................... 71 III: Filter Streams ............................................. 74 6. Filter Streams ............................................... 75 6.1 The Filter Stream Classes ...................................... 75 6.2 The Filter Stream Subclasses ................................... 80 6.3 Buffered Streams ........................................... 81 6.4 PushbackInputStream ........................................ 83 6.5 Print Streams .............................................. 84 6.6 Multitarget Output Streams ..................................... 85 6.7 File Viewer, Part 2 .......................................... 89 7. Data Streams ............................................... 96 7.1 The Data Stream Classes ...................................... 96 7.2 Reading and Writing Integers ................................... 98 7.3 Reading and Writing Floating-Point Numbers ........................ 103 7.4 Reading and Writing Booleans .................................. 106 7.5 Reading Byte Arrays ......................................... 106 7.6 Reading and Writing Text ..................................... 107 7.7 Miscellaneous Methods ....................................... 111 7.8 Reading and Writing Little-Endian Numbers ......................... 111 7.9 Thread Safety ............................................. 123 7.10 File Viewer, Part 3 ......................................... 124 8. Streams in Memory ........................................... 131 8.1 Sequence Input Streams ....................................... 131 8.2 Byte Array Streams .......................................... 132 8.3 Communicating Between Threads with Piped Streams ................... 135 9. Compressing Streams ......................................... 140 9.1 Inflaters and Deflaters ........................................ 140 9.2 Compressing and Decompressing Streams ........................... 152 9.3 Working with Zip Files ....................................... 159 9.4 Checksums ............................................... 172 9.5 JAR Files ................................................ 176 9.6 File Viewer, Part 4 .......................................... 189 10. Cryptographic Streams ....................................... 193 10.1 Hash Function Basics ....................................... 193 10.2 The MessageDigest Class ..................................... 195 10.3 Digest Streams ............................................ 203 10.4 Encryption Basics .......................................... 209 10.5 The Cipher Class .......................................... 212 10.6 Cipher Streams ............................................ 225 10.7 File Viewer, Part 5 ......................................... 231 IV: Advanced and Miscellaneous Topics .............................. 236 11. Object Serialization .......................................... 237 11.1 Reading and Writing Objects ................................... 237 11.2 Object Streams ............................................ 238 11.3 How Object Serialization Works ................................ 239 11.4 Performance ............................................. 241 11.5 The Serializable Interface ..................................... 241 11.6 The ObjectInput and ObjectOutput Interfaces ........................ 247 11.7 Versioning ............................................... 249 11.8 Customizing the Serialization Format ............................. 251 11.9 Resolving Classes .......................................... 260 11.10 Resolving Objects ......................................... 261 11.11 Validation .............................................. 261 11.12 Sealed Objects ........................................... 263 12. Working with Files .......................................... 267 12.1 Understanding Files ......................................... 267 12.2 Directories and Paths ........................................ 274 12.3 The File Class ............................................ 280 12.4 Filename Filters ........................................... 299 12.5 File Filters ............................................... 300 12.6 File Descriptors ........................................... 301 12.7 Random-Access Files ....................................... 302 12.8 General Techniques for Cross-Platform File Access Code ................ 304 13. File Dialogs and Choosers ...................................... 306 13.1 File Dialogs .............................................. 306 13.2 JfileChooser .............................................. 313 13.3 File Viewer, Part 6 ......................................... 331 14. Multilingual Character Sets and Unicode ........................... 337 14.1 Unicode ................................................ 337 14.2 Displaying Unicode Text ..................................... 338 14.3 Unicode Escapes ........................................... 345 14.4 UTF-8 ................................................. 346 14.5 The char Data Type ......................................... 348 14.6 Other Encodings ........................................... 356 14.7 Converting Between Byte Arrays and Strings ........................ 357 15. Readers and Writers ......................................... 360 15.1 The java.io.Writer Class ...................................... 360 15.2 The OutputStreamWriter Class ................................. 361 15.3 The java.io.Reader Class ..................................... 363 15.4 The InputStreamReader Class .................................. 365 15.5 Character Array Readers and Writers ............................. 366 15.6 String Readers and Writers .................................... 369 15.7 Reading and Writing Files .................................... 372 15.8 Buffered Readers and Writers .................................. 374 15.9 Print Writers ............................................. 378 15.10 Piped Readers and Writers ................................... 380 15.11 Filtered Readers and Writers .................................. 381 15.12 File Viewer Finis .......................................... 386 16. Formatted I/O with java.text .................................... 395 16.1 The Old Way ............................................. 395 16.2 Choosing a Locale .......................................... 397 16.3 Number Formats ........................................... 400 16.4 Specifying Width with FieldPosition .............................. 408 16.5 Parsing Input ............................................. 412 16.6 Decimal Formats .......................................... 414 16.7 An Exponential Number Format ................................ 423 17. The Java Communications API .................................. 429 17.1 The Architecture of the Java Communications API .................... 429 17.2 Identifying Ports ........................................... 430 17.3 Communicating with a Device on a Port ........................... 437 17.4 Serial Ports .............................................. 443 17.5 Parallel Ports ............................................. 452 V: Appendixes ................................................ 458 A. Additional Resources ......................................... 459 A.1 Digital Think ............................................. 459 A.2 Design Patterns ............................................ 459 A.3 The java.io Package ......................................... 460 A.4 Network Programming ....................................... 460 A.5 Data Compression .......................................... 461 A.6 Encryption and Related Technology .............................. 461 A.7 Object Serialization ......................................... 462 A.8 International Character Sets and Unicode ........................... 462 A.9 Java Communications API ..................................... 463 A.10 Updates and Breaking News ................................... 463 B. Character Sets .............................................. 465 Colophon .................................................... 472 Dedication To Lynn, the best aunt a boy could ask for. Java I/O Preface In many ways this book is a prequel to my previous book, Java Network Programming (O'Reilly & Associates). When writing that book, I more or less assumed that readers were familiar with basic input and output in Java™—that they knew how to use input streams and output streams, convert bytes to characters, connect filter streams to each other, and so forth. However, after that book was published, I began to notice that a lot of the questions I got from readers of the book and students in my classes weren't so much about network programming itself as they were about input and output (I/O in programmer vernacular). When Java 1.1 was released with a vastly expanded java.io package and many new I/O classes spread out across the rest of the class library, it became obvious that a book that specifically addressed I/O was required. This is that book. Java I/O endeavors to show you how to really use Java's I/O classes, allowing you to quickly and easily write programs that accomplish many common tasks. Some of these include: • Reading and writing files • Communicating over network connections • Filtering data • Interpreting a wide variety of formats for integer and floating-point numbers • Passing data between threads • Encrypting and decrypting data • Calculating digital signatures for streams • Compressing and decompressing data • Writing objects to streams • Copying, moving, renaming, and getting information about files and directories • Letting users choose files from a GUI interface • Reading and writing non-English text in a variety of character sets • Formatting integer and floating-point numbers as strings • Talking directly to modems and other serial port devices • Talking directly to printers and other parallel port devices Java is the first language to provide a cross-platform I/O library that is powerful enough to handle all these diverse tasks. Java I/O is the first book to fully expose the power and sophistication of this library. Correcting Misconceptions Java is the first programming language with a modern, object-oriented approach to input and output. Java's I/O model is more powerful and more suited to real-world tasks than any other major language used today. Surprisingly, however, I/O in Java has a bad reputation. It is widely believed (falsely) that Java I/O can't handle basic tasks that are easily accomplished in other languages like C, C++, and Pascal. In particular, it is commonly said that: • I/O is too complex for introductory students; or, more specifically, there's no good way to read a number from the console. • Java can't handle basic formatting tasks like printing with three decimal digits of precision. 1 Java I/O This book will show you that not only can Java handle these two tasks with relative ease and grace; it can do anything C and C++ can do, and a whole lot more. Java's I/O capabilities not only match those of classic languages like C and Pascal, they vastly surpass them. The most common complaint about Java I/O among students, teachers, authors of textbooks, and posters to comp.lang.java is that there's no simple way to read a number from the console (System.in). Many otherwise excellent introductory Java books repeat this canard. Some textbooks go to great lengths to reproduce the behavior they're accustomed to from C or Pascal, apparently so teachers don't have to significantly rewrite the tired Pascal exercises they've been using for the last 20 years. However, new books that aren't committed to the old ways of doing things generally use command-line arguments for basic exercises, then rapidly introduce the graphical user interfaces any real program is going to use anyway. Apple wisely abandoned the command-line interface back in 1984, and the rest of the world is slowly catching up.[1] Although System.in and System.out are certainly convenient for teaching and debugging, in 1999 no completed, cross-platform program should even assume the existence of a console for either input or output. The second common complaint about Java I/O is that it can't handle formatted output; that is, that there's no equivalent of printf() in Java. In a very narrow sense, this is true because Java does not support the variable length argument lists a function like printf() requires. Nonetheless, a number of misguided souls (your author not least among them) have at one time or another embarked on futile efforts to reproduce printf() in Java. This may have been necessary in Java 1.0, but as of Java 1.1, it's no longer needed. The java.text package, discussed in Chapter 16, provides complete support for formatting numbers. Furthermore, the java.text package goes way beyond the limited capabilities of printf(). It supports not only different precisions and widths, but also internationalization, currency formats, percentages, grouping symbols, and a lot more. It can easily be extended to handle Roman numerals, scientific or exponential notation, or any other number format you may require. The underlying flaw in most people's analysis of Java I/O is that they've confused input and output with the formatting and interpreting of data. Java is the first major language to cleanly separate the classes that read and write bytes (primarily, various kinds of input streams and output streams) from the classes that interpret this data. You often need to format strings without necessarily writing them on the console. You may also need to write large chunks of data without worrying about what they represent. Traditional languages that connect formatting and interpretation to I/O and hard-wire a few specific formats are extremely difficult to extend to other formats. In essence, you have to give up and start from scratch every time you want to process a new format. Furthermore, C's printf(), fprintf(), and sprintf() family only really works well on Unix (where, not coincidentally, C was invented). On other platforms, the underlying assumption that every target may be treated as a file fails, and these standard library functions must be replaced by other functions from the host API. Java's clean separation between formatting and I/O allows you to create new formatting classes without throwing away the I/O classes, and to write new I/O classes while still using the old formatting classes. Formatting and interpreting strings are fundamentally different 1 MacOS X will reportedly add a real command-line shell to the Mac for the first time ever. Mainly, this is because MacOS X has Unix at its heart. However, Apple at least has the good taste to hide the shell so it won't confuse end users and tempt developers away from the righteous path of graphical user interfaces. 2 Java I/O operations from moving bytes from one device to another. Java is the first major language to recognize and take advantage of this. Organization of the Book This book has 17 chapters that are divided into four parts, plus two appendixes. Part I: Basic I/O Chapter 1 Chapter 1 introduces the basic architecture and design of the java.io package, including the reader/stream dichotomy. Some basic preliminaries about the int, byte, and char data types are discussed. The IOException thrown by many I/O methods is introduced. The console is introduced, along with some stern warnings about its proper use. Finally, I offer a cautionary message about how the security manager can interfere with most kinds of I/O, sometimes in unexpected ways. Chapter 2 Chapter 2 teaches you the basic methods of the java.io.OutputStream class you need to write data onto any output stream. You'll learn about the three overloaded versions of write(), as well as flush() and close(). You'll see several examples, including a simple subclass of OutputStream that acts like /dev/null and a TextArea component that gets its data from an output stream. Chapter 3 The third chapter introduces the basic methods of the java.io.InputStream class you need to read data from a variety of sources. You'll learn about the three overloaded variants of the read() method and when to use each. You'll see how to skip over data and check how much data is available, as well as how to place a bookmark in an input stream, then reset back to that point. You'll learn how and why to close input streams. This will all be drawn together with a StreamCopier program that copies data read from an input stream onto an output stream. This program will be used repeatedly over the next several chapters. Part II: Data Sources Chapter 4 The majority of I/O involves reading or writing files. Chapter 4 introduces the FileInputStream and FileOutputStream classes, concrete subclasses of InputStream and OutputStream that let you read and write files. These classes have all the usual methods of their superclasses, such as read(), write(), available(), flush(), and so on. Also in this chapter, development of a File Viewer program commences. You'll see how to inspect the raw bytes in a file in both decimal and hexadecimal format. This example will be progressively expanded throughout the rest of the book. 3

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.