⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter10.html

📁 java 是一个很好的网络开发环境。由于它是通过解释的方法
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<!--
This document was converted from RTF source: 
By rtftohtml 4.19
See http://www.sunpack.com/RTF
Filename:Tjava14.rtf
Application Directory:c:\TOOLS\RTF2HTML\
Subject:
Author:Bruce Eckel
Operator:Bruce Eckel
Document Comments:
Version Comments:
Comments:
Keywords:
Translation Date:02/04/2000
Translation Time:23:25:15
Translation Platform:Win32
Number of Output files:27
This File:Chapter10.html
SplitDepth=1
SkipNavPanel=1
SkipLeadingToc=1
SkipTrailingToc=1
GenContents=1
GenFrames=1
GenIndex=1
-->
<HEAD lang="en"><META http-equiv="Content-Type" content="text/html">
<TITLE>10: The Java  IO system</TITLE>
</HEAD>

<BODY  BGCOLOR="#FFFFFF"><DIV ALIGN="CENTER">
  <a href="http://www.MindView.net">
  <img src="mindview-head.gif" alt="MindView Inc." BORDER = "0"></a>
  <CENTER>
    <FONT FACE="Verdana" size = "-1">
    [ <a href="README-HTML.txt">Viewing Hints</a> ]
    [ <a href="http://www.mindview.net/TIJ2/index.html">2nd Edition</a> ]
    [ <a href="http://www.mindview.net/MailingList.html">Free Newsletter</a> ] <br>
    [ <a href="http://www.mindview.net/Training.html">Seminars</a> ]
    [ <a href="http://www.mindview.net/javaCD2.html">Seminars on CD ROM</a> ]
    [ <a href="http://www.mindview.net/CPPServices/#ConsultingServices">Consulting</a> ]
    </FONT>
  <H2><FONT FACE="Verdana">
  Thinking in Java, 1st edition</FONT></H2>
  <H3><FONT FACE="Verdana">&copy;1998 by Bruce Eckel</FONT></H3>
  
    <FONT FACE="Verdana" size = "-1">
     [ <a href="Chapter09.html">Previous Chapter</a> ] 
    [ <a href="SimpleContents.html">Short TOC</a> ] 
    [ <a href="Contents.html">Table of Contents</a> ] 
    [ <a href="DocIndex.html">Index</a> ]
     [ <a href="Chapter11.html">Next Chapter</a> ] 
    </FONT>
    
  </CENTER>
  </P></DIV><A NAME="Chapter_10"></A><A NAME="_Toc375545381"></A><A NAME="_Toc407441454"></A><A NAME="_Toc408018609"></A><A NAME="Heading302"></A><FONT FACE = "Verdana"><H1 ALIGN="LEFT">
10: The Java <BR>IO system<A NAME="AAA"></A></H1></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Verdana" SIZE=4>Creating a good
input/output (IO) system is one of the more difficult tasks for the language
designer.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This is evidenced by the number of
different approaches. The challenge seems to be in covering all eventualities.
Not only are there different kinds of IO that you want to communicate with
(files, the console, network connections), but you need to talk to them in a
wide variety of ways (sequential, random-access, binary, character, by lines, by
words, etc.).</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The Java library designers attacked
the problem by creating lots of classes. In fact, there are so many classes for
Java&#8217;s IO system that it can be intimidating at first (ironically, the
Java IO design actually prevents an explosion of classes). There has also been a
significant change in the IO <A NAME="Index980"></A>library between Java
1.0<A NAME="Index981"></A> and Java 1.1<A NAME="Index982"></A>. Instead of
simply replacing the old library with a new one, the designers at Sun extended
the old library and added the new one alongside it. As a result you can
sometimes end up mixing the old and new libraries and creating even more
intimidating code. </FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This chapter will help you
understand the variety of IO classes in the standard Java library and how to use
them. The first portion of the chapter will introduce the &#8220;old&#8221; Java
1.0<A NAME="Index983"></A> IO stream library, since there is a significant
amount of existing code that uses that library. The remainder of the chapter
will introduce the new features in the Java 1.1 IO library. Note that when you
compile some of the code in the first part of the chapter with a Java 1.1
compiler you can get a &#8220;<A NAME="Index984"></A>deprecated feature&#8221;
warning message at compile time. The code still works; the compiler is just
suggesting that you use certain new features that are described in the latter
part of this chapter. It is valuable, however, to see the difference between the
old and new way of doing things and that&#8217;s why it was left in &#8211; to
increase your understanding (and to allow you to read code written for Java
1.0<A NAME="Index985"></A>).</FONT><A NAME="_Toc375545382"></A><A NAME="_Toc408018610"></A><BR></P></DIV>
<A NAME="Heading303"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Input and output</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The Java library classes for IO are
divided by <A NAME="Index986"></A>input and <A NAME="Index987"></A>output, as
you can see by looking at the online Java class hierarchy with your Web browser.
By inheritance, all classes derived from
<A NAME="Index988"></A><A NAME="Index989"></A><B>InputStream</B> have basic
methods called <A NAME="Index990"></A><A NAME="Index991"></A><B>read(&#160;)
</B>for reading a single byte or array of bytes. Likewise, all classes derived
from <A NAME="Index992"></A><A NAME="Index993"></A><B>OutputStream </B>have
basic methods called
<A NAME="Index994"></A><A NAME="Index995"></A><B>write(&#160;) </B>for writing a
single byte or array of bytes. However, you won&#8217;t generally use these
methods; they exist so more sophisticated classes can use them as they provide a
more useful interface. Thus, you&#8217;ll rarely create your stream object by
using a single class, but instead will layer multiple objects together to
provide your desired functionality. The fact that you create more than one
object to create a single resulting stream is the primary reason that
Java&#8217;s stream library is confusing.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">It&#8217;s helpful to categorize
the classes by their functionality. The library designers started by deciding
that all classes that had anything to do with input would be inherited from
<B>InputStream</B> and all classes that were associated with output would be
inherited from <B>OutputStream</B>.
</FONT><A NAME="_Toc375545383"></A><A NAME="_Toc408018611"></A><BR></P></DIV>
<A NAME="Heading304"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Types of InputStream</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>InputStream</B>&#8217;s job is
to represent classes that produce input from different sources. These sources
can be (and each has an associated subclass of <B>InputStream</B>):
<A NAME="Index996"></A><A NAME="Index997"></A> </FONT><BR></P></DIV>
<OL>
<LI><FONT FACE="Georgia">	An array of
bytes</FONT><LI><FONT FACE="Georgia">	A <B>String</B>
object</FONT><LI><FONT FACE="Georgia">	A
file</FONT><LI><FONT FACE="Georgia">	A &#8220;pipe,&#8221; which works
like a physical pipe: you put things in one end and they come out the
other</FONT><LI><FONT FACE="Georgia">	A sequence of other streams, so you
can collect them together into a single
stream</FONT><LI><FONT FACE="Georgia">	Other sources, such as an Internet
connection. (This will be discussed in a later
chapter.)</FONT></OL><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In addition,
the <B>FilterInputStream</B> is also a type of <B>InputStream</B>, to provide a
base class for "decorator" classes that attach attributes or useful interfaces
to input streams. This is discussed later.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Table 10-1. Types of
InputStream<A NAME="Index998"></A><A NAME="Index999"></A><A NAME="Index1000"></A><A NAME="Index1001"></A><A NAME="Index1002"></A><A NAME="Index1003"></A><A NAME="Index1004"></A><A NAME="Index1005"></A><A NAME="Index1006"></A><A NAME="Index1007"></A><A NAME="Index1008"></A><A NAME="Index1009"></A><A NAME="Index1010"></A><A NAME="Index1011"></A><A NAME="Index1012"></A><A NAME="Index1013"></A><A NAME="Index1014"></A><A NAME="Index1015"></A><A NAME="Index1016"></A><A NAME="Index1017"></A></B></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><TABLE BORDER>
<TR VALIGN="TOP">
<TH WIDTH=76 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Class</FONT><BR></P></DIV>
</TH>
<TH WIDTH=90 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Function</FONT><BR></P></DIV>
</TH>
<TH WIDTH=162 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Constructor
Arguments</FONT><BR></P></DIV>
</TH>
</TR>
<TR VALIGN="TOP">
<TH WIDTH=162 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">How to use it</FONT><BR></P></DIV>
</TH>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>ByteArray-InputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Allows a buffer in memory to be
used as an <B>InputStream</B>.</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The buffer from which to extract
the bytes.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">As a source of data. Connect it to
a <B>FilterInputStream</B> object to provide a useful
interface.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>StringBuffer-InputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Converts a <B>String</B> into an
<B>InputStream</B>.</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A <B>String</B>. The underlying
implementation actually uses a <B>StringBuffer</B>.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">As a source of data. Connect it to
a <B>FilterInputStream</B> object to provide a useful
interface.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>File-InputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">For reading information from a
file.</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A <B>String</B> representing the
file name, or a <B>File</B> or <B>FileDescriptor</B> object.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">As a source of data. Connect it to
a <B>FilterInputStream</B> object to provide a useful
interface.</FONT><BR></P></DIV>
</TD>
</TR>
</TABLE></P></DIV>

<DIV ALIGN="LEFT"><P><TABLE BORDER>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Piped-InputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Produces the data that&#8217;s
being written to the associated <B>PipedOutput-Stream</B>. Implements the
&#8220;piping&#8221; concept.</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>PipedOutputStream</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">As a source of data in
multithreading. Connect it to a <B>FilterInputStream</B> object to provide a
useful interface.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Sequence-InputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Coverts two or more
<B>InputStream</B> objects into a single <B>InputStream</B>.</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Two <B>InputStream</B> objects or
an <B>Enumeration</B> for a container of <B>InputStream</B>
objects.</FONT><BR></P></DIV>
</TD>
</TR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -