📄 tij0112.html
字号:
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>finalize( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
will be called for every object in the system. The safest approach is to
explicitly call <A NAME="Index1099"></A><A NAME="Index1100"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>close( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for files.
</FONT><P></DIV>
<A NAME="Heading319"></A><H4 ALIGN=LEFT>
2.
Input from memory
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
piece takes the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String
s2
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that now contains the entire contents of the file and uses it to create a <A NAME="Index1101"></A><A NAME="Index1102"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StringBufferInputStream.</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(A
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
not a <A NAME="Index1103"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StringBuffer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
is required as the constructor argument.) Then
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>read( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is used to read each character one at a time and send it out to the console.
Note that
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>read( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
returns the next byte as an
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>int</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and thus it must be cast to a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>char</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to print properly.
</FONT><P></DIV>
<A NAME="Heading320"></A><H4 ALIGN=LEFT>
3.
Formatted memory input
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
interface for
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StringBufferInputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is limited, so you usually enhance it by wrapping it inside a <A NAME="Index1104"></A><A NAME="Index1105"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DataInputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
However, if you choose to read the characters out a byte at a time using
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>readByte( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
any value is valid so the return value cannot be used to detect the end of
input. Instead, you can use the <A NAME="Index1106"></A><A NAME="Index1107"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>available( )
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">method
to find out how many more characters are available. Here’s an example
that shows how to read a file one byte at a time:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: TestEOF.java</font>
<font color="#009900">// Testing for the end of file while reading</font>
<font color="#009900">// a byte at a time.</font>
<font color="#0000ff">import</font> java.io.*;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> TestEOF {
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
<font color="#0000ff">try</font> {
DataInputStream in =
<font color="#0000ff">new</font> DataInputStream(
<font color="#0000ff">new</font> BufferedInputStream(
<font color="#0000ff">new</font> FileInputStream("TestEof.java")));
<font color="#0000ff">while</font>(in.available() != 0)
System.out.print((<font color="#0000ff">char</font>)in.readByte());
} <font color="#0000ff">catch</font> (IOException e) {
System.err.println("IOException");
}
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Note
that
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>available( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
works differently depending on what sort of medium you’re reading from
– it’s literally “the number of bytes that can be read
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>without
blocking
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.”<A NAME="Index1108"></A><A NAME="Index1109"></A>
With a file this means the whole file, but with a different kind of stream this
might not be true, so use it thoughtfully.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
could also detect the end of input in cases like these by catching an
exception. However, the use of exceptions for control flow is considered a
misuse of that feature.
</FONT><P></DIV>
<A NAME="Heading321"></A><H4 ALIGN=LEFT>
4.
Line numbering and file output
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
example shows the use of the <A NAME="Index1110"></A><A NAME="Index1111"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>LineNumberInputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to keep track of the input line numbers. Here, you cannot simply gang all the
constructors together, since you have to keep a handle to the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>LineNumberInputStream.</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(Note that this is
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>not</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
an inheritance situation, so you cannot simply cast
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>in4</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>LineNumberInputStream.</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)
Thus,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>li</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
holds the handle to the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>LineNumberInputStream,</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
which is then used to create a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DataInputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for easy reading.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
example also shows how to write formatted data to a file. First, a <A NAME="Index1112"></A><A NAME="Index1113"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FileOutputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is created to connect to the file. For efficiency, this is made a <A NAME="Index1114"></A><A NAME="Index1115"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BufferedOutputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which is what you’ll virtually always want to do, but you’re forced
to do it explicitly. Then for the formatting it’s turned into a <A NAME="Index1116"></A><A NAME="Index1117"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PrintStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The data file created this way is readable as an ordinary text file.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">One
of the methods that indicates when a <A NAME="Index1118"></A><A NAME="Index1119"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DataInputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is exhausted is <A NAME="Index1120"></A><A NAME="Index1121"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>readLine( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which returns
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>null</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
when there are no more strings to read. Each line is printed to the file along
with its line number, which is acquired through
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>li</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You’ll
see an explicit
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>close( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>out1</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which would make sense
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>if</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
the program were to turn around and read the same file again. However, this
program ends without ever looking at the file
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>IODemo.out</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
As mentioned before, if you don’t call
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>close( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for all your output files, you might discover that the buffers don’t get
flushed so they’re incomplete.
</FONT><a name="_Toc375545395"></a><a name="_Toc408018622"></a><P></DIV>
<A NAME="Heading322"></A><H3 ALIGN=LEFT>
Output
streams
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
two primary kinds of output streams are separated by the way they write data:
one writes it for human consumption, and the other writes it to be re-acquired
by a <A NAME="Index1122"></A><A NAME="Index1123"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DataInputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The <A NAME="Index1124"></A><A NAME="Index1125"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>RandomAccessFile</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
stands alone, although its data format is compatible with the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DataInputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and <A NAME="Index1126"></A><A NAME="Index1127"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DataOutputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV>
<A NAME="Heading323"></A><H4 ALIGN=LEFT>
5.
Storing and recovering data
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
<A NAME="Index1128"></A><A NAME="Index1129"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PrintStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
formats data so it’s readable by a human. To output data so that it can
be recovered by another stream, you use a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DataOutputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to write the data and a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DataInputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to recover the data. Of course, these streams could be anything, but here a
file is used, buffered for both reading and writing.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Note
that the character string is written using <A NAME="Index1130"></A><A NAME="Index1131"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>writeBytes( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and not <A NAME="Index1132"></A><A NAME="Index1133"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>writeChars( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
If you use the latter, you’ll be writing the 16-bit Unicode characters.
Since there is no complementary “readChars” method in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DataInputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
you’re stuck pulling these characters off one at a time with <A NAME="Index1134"></A><A NAME="Index1135"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>readChar( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
So for ASCII, it’s easier to write the characters as bytes followed by a
newline; then use <A NAME="Index1136"></A><A NAME="Index1137"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>readLine( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to read back the bytes as a regular ASCII line.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
<A NAME="Index1138"></A><A NAME="Index1139"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>writeDouble( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
stores the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>double</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
number to the stream and the complementary <A NAME="Index1140"></A><A NAME="Index1141"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>readDouble( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
recovers it. But for any of the reading methods to work correctly, you must
know the exact placement of the data item in the stream, since it would be
equally possible to read the stored
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>double</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
as a simple sequence of bytes, or as a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>char</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
etc. So you must either have a fixed format for the data in the file or extra
information must be stored in the file that you parse to determine where the
data is located.
</FONT><P></DIV>
<A NAME="Heading324"></A><H4 ALIGN=LEFT>
6.
Reading and writing random access files
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">As
previously noted, the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>RandomAccessFile</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is almost totally isolated from the rest of the IO hierarchy, save for the fact
that it implements the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DataInput</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DataOutput</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
interfaces. So you cannot combine it with any of the aspects of the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>InputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>OutputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
subclasses. Even though it might make sense to treat a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ByteArrayInputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
as a random access element, you can use
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>RandomAccessFile</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to only open a file. You must assume a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>RandomAccessFile</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is properly buffered since you cannot add that.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
one option you have is in the second constructor argument: you can open a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>RandomAccessFile</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to read (
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>“r”</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)
or read and write (
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>“rw”</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">).</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Using
a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>RandomAccessFile</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is like using a combined
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DataInputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -