没有合适的资源?快使用搜索试试~ 我知道了~
首页Java核心技术卷II:第9版高级特性概览
《Java核心技术卷II:高级特性 第9版》(英文原版)是由Cay S. Horstmann和Gary Cornell合著的一本经典计算机科学著作。本书是CoreJava系列的第二卷,专为深入理解Java编程语言的高级特性和最佳实践而设计,适合已经掌握了基础Java知识的开发者进一步提升技能。 在第9版中,作者们聚焦于Java的高级特性,包括但不限于并发编程、泛型、集合框架、I/O流、网络编程、异常处理、动态代理、反射和注解等。这些内容不仅介绍了技术原理,还提供了丰富的实战案例,帮助读者理解和掌握如何在实际项目中高效地利用这些特性。 本书特别强调了面向对象编程的深化,如设计模式的应用,以及Java平台的高级API,如Java SE 8引入的Lambda表达式和Stream API,这些都是现代Java开发中的核心内容。此外,书中还会探讨Java内存模型和垃圾回收机制,这对于理解程序性能和内存管理至关重要。 在版权方面,Oracle和Java是Oracle公司及其关联公司的注册商标,书中提到的其他名称可能也属于各自的持有者。尽管本书详尽细致,但作者和出版社不承担任何形式的明示或默示担保,也不对由于使用书中的信息或程序可能导致的附带或间接损失负责。 该书旨在提供最新的技术信息,但请注意,内容可能会随着技术的发展和Oracle的更新而发生变化。此文档仅为信息参考,不作法律保障,用户需保持对最新版本的了解,以便在实际工作中保持竞争力。 《Java核心技术卷II:高级特性 第9版》是一本不可或缺的Java学习资料,对于希望在专业领域进阶的程序员和软件工程师来说,深入研读这本书将极大地提升他们的技术实力和解决问题的能力。
资源详情
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/9297729/bg10.jpg)
Figure 1.2. Reader and writer hierarchy
abstract int read()
abstract void write(int c)
The read method returns either a Unicode code unit (as an integer between 0 and 65535) or -1 when you have reached the end of the file. The
write method is called with a Unicode code unit. (See Volume I, Chapter 3 for a discussion of Unicode code units.)
There are four additional interfaces: Closeable, Flushable, Readable, and Appendable (see Figure 1.3). The first two interfaces are very
simple, with methods
void close() throws IOException
and
void flush()
Figure 1.3. The Closeable, Flushable, Readable, and Appendable interfaces
![](https://csdnimg.cn/release/download_crawler_static/9297729/bg11.jpg)
respectively. The classes InputStream, OutputStream, Reader, and Writer all implement the Closeable interface.
Note
The java.io.Closeable interface extends the java.lang.AutoCloseable interface. Therefore, you can use the try-with-
resources statement with any Closeable. Why have two interfaces? The close method of the Closeable interface only throws an
IOException, whereas the AutoCloseable.close method may throw any exception.
OutputStream and Writer implement the Flushable interface.
The Readable interface has a single method
int read(CharBuffer cb)
The CharBuffer class has methods for sequential and random read/write access. It represents an in-memory buffer or a memory-mapped file.
(See Section 1.7.1, “The Buffer Data Structure,” on p. 77 for details.)
The Appendable interface has two methods for appending single characters and character sequences:
Appendable append(char c)
Appendable append(CharSequence s)
The CharSequence interface describes basic properties of a sequence of char values. It is implemented by String, CharBuffer,
StringBuilder, and StringBuffer.
Of the stream zoo classes, only Writer implements Appendable.
java.io.Closeable
5.0
• void close()
closes this Closeable. This method may throw an IOException.
java.io.Flushable
5.0
• void flush()
flushes this Flushable.
java.lang.Readable
5.0
• int read(CharBuffer cb)
attempts to read as many char values into cb as it can hold. Returns the number of values read, or -1 if no further values are available
from this Readable.
java.lang.Appendable
5.0
• Appendable append(char c)
• Appendable append(CharSequence cs)
appends the given code unit, or all code units in the given sequence, to this Appendable; returns this.
java.lang.CharSequence
1.4
• char charAt(int index)
returns the code unit at the given index.
• int length()
returns the number of code units in this sequence.
• CharSequence subSequence(int startIndex, int endIndex)
returns a CharSequence consisting of the code units stored at index startIndex to endIndex - 1.
• String toString()
returns a string consisting of the code units of this sequence.
1.1.3. Combining Stream Filters
FileInputStream and FileOutputStream give you input and output streams attached to a disk file. You need to pass the file name or full
path name of the file to the constructor. For example,
FileInputStream fin = new FileInputStream("employee.dat");
looks in the user directory for a file named employee.dat.
Tip
All the classes in java.io interpret relative path names as starting from the user’s working directory. You can get this directory by a call
to System.getProperty("user.dir").
![](https://csdnimg.cn/release/download_crawler_static/9297729/bg12.jpg)
Caution
Since the backslash character is the escape character in Java strings, be sure to use \\ for Windows-style path names (for example,
C:\\Windows\\win.ini). In Windows, you can also use a single forward slash (C:/Windows/win.ini) because most Windows
file-handling system calls will interpret forward slashes as file separators. However, this is not recommended—the behavior of the
Windows system functions is subject to change. Instead, for portable programs, use the file separator character for the platform on which
your program runs. It is available as the constant string java.io.File.separator.
Like the abstract InputStream and OutputStream classes, these classes support only reading and writing at the byte level. That is, we can
only read bytes and byte arrays from the object fin.
byte b = (byte) fin.read();
As you will see in the next section, if we just had a DataInputStream, we could read numeric types:
DataInputStream din = . . .;
double s = din.readDouble();
But just as the FileInputStream has no methods to read numeric types, the DataInputStream has no method to get data from a file.
Java uses a clever mechanism to separate two kinds of responsibilities. Some streams (such as the FileInputStream and the input stream
returned by the openStream method of the URL class) can retrieve bytes from files and other more exotic locations. Other streams (such as the
DataInputStream and the PrintWriter) can assemble bytes into more useful data types. The Java programmer has to combine the two.
For example, to be able to read numbers from a file, first create a FileInputStream and then pass it to the constructor of a
DataInputStream.
Click here to view code image
FileInputStream fin = new FileInputStream("employee.dat");
DataInputStream din = new DataInputStream(fin);
double s = din.readDouble();
If you look at Figure 1.1 again, you can see the classes FilterInputStream and FilterOutputStream. The subclasses of these classes
are used to add capabilities to raw byte streams.
You can add multiple capabilities by nesting the filters. For example, by default streams are not buffered. That is, every call to read asks the
operating system to dole out yet another byte. It is more efficient to request blocks of data instead and store them in a buffer. If you want buffering
and the data input methods for a file, you need to use the following rather monstrous sequence of constructors:
DataInputStream din = new DataInputStream(
new BufferedInputStream(
new FileInputStream("employee.dat")));
Notice that we put the DataInputStream last in the chain of constructors because we want to use the DataInputStream methods, and we
want them to use the buffered read method.
Sometimes you’ll need to keep track of the intermediate streams when chaining them together. For example, when reading input, you often need
to peek at the next byte to see if it is the value that you expect. Java provides the PushbackInputStream for this purpose.
Click here to view code image
PushbackInputStream pbin = new PushbackInputStream(
new BufferedInputStream(
new FileInputStream("employee.dat")));
Now you can speculatively read the next byte
int b = pbin.read();
and throw it back if it isn’t what you wanted.
if (b != '<') pbin.unread(b);
However, reading and unreading are the only methods that apply to a pushback input stream. If you want to look ahead and also read numbers,
then you need both a pushback input stream and a data input stream reference.
DataInputStream din = new DataInputStream(
pbin = new PushbackInputStream(
new BufferedInputStream(
new FileInputStream("employee.dat"))));
Of course, in the stream libraries of other programming languages, niceties such as buffering and lookahead are automatically taken care of, so it
is a bit of a hassle to resort, in Java, to combining stream filters. However, the ability to mix and match filter classes to construct truly useful
sequences of streams does give you an immense amount of flexibility. For example, you can read numbers from a compressed ZIP file by using
the following sequence of streams (see Figure 1.4):
Click here to view code image
ZipInputStream zin = new ZipInputStream(new FileInputStream("employee.zip"));
DataInputStream din = new DataInputStream(zin);
![](https://csdnimg.cn/release/download_crawler_static/9297729/bg13.jpg)
Figure 1.4. A sequence of filtered streams
(See Section 1.4, “ZIP Archives,” on p. 33 for more on Java’s handling of ZIP files.)
java.io.FileInputStream 1.0
• FileInputStream(String name)
• FileInputStream(File file)
creates a new file input stream using the file whose path name is specified by the name string or the file object. (The File class is
described at the end of this chapter.) Path names that are not absolute are resolved relative to the working directory that was set when
the VM started.
java.io.FileOutputStream 1.0
• FileOutputStream(String name)
• FileOutputStream(String name, boolean append)
• FileOutputStream(File file)
• FileOutputStream(File file, boolean append)
creates a new file output stream specified by the name string or the file object. (The File class is described at the end of this
chapter.) If the append parameter is true, an existing file with the same name will not be deleted and data will be added at the end of
the file. Otherwise, this method deletes any existing file with the same name.
java.io.BufferedInputStream 1.0
• BufferedInputStream(InputStream in)
creates a buffered stream. A buffered input stream reads bytes from a stream without causing a device access every time. When the
buffer is empty, a new block of data is read into the buffer.
java.io.BufferedOutputStream 1.0
• BufferedOutputStream(OutputStream out)
creates a buffered stream. A buffered output stream collects bytes to be written without causing a device access every time. When the
buffer fills up or when the stream is flushed, the data are written.
java.io.PushbackInputStream 1.0
• PushbackInputStream(InputStream in)
• PushbackInputStream(InputStream in, int size)
constructs a stream with one-byte lookahead or a pushback buffer of specified size.
• void unread(int b)
pushes back a byte, which is retrieved again by the next call to read.
Parameters: b The byte to be read again
1.2. Text Input and Output
When saving data, you have the choice between binary and text formats. For example, if the integer 1234 is saved in binary, it is written as the
sequence of bytes 00 00 04 D2 (in hexadecimal notation). In text format, it is saved as the string "1234". Although binary I/O is fast and
efficient, it is not easily readable by humans. We first discuss text I/O and cover binary I/O in Section 1.3, “Reading and Writing Binary Data,” on p.
25.
When saving text strings, you need to consider the character encoding. In the UTF-16 encoding, the string "1234" is encoded as 00 31 00
![](https://csdnimg.cn/release/download_crawler_static/9297729/bg14.jpg)
When saving text strings, you need to consider the . In the UTF-16 encoding, the string "1234" is encoded as 00 31 00
32 00 33 00 34 (in hex). However, many programs expect that text files are encoded in a different encoding. In ISO 8859-1, the encoding
most commonly used in the United States and Western Europe, the string would be written as 31 32 33 34, without the zero bytes.
The OutputStreamWriter class turns a stream of Unicode code units into a stream of bytes, using a chosen character encoding. Conversely,
the InputStreamReader class turns an input stream that contains bytes (specifying characters in some character encoding) into a reader that
emits Unicode code units.
For example, here is how you make an input reader that reads keystrokes from the console and converts them to Unicode:
InputStreamReader in = new InputStreamReader(System.in);
This input stream reader assumes the default character encoding used by the host system, such as the ISO 8859-1 encoding in Western Europe.
You can choose a different encoding by specifying it in the constructor for the InputStreamReader, for example:
InputStreamReader in = new InputStreamReader(new FileInputStream("kremlin.dat"), "ISO8859_5");
See Section 1.2.4, “Character Sets,” on p. 20 for more information on character encodings.
1.2.1. How to Write Text Output
For text output, use a PrintWriter. That class has methods to print strings and numbers in text format. There is even a convenience constructor
to link a PrintWriter to a FileWriter. The statement
PrintWriter out = new PrintWriter("employee.txt");
is equivalent to
PrintWriter out = new PrintWriter(new FileWriter("employee.txt"));
To write to a print writer, use the same print, println, and printf methods that you used with System.out. You can use these methods to
print numbers (int, short, long, float, double), characters, boolean values, strings, and objects.
For example, consider this code:
String name = "Harry Hacker";
double salary = 75000;
out.print(name);
out.print(' ');
out.println(salary);
This writes the characters
Harry Hacker 75000.0
to the writer out. The characters are then converted to bytes and end up in the file employee.txt.
The println method adds the correct end-of-line character for the target system ("\r\n" on Windows, "\n" on UNIX) to the line. This is the
string obtained by the call System.getProperty("line.separator").
If the writer is set to autoflush mode, all characters in the buffer are sent to their destination whenever println is called. (Print writers are always
buffered.) By default, autoflushing is not enabled. You can enable or disable autoflushing by using the PrintWriter(Writer out,
boolean autoFlush) constructor:
PrintWriter out = new PrintWriter(new FileWriter("employee.txt"), true); // autoflush
The print methods don’t throw exceptions. You can call the checkError method to see if something went wrong with the stream.
Note
Java veterans might wonder whatever happened to the PrintStream class and to System.out. In Java 1.0, the PrintStream class
simply truncated all Unicode characters to ASCII characters by dropping the top byte. (At the time, Unicode was still a 16-bit encoding.)
Clearly, that was not a clean or portable approach, and it was fixed with the introduction of readers and writers in Java 1.1. For
compatibility with existing code, System.in, System.out, and System.err are still streams, not readers and writers. But now the
PrintStream class internally converts Unicode characters to the default host encoding in the same way as the PrintWriter does.
Objects of type PrintStream act exactly like print writers when you use the print and println methods, but unlike print writers they
allow you to output raw bytes with the write(int) and write(byte[]) methods.
剩余1403页未读,继续阅读
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://profile-avatar.csdnimg.cn/99113b2042f1459690cf7035dd55e744_vic_black.jpg!1)
青冥遗落
- 粉丝: 18
- 资源: 8
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![](https://csdnimg.cn/release/wenkucmsfe/public/img/voice.245cc511.png)
最新资源
- 计算机人脸表情动画技术发展综述
- 关系数据库的关键字搜索技术综述:模型、架构与未来趋势
- 迭代自适应逆滤波在语音情感识别中的应用
- 概念知识树在旅游领域智能分析中的应用
- 构建is-a层次与OWL本体集成:理论与算法
- 基于语义元的相似度计算方法研究:改进与有效性验证
- 网格梯度多密度聚类算法:去噪与高效聚类
- 网格服务工作流动态调度算法PGSWA研究
- 突发事件连锁反应网络模型与应急预警分析
- BA网络上的病毒营销与网站推广仿真研究
- 离散HSMM故障预测模型:有效提升系统状态预测
- 煤矿安全评价:信息融合与可拓理论的应用
- 多维度Petri网工作流模型MD_WFN:统一建模与应用研究
- 面向过程追踪的知识安全描述方法
- 基于收益的软件过程资源调度优化策略
- 多核环境下基于数据流Java的Web服务器优化实现提升性能
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![](https://csdnimg.cn/release/wenkucmsfe/public/img/green-success.6a4acb44.png)