📄 tij0112.html
字号:
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DataOutputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(because it implements the equivalent interfaces). In addition, you can see that <A NAME="Index1142"></A><A NAME="Index1143"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>seek( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is used to move about in the file and change one of the values.
</FONT><a name="_Toc375545396"></a><a name="_Toc408018623"></a><P></DIV>
<A NAME="Heading325"></A><H3 ALIGN=LEFT>
Shorthand
for file manipulation
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Since
there are certain canonical forms that you’ll be using regularly with
files, you may wonder why you have to do all of that typing – this is one
of the drawbacks of the decorator pattern. This portion shows the creation and
use of shorthand versions of typical file reading and writing configurations.
These shorthands are placed in the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>com.bruceeckel.tools</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that was begun in Chapter 5 (See page
<A HREF=" PAGE#COM_EckelObjects_tools">196</A>).
To add each class to the library, simply place it in the appropriate directory
and add the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statement.
</FONT><P></DIV>
<A NAME="Heading326"></A><H4 ALIGN=LEFT>
7.
File input shorthand
<P><A NAME="Index1144"></A><A NAME="Index1145"></A><A NAME="Index1146"></A></H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
creation of an object that reads a file from a buffered
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DataInputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
can be encapsulated into a class called
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>InFile</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">:</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: InFile.java</font>
<font color="#009900">// Shorthand class for opening an input file</font>
<font color="#0000ff">package</font> com.bruceeckel.tools;
<font color="#0000ff">import</font> java.io.*;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> InFile <font color="#0000ff">extends</font> DataInputStream {
<font color="#0000ff">public</font> InFile(String filename)
<font color="#0000ff">throws</font> FileNotFoundException {
<font color="#0000ff">super</font>(
<font color="#0000ff">new</font> BufferedInputStream(
<font color="#0000ff">new</font> FileInputStream(filename)));
}
<font color="#0000ff">public</font> InFile(File file)
<font color="#0000ff">throws</font> FileNotFoundException {
<font color="#0000ff">this</font>(file.getPath());
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Both
the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
versions of the constructor and the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>File</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
versions are included, to parallel the creation of a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FileInputStream</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">Now
you can reduce your chances of repetitive stress syndrome while creating files,
as seen in the example.
</FONT><P></DIV>
<A NAME="Heading327"></A><H4 ALIGN=LEFT>
8.
Formatted file output shorthand
<P><A NAME="Index1147"></A><A NAME="Index1148"></A><A NAME="Index1149"></A></H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
same kind of approach can be taken to create a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PrintStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that writes to a buffered file. Here’s the extension to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>com.bruceeckel.tools</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">:</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: PrintFile.java</font>
<font color="#009900">// Shorthand class for opening an output file</font>
<font color="#009900">// for human-readable output.</font>
<font color="#0000ff">package</font> com.bruceeckel.tools;
<font color="#0000ff">import</font> java.io.*;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> PrintFile <font color="#0000ff">extends</font> PrintStream {
<font color="#0000ff">public</font> PrintFile(String filename)
<font color="#0000ff">throws</font> IOException {
<font color="#0000ff">super</font>(
<font color="#0000ff">new</font> BufferedOutputStream(
<font color="#0000ff">new</font> FileOutputStream(filename)));
}
<font color="#0000ff">public</font> PrintFile(File file)
<font color="#0000ff">throws</font> IOException {
<font color="#0000ff">this</font>(file.getPath());
}
} <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 it is not possible for a constructor to catch an exception that’s
thrown by a base-class constructor.
</FONT><P></DIV>
<A NAME="Heading328"></A><H4 ALIGN=LEFT>
9.
Data file output shorthand
<P><A NAME="Index1150"></A><A NAME="Index1151"></A><A NAME="Index1152"></A></H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Finally,
the same kind of shorthand can create a buffered output file for data storage
(as opposed to human-readable storage):
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: OutFile.java</font>
<font color="#009900">// Shorthand class for opening an output file</font>
<font color="#009900">// for data storage.</font>
<font color="#0000ff">package</font> com.bruceeckel.tools;
<font color="#0000ff">import</font> java.io.*;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> OutFile <font color="#0000ff">extends</font> DataOutputStream {
<font color="#0000ff">public</font> OutFile(String filename)
<font color="#0000ff">throws</font> IOException {
<font color="#0000ff">super</font>(
<font color="#0000ff">new</font> BufferedOutputStream(
<font color="#0000ff">new</font> FileOutputStream(filename)));
}
<font color="#0000ff">public</font> OutFile(File file)
<font color="#0000ff">throws</font> IOException {
<font color="#0000ff">this</font>(file.getPath());
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">It
is curious (and unfortunate) that the Java library designers didn’t think
to provide these conveniences as part of their standard.
</FONT><a name="_Toc408018624"></a><a name="_Toc375545397"></a><P></DIV>
<A NAME="Heading329"></A><H3 ALIGN=LEFT>
Reading
from standard input
<P><A NAME="Index1153"></A><A NAME="Index1154"></A><A NAME="Index1155"></A></H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Following
the approach pioneered in Unix of “standard input,” “standard
output,” and “standard error output,” Java has <A NAME="Index1156"></A><A NAME="Index1157"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.in</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
<A NAME="Index1158"></A><A NAME="Index1159"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.out,</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and <A NAME="Index1160"></A><A NAME="Index1161"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.err</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Throughout the book you’ve seen how to write to standard output using
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.out,</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
which is already pre-wrapped as a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PrintStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object.
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.err</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is likewise a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PrintStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
but
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.in</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is a raw
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>InputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
with no wrapping. This means that while you can use
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.out</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.err</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
right away,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.in</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
must be wrapped before you can read from it.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Typically,
you’ll want to read input a line at a time using
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>readLine( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
so you’ll want to wrap
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.in</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DataInputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This is the “old” Java 1.0<A NAME="Index1162"></A>
way to do line input. A bit later in the chapter you’ll see the Java 1.1<A NAME="Index1163"></A>
solution. Here’s an example that simply echoes each line that you type in:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: Echo.java</font>
<font color="#009900">// How to read from standard input</font>
<font color="#0000ff">import</font> java.io.*;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> Echo {
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
DataInputStream in =
<font color="#0000ff">new</font> DataInputStream(
<font color="#0000ff">new</font> BufferedInputStream(System.in));
String s;
<font color="#0000ff">try</font> {
<font color="#0000ff">while</font>((s = in.readLine()).length() != 0)
System.out.println(s);
<font color="#009900">// An empty line terminates the program</font>
} <font color="#0000ff">catch</font>(IOException e) {
e.printStackTrace();
}
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
reason for the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>try</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
block is that <A NAME="Index1164"></A><A NAME="Index1165"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>readLine( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
can throw an
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>IOException</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Note that
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.in</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
should also be buffered, as with most streams
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">It’s
a bit inconvenient that you’re forced to wrap
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.in</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DataInputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in each program, but perhaps it was designed this way to allow maximum
flexibility.
</FONT><a name="_Toc408018625"></a><P></DIV>
<A NAME="Heading330"></A><H3 ALIGN=LEFT>
Piped
streams
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
<A NAME="Index1166"></A><A NAME="Index1167"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PipedInputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and <A NAME="Index1168"></A><A NAME="Index1169"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PipedOutputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
have been mentioned only briefly in this chapter. This is not to suggest that
they aren’t useful, but their value is not apparent until you begin to
understand multithreading, since the piped streams are used to communicate
between threads. This is covered along with an example in Chapter 14.
</FONT><a name="_Toc375545398"></a><a name="_Toc408018626"></a><P></DIV>
<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0111.html">Prev</a> | <a href="tij0113.html">Next</a>
</div>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -