📄 tij0112.html
字号:
<html><body>
<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0111.html">Prev</a> | <a href="tij0113.html">Next</a>
</td>
</tr></table>
<hr>
<H2 ALIGN=LEFT>
Typical
uses of IO streams
</H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Although
there are a lot of IO stream classes in the library that can be combined in
many different ways, there are just a few ways that you’ll probably end
up using them. However, they require attention to get the correct combinations.
The following rather long example shows the creation and use of <A NAME="Index1087"></A>typical
IO configurations so you can use it as a reference when writing your own code.
Note that each configuration begins with a commented number and title that
corresponds to the heading for the appropriate explanation that follows in the
text.
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: IOStreamDemo.java</font>
<font color="#009900">// Typical IO Stream Configurations</font>
<font color="#0000ff">import</font> java.io.*;
<font color="#0000ff">import</font> com.bruceeckel.tools.*;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> IOStreamDemo {
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
<font color="#0000ff">try</font> {
<font color="#009900">// 1. Buffered input file</font>
DataInputStream in =
<font color="#0000ff">new</font> DataInputStream(
<font color="#0000ff">new</font> BufferedInputStream(
<font color="#0000ff">new</font> FileInputStream(args[0])));
String s, s2 = <font color="#0000ff">new</font> String();
<font color="#0000ff">while</font>((s = in.readLine())!= <font color="#0000ff">null</font>)
s2 += s + "\n";
in.close();
<font color="#009900">// 2. Input from memory</font>
StringBufferInputStream in2 =
<font color="#0000ff">new</font> StringBufferInputStream(s2);
<font color="#0000ff">int</font> c;
<font color="#0000ff">while</font>((c = in2.read()) != -1)
System.out.print((<font color="#0000ff">char</font>)c);
<font color="#009900">// 3. Formatted memory input</font>
<font color="#0000ff">try</font> {
DataInputStream in3 =
<font color="#0000ff">new</font> DataInputStream(
<font color="#0000ff">new</font> StringBufferInputStream(s2));
<font color="#0000ff">while</font>(<font color="#0000ff">true</font>)
System.out.print((<font color="#0000ff">char</font>)in3.readByte());
} <font color="#0000ff">catch</font>(EOFException e) {
System.out.println(
"End of stream encountered");
}
<font color="#009900">// 4. Line numbering & file output</font>
<font color="#0000ff">try</font> {
LineNumberInputStream li =
<font color="#0000ff">new</font> LineNumberInputStream(
<font color="#0000ff">new</font> StringBufferInputStream(s2));
DataInputStream in4 =
<font color="#0000ff">new</font> DataInputStream(li);
PrintStream out1 =
<font color="#0000ff">new</font> PrintStream(
<font color="#0000ff">new</font> BufferedOutputStream(
<font color="#0000ff">new</font> FileOutputStream(
"IODemo.out")));
<font color="#0000ff">while</font>((s = in4.readLine()) != <font color="#0000ff">null</font> )
out1.println(
"Line " + li.getLineNumber() + s);
out1.close(); <font color="#009900">// finalize() not reliable!</font>
} <font color="#0000ff">catch</font>(EOFException e) {
System.out.println(
"End of stream encountered");
}
<font color="#009900">// 5. Storing & recovering data</font>
<font color="#0000ff">try</font> {
DataOutputStream out2 =
<font color="#0000ff">new</font> DataOutputStream(
<font color="#0000ff">new</font> BufferedOutputStream(
<font color="#0000ff">new</font> FileOutputStream("Data.txt")));
out2.writeBytes(
"Here's the value of pi: \n");
out2.writeDouble(3.14159);
out2.close();
DataInputStream in5 =
<font color="#0000ff">new</font> DataInputStream(
<font color="#0000ff">new</font> BufferedInputStream(
<font color="#0000ff">new</font> FileInputStream("Data.txt")));
System.out.println(in5.readLine());
System.out.println(in5.readDouble());
} <font color="#0000ff">catch</font>(EOFException e) {
System.out.println(
"End of stream encountered");
}
<font color="#009900">// 6. Reading/writing random access files</font>
RandomAccessFile rf =
<font color="#0000ff">new</font> RandomAccessFile("rtest.dat", "rw");
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < 10; i++)
rf.writeDouble(i*1.414);
rf.close();
rf =
<font color="#0000ff">new</font> RandomAccessFile("rtest.dat", "rw");
rf.seek(5*8);
rf.writeDouble(47.0001);
rf.close();
rf =
<font color="#0000ff">new</font> RandomAccessFile("rtest.dat", "r");
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < 10; i++)
System.out.println(
"Value " + i + ": " +
rf.readDouble());
rf.close();
<font color="#009900">// 7. File input shorthand</font>
InFile in6 = <font color="#0000ff">new</font> InFile(args[0]);
String s3 = <font color="#0000ff">new</font> String();
System.out.println(
"First line in file: " +
in6.readLine());
in6.close();
<font color="#009900">// 8. Formatted file output shorthand</font>
PrintFile out3 = <font color="#0000ff">new</font> PrintFile("Data2.txt");
out3.print("Test of PrintFile");
out3.close();
<font color="#009900">// 9. Data file output shorthand</font>
OutFile out4 = <font color="#0000ff">new</font> OutFile("Data3.txt");
out4.writeBytes("Test of outDataFile\n\r");
out4.writeChars("Test of outDataFile\n\r");
out4.close();
} <font color="#0000ff">catch</font>(FileNotFoundException e) {
System.out.println(
"File Not Found:" + args[0]);
} <font color="#0000ff">catch</font>(IOException e) {
System.out.println("IO Exception");
}
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><a name="_Toc375545394"></a><a name="_Toc408018621"></a><P></DIV>
<A NAME="Heading317"></A><H3 ALIGN=LEFT>
Input
streams
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Of
course, one common thing you’ll want to do is print formatted output to
the console, but that’s already been simplified in the package
</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">
created in Chapter 5.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Parts
1 through 4 demonstrate the creation and use of input streams (although part 4
also shows the simple use of an output stream as a testing tool).
</FONT><P></DIV>
<A NAME="Heading318"></A><H4 ALIGN=LEFT>
1.
Buffered input file
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">To
open a file for input, you use a <A NAME="Index1088"></A><A NAME="Index1089"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FileInputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
with a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>File</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object as the file name. For speed, you’ll want that file to be buffered
so you give the resulting handle to the constructor for a <A NAME="Index1090"></A><A NAME="Index1091"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BufferedInputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
To read input in a formatted fashion, you give that resulting handle to the
constructor for a <A NAME="Index1092"></A><A NAME="Index1093"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DataInputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which is your final object and the interface you read from.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
this example, only the <A NAME="Index1094"></A><A NAME="Index1095"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>readLine( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method is used, but of course any of the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DataInputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
methods are available. When you reach the end of the file,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>readLine( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
returns
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>null</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
so that is used to break out of the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>while</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
loop.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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">
is used to accumulate the entire contents of the file (including newlines that
must be added since
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>readLine( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
strips them off).
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>s2
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">is
then used in the later portions of this program. Finally,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>close( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is called to close the file. Technically,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>close( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
will be called when
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>finalize( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is run, and this is supposed to happen (whether or not garbage collection
occurs) as the program exits. However, Java 1.0<A NAME="Index1096"></A>
has a rather important bug, so this doesn’t happen. In Java 1.1<A NAME="Index1097"></A>
you must explicitly call <A NAME="Index1098"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.runFinalizersOnExit(true)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to guarantee that
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -