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

📄 chapter10.html

📁 java 是一个很好的网络开发环境。由于它是通过解释的方法
💻 HTML
📖 第 1 页 / 共 5 页
字号:
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This doesn&#8217;t provide an
interface <I>per se</I>, just a requirement that a buffer be used. Attach an
interface object.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>LineNumber-InputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Keeps track of line numbers in the
input stream; you can call <B>getLineNumber(&#160;)</B> and
<B>setLineNumber(int)</B>.</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>InputStream</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This just adds line numbering, so
you&#8217;ll probably attach an interface object.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Pushback-InputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Has a one byte push-back buffer so
that you can push back the last character read.</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>InputStream</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Generally used in the scanner for a
compiler and probably included because the Java compiler needed it. You probably
won&#8217;t use this.</FONT><BR></P></DIV>
</TD>
</TR>
<A NAME="_Toc375545388"></A><A NAME="_Toc408018615"></A></TABLE></P></DIV>
<A NAME="Heading308"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Writing to an OutputStream <BR>with FilterOutputStream</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The complement to
<B>DataInputStream</B> is <B>DataOutputStream</B>, which formats each of the
primitive types and <B>String</B> objects onto a stream in such a way that any
<B>DataInputStream</B>, on any machine, can read them. All the methods start
with &#8220;write,&#8221; such as <B>writeByte(&#160;)</B>,
<B>writeFloat(&#160;)</B>, etc.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If you want to do true formatted
output, for example, to the console, use a <B>PrintStream</B>. This is the
endpoint that allows you to print all of the primitive data types and
<B>String</B> objects in a viewable format as opposed to
<B>DataOutputStream,</B> whose goal is to put them on a stream in a way that
<B>DataInputStream</B> can portably reconstruct them. The <B>System.out</B>
static object is a <B>PrintStream</B>.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The two important methods in
<B>PrintStream</B> are <B>print(&#160;)</B> and <B>println(&#160;)</B>, which
are overloaded to print out all the various types. The difference between
<B>print(&#160;)</B> and <B>println(&#160;)</B> is that the latter adds a
newline when it&#8217;s done.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>BufferedOutputStream</B> is a
modifier and tells the stream to use buffering so you don&#8217;t get a physical
write every time you write to the stream. You&#8217;ll probably always want to
use this with files, and possibly console IO.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Table 10-4. Types of
FilterOutputStream<A NAME="Index1038"></A><A NAME="Index1039"></A><A NAME="Index1040"></A><A NAME="Index1041"></A><A NAME="Index1042"></A><A NAME="Index1043"></A></B></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><TABLE BORDER>
<TR VALIGN="TOP">
<TH WIDTH=85 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Class</FONT><BR></P></DIV>
</TH>
<TH WIDTH=121 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Function</FONT><BR></P></DIV>
</TH>
<TH WIDTH=121 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=121 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>Data-OutputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Used in concert with
<B>DataInputStream</B> so you can write primitives (int, char, long, etc.) to a
stream in a portable fashion.</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>OutputStream</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Contains full interface to allow
you to write primitive types.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>PrintStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">For producing formatted output.
While <B>DataOutputStream</B> handles the <I>storage</I> of data,
<B>PrintStream</B> handles <I>display</I>.</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>OutputStream</B>, with optional
boolean indicating that the buffer is flushed with every
newline.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Should be the &#8220;final&#8221;
wrapping for your <B>OutputStream</B> object. You&#8217;ll probably use this a
lot.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Buffered-OutputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Use this to prevent a physical
write every time you send a piece of data. You&#8217;re saying &#8220;Use a
buffer.&#8221; You can call <B>flush(&#160;)</B> to flush the
buffer.</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>OutputStream</B>, with optional
buffer size.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This doesn&#8217;t provide an
interface <I>per se</I>, just a requirement that a buffer is used. Attach an
interface object.</FONT><BR></P></DIV>
</TD>
</TR>
<A NAME="_Toc375545389"></A><A NAME="_Toc408018616"></A></TABLE></P></DIV>
<A NAME="Heading309"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Off by itself:
<BR>RandomAccessFile<BR><A NAME="Index1044"></A><A NAME="Index1045"></A></H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>RandomAccessFile</B> is used for
files containing records of known size so that you can move from one record to
another using
<A NAME="Index1046"></A><A NAME="Index1047"></A><B>seek(&#160;)</B>, then read
or change the records. The records don&#8217;t have to be the same size; you
just have to be able to determine how big they are and where they are placed in
the file.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">At first it&#8217;s a little bit
hard to believe that <B>RandomAccessFile</B> is not part of the
<B>InputStream</B> or <B>OutputStream</B> hierarchy. It has no association with
those hierarchies other than that it happens to implement the
<A NAME="Index1048"></A><A NAME="Index1049"></A><B>DataInput</B> and
<A NAME="Index1050"></A><A NAME="Index1051"></A><B>DataOutput</B> interfaces
(which are also implemented by <B>DataInputStream</B> and
<B>DataOutputStream</B>). It doesn&#8217;t even use any of the functionality of
the existing <B>InputStream</B> or <B>OutputStream</B> classes &#8211;
it&#8217;s a completely separate class, written from scratch, with all of its
own (mostly native) methods. The reason for this may be that
<B>RandomAccessFile</B> has essentially different behavior than the other IO
types, since you can move forward and backward within a file. In any event, it
stands alone, as a direct descendant of <B>Object</B>.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Essentially, a
<B>RandomAccessFile</B> works like a <B>DataInputStream</B> pasted together with
a <B>DataOutputStream </B>and the methods <B>getFilePointer(&#160;)</B> to find
out where you are in the file, <B>seek(&#160;)</B> to move to a new point in the
file, and <B>length(&#160;)</B> to determine the maximum size of the file. In
addition, the constructors require a second argument (identical to
<B>fopen(&#160;) </B>in C) indicating whether you are just randomly reading
(<B>&#8220;r&#8221;</B>) or reading and writing (<B>&#8220;rw&#8221;</B>).
There&#8217;s no support for write-only files, which could suggest that
<B>RandomAccessFile</B> might have worked well if it were inherited from
<B>DataInputStream</B>.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">What&#8217;s even more frustrating
is that you could easily imagine wanting to seek within other types of streams,
such as a <B>ByteArrayInputStream</B>, but the seeking methods are available
only in <B>RandomAccessFile</B>, which works for files only.
<B>BufferedInputStream</B> does allow you to
<A NAME="Index1052"></A><A NAME="Index1053"></A><B>mark(&#160;)</B> a position
(whose value is held in a single internal variable) and
<A NAME="Index1054"></A><A NAME="Index1055"></A><B>reset(&#160;)</B> to that
position, but this is limited and not too
useful.</FONT><A NAME="_Toc375545390"></A><A NAME="_Toc408018617"></A><BR></P></DIV>
<A NAME="Heading310"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
The File class</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The
<A NAME="Index1056"></A><A NAME="Index1057"></A><B>File </B>class has a
deceiving name &#8211; you might think it refers to a file, but it
doesn&#8217;t. It can represent either the <I>name</I> of a particular file or
the <I>names</I> of a set of files in a directory. If it&#8217;s a set of files,
you can ask for the set with the
<A NAME="Index1058"></A><A NAME="Index1059"></A><B>list(&#160;)</B> method, and
this returns an array of <B>String</B>. It makes sense to return an array rather
than one of the flexible collection classes because the number of elements is
fixed, and if you want a different directory listing you just create a different
<B>File</B> object. In fact, &#8220;FilePath&#8221; would have been a better
name. This section shows a complete example of the use of this class, including
the associated
<A NAME="Index1060"></A><A NAME="Index1061"></A><B>FilenameFilter</B>
<B>interface</B>.</FONT><A NAME="_Toc375545391"></A><A NAME="_Toc408018618"></A><BR></P></DIV>
<A NAME="Heading311"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
A directory lister<BR><A NAME="Index1062"></A><A NAME="Index1063"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Suppose you&#8217;d like to see a
directory listing. The <B>File</B> object can be listed in two ways. If you call
<B>list(&#160;)</B> with no arguments, you&#8217;ll get the full list that the
<B>File</B> object contains. However, if you want a restricted list, for
example, all of the files with an extension of <B>.java</B>, then you use a
&#8220;directory filter,&#8221; which is a class that tells how to select the
<B>File</B> objects for display.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Here&#8217;s the code for the
example: </FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: DirList.java</font>
<font color=#009900>// Displays directory listing</font>
<font color=#0000ff>package</font> c10;
<font color=#0000ff>import</font> java.io.*;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> DirList {
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    <font color=#0000ff>try</font> {
      File path = <font color=#0000ff>new</font> File(<font color=#004488>"."</font>);
      String[] list;
      <font color=#0000ff>if</font>(args.length == 0)
        list = path.list();
      <font color=#0000ff>else</font> 
        list = path.list(<font color=#0000ff>new</font> DirFilter(args[0]));
      <font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 0; i &lt; list.length; i++)
        System.out.println(list[i]);
    } <font color=#0000ff>catch</font>(Exception e) {
      e.printStackTrace();
    }
  }
}

<font color=#0000ff>class</font> DirFilter <font color=#0000ff>implements</font> FilenameFilter {
  String afn;
  DirFilter(String afn) { <font color=#0000ff>this</font>.afn = afn; }

⌨️ 快捷键说明

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