O'Reilly - Java IO (1999).pdf

(2182 KB) Pobierz
Java I/O
220149161.001.png
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 ..................................... ................
Correcting Misconceptions .......................................
Organization of the Book ........................................
Who You Are ................................................
Versions ...................................................
Security Issues ...............................................
Conventions Used in This Book ....................................
Request for Comments ..........................................
Acknowledgments .............................................
1
1
3
8
8
9
9
11
12
I: Basic I/O ................................... ...............
13
1. Introducing I/O .............................. ................
1.1 What Is a Stream? ...........................................
1.2 Numeric Data .............................................
1.3 Character Data .............................................
1.4 Readers and Writers .........................................
1.5 The Ubiquitous IOException ...................................
1.6 The Console: System.out, System.in, and System.err ....................
1.7 Security Checks on I/O .......................................
14
14
17
20
24
25
26
32
2. Output Streams ........................................... ..
2.1 The OutputStream Class ......................................
2.2 Writing Bytes to Output Streams .................................
2.3 Writing Arrays of Bytes .......................................
2.4 Flushing and Closing Output Streams ..............................
2.5 Subclassing OutputStream .....................................
2.6 A Graphical User Interface for Output Streams ........................
34
34
34
36
37
38
39
3. Input Streams ...............................................
3.1 The InputStream Class ........................................
3.2 The read( ) Method ..........................................
3.3 Reading Chunks of Data from a Stream ............................
3.4 Counting the Available Bytes ...................................
3.5 Skipping Bytes .............................................
3.6 Closing Input Streams ........................................
3.7 Marking and Resetting ........................................
3.8 Subclassing InputStream ......................................
3.9 An Efficient Stream Copier ....................................
42
42
42
44
45
46
46
47
47
48
II: Data Sources ...............................................
50
4. File Streams ................................................
4.1 Reading Files ..............................................
4.2 Writing Files ..............................................
4.3 File Viewer, Part 1 ..........................................
51
51
53
56
5. Network Streams ............................................
5.1 URLs ...................................................
5.2 URL Connections ...........................................
5.3 Sockets ..................................................
5.4 Server Sockets .............................................
5.5 URLViewer ...............................................
60
60
62
65
68
71
III: Filter Streams .............................................
74
6.1 The Filter Stream Classes ......................................
6.2 The Filter Stream Subclasses ...................................
6.3 Buffered Streams ...........................................
6.4 PushbackInputStream ........................................
6.5 Print Streams ..............................................
6.6 Multitarget Output Streams .....................................
6.7 File Viewer, Part 2 ..........................................
75
75
80
81
83
84
85
89
7. Data Streams ...............................................
7.1 The Data Stream Classes ......................................
7.2 Reading and Writing Integers ...................................
7.3 Reading and Writing Floating-Point Numbers ........................
7.4 Reading and Writing Booleans ..................................
7.5 Reading Byte Arrays .........................................
7.6 Reading and Writing Text .....................................
7.7 Miscellaneous Methods .......................................
7.8 Reading and Writing Little-Endian Numbers .........................
7.9 Thread Safety .............................................
7.10 File Viewer, Part 3 .........................................
96
96
98
103
106
106
107
111
111
123
124
8. Streams in Memory ...........................................
8.1 Sequence Input Streams .......................................
8.2 Byte Array Streams ..........................................
8.3 Communicating Between Threads with Piped Streams ...................
131
131
132
135
9. Compressing Streams .........................................
9.1 Inflaters and Deflaters ........................................
9.2 Compressing and Decompressing Streams ...........................
9.3 Working with Zip Files .......................................
9.4 Checksums ...............................................
9.5 JAR Files ................................................
9.6 File Viewer, Part 4 ..........................................
140
140
152
159
172
176
189
10. Cryptographic Streams .......................................
10.1 Hash Function Basics .......................................
10.2 The MessageDigest Class .....................................
10.3 Digest Streams ............................................
10.4 Encryption Basics ..........................................
10.5 The Cipher Class ..........................................
10.6 Cipher Streams ............................................
10.7 File Viewer, Part 5 .........................................
193
193
195
203
209
212
225
231
IV: Advanced and Miscellaneous Topics ..............................
236
11. Object Serialization ..........................................
11.1 Reading and Writing Objects ...................................
11.2 Object Streams ............................................
11.3 How Object Serialization Works ................................
11.4 Performa
11.5 The Serializable Interface .....................................
237
237
238
239
241
241
247
249
251
260
261
11.6 The ObjectInput and ObjectOutput Interfaces ........................
11.7 Versioning ...............................................
11.8 Customizing the Serialization Format .............................
11.9 Resolving Classes ..........................................
11.10 Resolving Objects .........................................
6. Filter Streams ...............................................
nce .............................................
11.11 Validation ..............................................
11.12 Sealed Objects ...........................................
261
263
12. Working with Files ..........................................
12.1 Understanding Files .........................................
12.2 Directories and Paths ........................................
12.3 The File Class ............................................
12.4 Filename Filters ...........................................
12.5 File Filters ...............................................
12.6 File Descriptors ...........................................
12.7 Random-Access Files .......................................
12.8 General Techniques for Cross-Platform File Access Code ................
267
267
274
280
299
300
301
302
304
13. File Dialogs and Choosers ......................................
13.1 File Dialogs ..............................................
13.2 JfileChooser ..............................................
13.3 File Viewer, Part 6 .........................................
306
306
313
331
14. Multilingual Character Sets and Unicode ...........................
14.1 Unicode ................................................
14.2 Displaying Unicode Text .....................................
14.3 Unicode Escapes ...........................................
14.4 UTF-8 .................................................
14.5 The char Data Type .........................................
14.6 Other Encodings ...........................................
14.7 Converting Between Byte Arrays and Strings ........................
337
337
338
345
346
348
356
357
15. Readers and Writers .........................................
15.1 The java.io.Writer Class ......................................
15.2 The OutputStreamWriter Class .................................
15.3 The java.io.Reader Class .....................................
15.4 The InputStreamReader Class ..................................
15.5 Character Array Readers and Writers .............................
15.6 String Readers and Writers ....................................
15.7 Reading and Writing Files ....................................
15.8 Buffered Readers and Writers ..................................
15.9 Print Writers .............................................
15.10 Piped Readers and Writers ...................................
15.11 Filtered Readers and Writers ..................................
15.12 File Viewer Finis ..........................................
360
360
361
363
365
366
369
372
374
378
380
381
386
16. Formatted I/O with java.text ....................................
16.1 The Old Way .............................................
16.2 Choosing a Locale ..........................................
16.3 Number Formats ...........................................
16.4 Specifying Width with FieldPosition ..............................
16.5 Parsing Input .............................................
16.6 Decimal Formats ..........................................
16.7 An Exponential Number Format ................................
395
395
397
400
408
412
414
423
17. The Java Communications API ..................................
17.1 The Architecture of the Java Communications API ....................
17.2 Identifying Ports ...........................................
17.3 Communicating with a Device on a Port ...........................
17.4 Serial Ports ..............................................
17.5 Parallel Ports .............................................
429
429
430
437
443
452
Zgłoś jeśli naruszono regulamin