📄 192-195.html
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1558515682"><META name=vstitle content="Java Digital Signal Processing"><META name=vsauthor content="Douglas A. Lyon"><META name=vsimprint content="M&T Books"><META name=vspublisher content="IDG Books Worldwide, Inc."><META name=vspubdate content="11/01/97"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Java Digital Signal Processing:Futil Recipes for Feudal Times</TITLE>
<!-- HEADER --><STYLE type="text/css"> <!-- A:hover { color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<!--ISBN=1558515682//-->
<!--TITLE=Java Digital Signal Processing//-->
<!--AUTHOR=Douglas A. Lyon//-->
<!--PUBLISHER=IDG Books Worldwide, Inc.//-->
<!--IMPRINT=M & T Books//-->
<!--CHAPTER=4//-->
<!--PAGES=192-195//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="188-192.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="195-199.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading40"></A><FONT COLOR="#000077">Futil.available</FONT></H4>
<P>The <I>Futil.available</I> method is a static public method that permits a fast file check to see how many bytes are in a file. This operation was developed for the recursive <I>futils.DirList</I> class (which appears in the next section).</P>
<!-- CODE //-->
<PRE>
// Open the file, return -1 if file cannot be opened
// otherwise return the size in bytes
public static int available(File file) {
FileInputStream fis = null;
int sizeInBytes = -1;
try {
fis = new FileInputStream(file);
sizeInBytes = fis.available();
fis.close();
}
catch (IOException e)
{System.out.println("Futil:Could not open file");}
return sizeInBytes;
}
</PRE>
<!-- END CODE //-->
<H4 ALIGN="LEFT"><A NAME="Heading41"></A><FONT COLOR="#000077">The futils.DirList Class: Recursive File Lister</FONT></H4>
<P>The <I>DirList</I> class resides in the <I>futils</I> package. If <I>DirList.main</I> is invoked, a standard file open dialog box is presented to the user. The user selects a file, and <I>DirList</I> builds a list of all files in the directory and all subdirectories. Invocation of <I>DirList.main</I> is like the UNIX command <I>ls -al */* >foo</I> in that <I>DirList</I> opens each file, prints the size (in bytes), and stores the <I>File</I> instances, the total number of files visited, and the total number of bytes in each file. A class instance variable called <I>history</I> is of <I>Vector</I> type and holds instances of all files visited.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>WARNING: </B>The power of recursive file management is fraught with danger. Do not attempt to write recursive file deletes, as this is a serious time bomb.<HR></FONT>
</BLOCKQUOTE>
<!-- CODE //-->
<PRE>
package futils;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.lang.*;
public class DirList {
String startDir;
public Vector history = new Vector();
int totalBytes = 0;
int totalFiles = 0;
public static void main(String args[]) {
DirList dl = new DirList();
dl.printStats();
}
DirList() {
startDir = Ls.getDirName();
startAtThisDir(startDir);
}
public void printStats() {
System.out.println("Saw " +
totalFiles +
" files with a total size of " +
totalBytes +
" bytes");
}
//----------------------------
// Recursive function that given an anchor directory
// will walk directory tree
//
//----------------------------
public void startAtThisDir(String anchorDir) {
FileFilter files = new FileFilter();
DirFilter dirs = new DirFilter();
File f1 = new File(anchorDir);
File f2;
FileInputStream fis;
int i;
String[] ls;
int bytes;
System.out.println("Selected -> " + anchorDir);
System.out.println("Files in this Directory: ");
//----------------------------
// Loop through all the file names in the current directory
// and store them in history:
//----------------------------
for (ls = f1.list(files), i=0;
ls != null && i < ls.length; i++) {
totalFiles++;
f2 = new File(f1, ls[i]);
bytes = Futil.available(f2);
totalBytes += bytes;
System.out.println(f2 +
" has "+
bytes + " bytes");
history.addElement(f2);
}
//----------------------------
// This loop recurses on all directory names in
// the current working directory.
//----------------------------
for (ls = f1.list(dirs),
i=0; ls != null && i < ls.length; i++)
startAtThisDir(anchorDir + ls[i] + f1.separator);
}
}
</PRE>
<!-- END CODE //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>The use of the <I>Ls.getDirName</I> and <I>Futil.available</I> methods lets us eliminate the try-catch structure that usually characterizes Java I/O code. As a result, the code for the <I>DirList</I> class has fewer nested code blocks. Because exceptions are handled at a lower level, some of the complexity is hidden from the programmer and the code is easier to read.<HR></FONT>
</BLOCKQUOTE>
<H3><A NAME="Heading42"></A><FONT COLOR="#000077">The DataInputStream Class</FONT></H3>
<P>The <I>DataInputStream</I> class resides in the <I>java.io</I> package. An instance of the DataInputStream is a byte stream reader. <I>DataInputStream</I> instances providehigh-level methods that supply reading and casting services from a stream of bytes into various primitive data types. This class is useful when you’re attempting to decode binary files (such as the audio files in Chapter 5).</P>
<P><I>DataInputStream</I> is a subclass of <I>FilterInputStream</I> and implements the <I>DataInput</I> interface. When a read is performed and no bytes are available, the thread will block (cease to execute).</P>
<H4 ALIGN="LEFT"><A NAME="Heading43"></A><FONT COLOR="#000077">Class Summary</FONT></H4>
<!-- CODE //-->
<PRE>
public class DataInputStream extends FilterInputStream implements
DataInput
{
public DataInputStream(InputStream in)
public final int read(byte b[]) throws IOException
public final int read(byte b[], int off, int len) throws IOException
public final void readFully(byte b[]) throws IOException
public final void readFully(byte b[], int off, int len) throws
IOException
public final int skipBytes(int n) throws IOException
public final boolean readBoolean() throws IOException
public final byte readByte() throws IOException
public final int readUnsignedByte() throws IOException
public final short readShort() throws IOException
public final int readUnsignedShort() throws IOException
public final char readChar() throws IOException
public final int readInt() throws IOException
public final long readLong() throws IOException
public final float readFloat() throws IOException
public final double readDouble() throws IOException
public final String readLine() throws IOException
public final String readUTF() throws IOException
public final static String readUTF(DataInput in) throws IOException
}
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="188-192.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="195-199.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<hr width="90%" size="1" noshade><div align="center"><font face="Verdana,sans-serif" size="1">Copyright © <a href="/reference/idgbooks00001.html">IDG Books Worldwide, Inc.</a></font></div>
<!-- all of the reference materials (books) have the footer and subfoot reveresed --><!-- reference_subfoot = footer --><!-- reference_footer = subfoot --></BODY></HTML><!-- END FOOTER -->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -