📄 tij314.htm
字号:
}
<font color=#0000ff>private</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> fileData(File f) {
System.out.println(
<font color=#004488>"Absolute path: "</font> + f.getAbsolutePath() +
<font color=#004488>"\n Can read: "</font> + f.canRead() +
<font color=#004488>"\n Can write: "</font> + f.canWrite() +
<font color=#004488>"\n getName: "</font> + f.getName() +
<font color=#004488>"\n getParent: "</font> + f.getParent() +
<font color=#004488>"\n getPath: "</font> + f.getPath() +
<font color=#004488>"\n length: "</font> + f.length() +
<font color=#004488>"\n lastModified: "</font> + f.lastModified());
<font color=#0000ff>if</font>(f.isFile())
System.out.println(<font color=#004488>"It's a file"</font>);
<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>;
}
count--;
<font color=#0000ff>while</font>(++count < args.length) {
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=#0000ff>if</font>(args.length == 1 &&
args[0].equals(<font color=#004488>"MakeDirectoriesTest"</font>))
monitor.expect(<font color=#0000ff>new</font> String[] {
<font color=#004488>"%% (MakeDirectoriesTest exists"</font>+
<font color=#004488>"|created MakeDirectoriesTest)"</font>,
<font color=#004488>"%% Absolute path: "</font>
+ <font color=#004488>"\\S+MakeDirectoriesTest"</font>,
<font color=#004488>"%% Can read: (true|false)"</font>,
<font color=#004488>"%% Can write: (true|false)"</font>,
<font color=#004488>" getName: MakeDirectoriesTest"</font>,
<font color=#004488>" getParent: null"</font>,
<font color=#004488>" getPath: MakeDirectoriesTest"</font>,
<font color=#004488>"%% length: \\d+"</font>,
<font color=#004488>"%% lastModified: \\d+"</font>,
<font color=#004488>"It's a directory"</font>
});
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>In <b>fileData( )</b> you can see various file investigation methods used to display information about the file or directory path. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1636" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The first method that’s exercised by <b>main( )</b> is <a name="Index1181"></a><a name="Index1182"></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 size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1637" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>If you experiment with the preceding program, you’ll find that you can make a directory path of any complexity, because <a name="Index1183"></a><a name="Index1184"></a><b>mkdirs( )</b> will do all the work for you. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1638" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h2>
<a name="_Toc24775787"></a><a name="Heading14057"></a>Input and output<br></h2>
<p><a name="Index1185"></a><a name="Index1186"></a><a name="Index1187"></a><a name="Index1188"></a><a name="Index1189"></a><a name="Index1190"></a><a name="Index1191"></a><a name="Index1192"></a><a name="Index1193"></a><a name="Index1194"></a><a name="Index1195"></a><a name="Index1196"></a><a name="Index1197"></a><a name="Index1198"></a><a name="Index1199"></a>I/O libraries often use the abstraction of a <i>stream</i>, which represents any data source or sink as an object capable of producing or receiving pieces of data. The stream hides the details of what happens to the data inside the actual I/O device. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1639" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The Java library classes for I/O are divided by input and output, as you can see by looking at the class hierarchy in the JDK documentation. By inheritance, everything derived from the <b>InputStream</b> or <b>Reader </b>classes have basic methods called <b>read( ) </b>for reading a single byte or array of bytes. Likewise, everything derived from <b>OutputStream </b>or <b>Writer </b>classes have basic methods called <b>write( ) </b>for writing a single byte or array of bytes. However, you won’t generally use these methods; they exist so that other classes can use them—these other classes provide a more useful interface. Thus, you’ll rarely create your stream object by using a single class, but instead will layer multiple objects together to provide your desired functionality. The fact that you create more than one object to create a single resulting stream is the primary reason that Java’s stream library is confusing. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1640" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>It’s helpful to categorize the classes by their functionality. In Java 1.0, the library designers started by deciding that all classes that had anything to do with input would be inherited from <b>InputStream</b>, and all classes that were associated with output would be inherited from <b>OutputStream</b>. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1641" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc375545383"></a><a name="_Toc24775788"></a><a name="Heading14061"></a>Types
of <b>InputStream</b></h3>
<p><b>InputStream</b>’s job is to represent classes that produce input from different sources. These sources can be: <br></p>
<ol>
<li>An array of bytes.</li>
<li>A <b>String</b> object.</li>
<li>A file.</li>
<li>A “pipe,” which works like a physical
<a name="Index1200"></a><a name="Index1201"></a>pipe: You put things in at one
end and they come out the other.</li>
<li>A sequence of other streams, so you can collect them together into a single
stream.</li>
<li>Other sources, such as an Internet connection. (This is covered in
<i>Thinking in Enterprise Java</i>.) <font size="-2"><a
href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1642" title="Send BackTalk
Comment">Feedback</a></font></li></ol><p>Each of these has an associated subclass of <b>InputStream</b>. In addition, the <b>FilterInputStream</b> is also a type of <b>InputStream</b>, to provide a base class for "decorator" classes that attach attributes or useful interfaces to input streams. This is discussed later. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1643" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><b>Table 12-1. Types of InputStream</b><br></p>
<div align="center" style="position:relative; left: 0"><table border="1">
<tr valign="top">
<th width="131.999967" colspan="1" rowspan="2" valign="top">
<p class="Table"><a name="Index1202"></a><a name="Index1203"></a><a name="Index1204"></a><a name="Index1205"></a><a name="Index1206"></a><a name="Index1207"></a><a name="Index1208"></a><a name="Index1209"></a><a name="Index1210"></a><a name="Index1211"></a><a name="Index1212"></a><a name="Index1213"></a><a name="Index1214"></a><a name="Index1215"></a><a name="Index1216"></a><a name="Index1217"></a><a name="Index1218"></a><a name="Index1219"></a><a name="Index1220"></a><a name="Index1221"></a><b>Class</b><br></p>
</th>
<th width="143.999964" colspan="1" rowspan="2" valign="top">
<p class="Table"><b>Function</b><br></p>
</th>
<th width="203.999949" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>Constructor Arguments</b><br></p>
</th>
</tr>
<tr valign="top">
<th width="203.999949" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>How to use it</b><br></p>
</th>
</tr>
<tr valign="top">
<td width="131.999967" colspan="1" rowspan="2" valign="top">
<p class="Table"><b>ByteArray-InputStream</b><br></p>
</td>
<td width="143.999964" colspan="1" rowspan="2" valign="top">
<p class="Table">Allows a buffer in memory to be used as an <b>InputStream</b>.<br></p>
</td>
<td width="203.999949" colspan="1" rowspan="1" valign="top">
<p class="Table">The buffer from which to extract the bytes.<br></p>
</td>
</tr>
<tr valign="top">
<td width="203.999949" colspan="1" rowspan="1" valign="top">
<p class="Table">As a source of data: Connect it to a <b>FilterInputStream</b> object to provide a useful interface.<br></p>
</td>
</tr>
<tr valign="top">
<td width="131.999967" colspan="1" rowspan="2" valign="top">
<p class="Table"><b>StringBuffer-InputStream</b><br></p>
</td>
<td width="143.999964" colspan="1" rowspan="2" valign="top">
<p class="Table">Converts a <b>String</b> into an <b>InputStream</b>.<br></p>
</td>
<td width="203.999949" colspan="1" rowspan="1" valign="top">
<p class="Table">A <b>String</b>. The underlying implementation actually uses a <b>StringBuffer</b>.<br></p>
</td>
</tr>
<tr valign="top">
<td width="203.999949" colspan="1" rowspan="1" valign="top">
<p class="Table">As a source of data: Connect it to a <b>FilterInputStream</b> object to provide a useful interface.<br></p>
</td>
</tr>
<tr valign="top">
<td width="131.999967" colspan="1" rowspan="2" valign="top">
<p class="Table"><b>File-InputStream</b><br></p>
</td>
<td width="143.999964" colspan="1" rowspan="2" valign="top">
<p class="Table">For reading information from a file.<br></p>
</td>
<td width="203.999949" colspan="1" rowspan="1" valign="top">
<p class="Table">A <b>String</b> representing the file name, or a <b>File</b> or <b>FileDescriptor</b> object.<br></p>
</td>
</tr>
<tr valign="top">
<td width="203.999949" colspan="1" rowspan="1" valign="top">
<p class="Table">As a source of data: Connect it to a <b>FilterInputStream</b> object to provide a useful interface.<br></p>
</td>
</tr>
<tr valign="top">
<td width="131.999967" colspan="1" rowspan="2" valign="top">
<p class="Table"><b>Piped-InputStream</b><br></p>
</td>
<td width="143.999964" colspan="1" rowspan="2" valign="top">
<p class="Table">Produces the data that’s being written to the associated <b>PipedOutput</b>-<b>Stream</b>. Implements the “piping” concept.<br></p>
</td>
<td width="203.999949" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>PipedOutputStream</b><br></p>
</td>
</tr>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -