📄 chapter10.html
字号:
<font color=#0000ff>else</font> <font color=#0000ff>if</font>(f.isDirectory())
System.out.println(<font color=#004488>"it's a directory"</font>);
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
<font color=#0000ff>if</font>(args.length < 1) usage();
<font color=#0000ff>if</font>(args[0].equals(<font color=#004488>"-r"</font>)) {
<font color=#0000ff>if</font>(args.length != 3) usage();
File
old = <font color=#0000ff>new</font> File(args[1]),
rname = <font color=#0000ff>new</font> File(args[2]);
old.renameTo(rname);
fileData(old);
fileData(rname);
<font color=#0000ff>return</font>; <font color=#009900>// Exit main</font>
}
<font color=#0000ff>int</font> count = 0;
<font color=#0000ff>boolean</font> del = <font color=#0000ff>false</font>;
<font color=#0000ff>if</font>(args[0].equals(<font color=#004488>"-d"</font>)) {
count++;
del = <font color=#0000ff>true</font>;
}
<font color=#0000ff>for</font>( ; count < args.length; count++) {
File f = <font color=#0000ff>new</font> File(args[count]);
<font color=#0000ff>if</font>(f.exists()) {
System.out.println(f + <font color=#004488>" exists"</font>);
<font color=#0000ff>if</font>(del) {
System.out.println(<font color=#004488>"deleting..."</font> + f);
f.delete();
}
}
<font color=#0000ff>else</font> { <font color=#009900>// Doesn't exist</font>
<font color=#0000ff>if</font>(!del) {
f.mkdirs();
System.out.println(<font color=#004488>"created "</font> + f);
}
}
fileData(f);
}
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In <B>fileData( )</B> you can
see the various file investigation methods put to use to display information
about the file or directory path.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The first method that’s
exercised by <B>main( )</B> is
<A NAME="Index1080"></A><A NAME="Index1081"></A><B>renameTo( )</B>, which
allows you to rename (or move) a file to an entirely new path represented by the
argument, which is another <B>File</B> object. This also works with directories
of any length.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If you experiment with the above
program, you’ll find that you can make a directory path of any complexity
because <A NAME="Index1082"></A><A NAME="Index1083"></A><B>mkdirs( )</B>
will do all the work for you. In Java 1.0<A NAME="Index1084"></A>, the <B>-d</B>
flag reports that the directory is deleted but it’s still there; in Java
1.1<A NAME="Index1085"></A> the directory is actually
deleted.</FONT><A NAME="_Toc375545393"></A><A NAME="_Toc408018620"></A><BR></P></DIV>
<A NAME="Heading315"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Typical uses of IO streams</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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="Index1086"></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><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><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 + <font color=#004488>"\n"</font>;
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(
<font color=#004488>"End of stream encountered"</font>);
}
<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(
<font color=#004488>"IODemo.out"</font>)));
<font color=#0000ff>while</font>((s = in4.readLine()) != <font color=#0000ff>null</font> )
out1.println(
<font color=#004488>"Line "</font> + li.getLineNumber() + s);
out1.close(); <font color=#009900>// finalize() not reliable!</font>
} <font color=#0000ff>catch</font>(EOFException e) {
System.out.println(
<font color=#004488>"End of stream encountered"</font>);
}
<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(<font color=#004488>"Data.txt"</font>)));
out2.writeBytes(
<font color=#004488>"Here's the value of pi: \n"</font>);
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(<font color=#004488>"Data.txt"</font>)));
System.out.println(in5.readLine());
System.out.println(in5.readDouble());
} <font color=#0000ff>catch</font>(EOFException e) {
System.out.println(
<font color=#004488>"End of stream encountered"</font>);
}
<font color=#009900>// 6. Reading/writing random access files</font>
RandomAccessFile rf =
<font color=#0000ff>new</font> RandomAccessFile(<font color=#004488>"rtest.dat"</font>, <font color=#004488>"rw"</font>);
<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(<font color=#004488>"rtest.dat"</font>, <font color=#004488>"rw"</font>);
rf.seek(5*8);
rf.writeDouble(47.0001);
rf.close();
rf =
<font color=#0000ff>new</font> RandomAccessFile(<font color=#004488>"rtest.dat"</font>, <font color=#004488>"r"</font>);
<font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 0; i < 10; i++)
System.out.println(
<font color=#004488>"Value "</font> + i + <font color=#004488>": "</font> +
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(
<font color=#004488>"First line in file: "</font> +
in6.readLine());
in6.close();
<font color=#009900>// 8. Formatted file output shorthand</font>
PrintFile out3 = <font color=#0000ff>new</font> PrintFile(<font color=#004488>"Data2.txt"</font>);
out3.print(<font color=#004488>"Test of PrintFile"</font>);
out3.close();
<font color=#009900>// 9. Data file output shorthand</font>
OutFile out4 = <font color=#0000ff>new</font> OutFile(<font color=#004488>"Data3.txt"</font>);
out4.writeBytes(<font color=#004488>"Test of outDataFile\n\r"</font>);
out4.writeChars(<font color=#004488>"Test of outDataFile\n\r"</font>);
out4.close();
} <font color=#0000ff>catch</font>(FileNotFoundException e) {
System.out.println(
<font color=#004488>"File Not Found:"</font> + args[0]);
} <font color=#0000ff>catch</font>(IOException e) {
System.out.println(<font color=#004488>"IO Exception"</font>);
}
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><DIV ALIGN="LEFT"><P><A NAME="_Toc375545394"></A><A NAME="_Toc408018621"></A><BR></P></DIV>
<A NAME="Heading316"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Input streams</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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 <B>com.bruceeckel.tools</B>
created in Chapter 5.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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><BR></P></DIV>
<A NAME="Heading317"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
1. Buffered input file</H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To open a file for input, you use a
<A NAME="Index1087"></A><A NAME="Index1088"></A><B>FileInputStream</B> with a
<B>String</B> or a <B>File</B> 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="Index1089"></A><A NAME="Index1090"></A><B>BufferedInputStream</B>. To
read input in a formatted fashion, you give that resulting handle to the
constructor for a
<A NAME="Index1091"></A><A NAME="Index1092"></A><B>DataInputStream</B>, which is
your final object and the interface you read from.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In this example, only the
<A NAME="Index1093"></A><A NAME="Index1094"></A><B>readLine( )</B> method
is used, but of course any of the <B>DataInputStream</B> methods are available.
When you reach the end of the file, <B>readLine( )</B> returns <B>null</B>
so that is used to break out of the <B>while</B> loop.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>String s2</B> is used to
accumulate the entire contents of the file (including newlines that must be
added since <B>readLine( )</B> strips them off). <B>s2 </B>is then used in
the later portions of this program. Finally, <B>close( )</B> is called to
close the file. Technically, <B>close( )</B> will be called when
<B>finalize( )</B> is run, and this is supposed to happen (whether or not
garbage collection occurs) as the program exits. However, Java
1.0<A NAME="Index1095"></A> has a rather important bug, so this doesn’t
happen. In Java 1.1<A NAME="Index1096"></A> you must explicitly call
<A NAME="Index1097"></A><B>System.runFinalizersOnExit(true)</B> to guarantee
that <B>finalize( )</B> will be called for every object in the system. The
safest approach is to explicitly call
<A NAME="Index1098"></A><A NAME="Index1099"></A><B>close( )</B> for
files.</FONT><BR></P></DIV>
<A NAME="Heading318"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
2. Input from memory</H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This piece takes the <B>String
s2</B> that now contains the entire contents of the file and uses it to create a
<A NAME="Index1100"></A><A NAME="Index1101"></A><B>StringBufferInputStream.</B>
(A <B>String</B>, not a <A NAME="Index1102"></A><B>StringBuffer</B>, is required
as the constructor argument.) Then <B>read( )</B> is used to read each
character one at a time and send it out to the console. Note that
<B>read( )</B> returns the next byte as an <B>int</B> and thus it must be
cast to a <B>char</B> to print properly.</FONT><BR></P></DIV>
<A NAME="Heading319"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
3. Formatted memory input</H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The interface for
<B>StringBufferInputStream</B> is limited, so you usually enhance it by wrapping
it inside a
<A NAME="Index1103"></A><A NAME="Index1104"></A><B>DataInputStream</B>. However,
if you choose to read the characters out a byte at a time using
<B>readByte( )</B>, any value is valid so the return value cannot be used
to detect the end of input. Instead, y
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -