⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 188-192.html

📁 dshfghfhhgsfgfghfhfghgfhfghfgh fg hfg hh ghghf hgf hghg gh fg hg hfg hfh f hg hgfh gkjh kjkh g yj f
💻 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=188-192//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="184-188.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="192-195.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading35"></A><FONT COLOR="#000077">Futil.makeTocHtml</FONT></H4>
<P>In this section we present a method for reading all the documents in a directory and creating a table of contents in HTML. The user is prompted for the input directory and location of the output file. It is beyond the scope of this book to explain HTML.
</P>
<P><I>Futil.makeTocHTML</I> uses <I>Futil.getDirFile</I> to create a standard file open dialog box that prompts the user for an HTML file.</P>
<!-- CODE SNIP //-->
<PRE>
1.     public static void makeTocHtml() {
2.   File dir = getDirFile();
3.   String[] files = dir.list(new FileFilter());
4.   System.out.println(files.length + " file(s):");
</PRE>
<!-- END CODE SNIP //-->
<P>In line 5, <I>Futil.getFileOutputStream</I> is used (with exception handling, handled at a lower level).</P>
<!-- CODE SNIP //-->
<PRE>
5.   FileOutputStream fos = getFileOutputStream();
6.   PrintStream ps = new PrintStream(fos);
7.   ps.println("&lt;HTML&gt;");
8.   ps.println("&lt;BODY&gt;");
9.   ps.println("&lt;ul&gt;");
</PRE>
<!-- END CODE SNIP //-->
<P>In lines 10 and 11, the file names are written as a part of the HTML. Note that file instances were never needed, because this is a simple text output program. The <I>href</I> is a hypertext reference that shows the file with a relative path name.</P>
<!-- CODE SNIP //-->
<PRE>
10.   for (int i=0; i &lt; files.length; i++)
11.      ps.println("&lt;LI&gt;&lt;a href = \"" + files[i]+"\"&gt;"+
12.           files[i]+"&lt;/a&gt;&lt;P&gt;");
13.   ps.println("&lt;/ul&gt;");
14.   ps.println("&lt;/BODY&gt;");
15.   ps.println("&lt;/HTML&gt;");
16.   closeOutputStream(fos);
17.     }
</PRE>
<!-- END CODE SNIP //-->
<P>The browser output of <I>makeTocHtml</I> (after being run on the <I>futils</I> package) looks like this:</P>
<!-- CODE SNIP //-->
<PRE>
<U>Cat.java
DirFilter.java
FileFilter.java
Find.java
Futil.java
Ls.java
WildFilter.java</U>
</PRE>
<!-- END CODE SNIP //-->
<P>When the user clicks on any of the underlined links, a plain-text file of the source code is shown.
</P>
<H3><A NAME="Heading36"></A><FONT COLOR="#000077">The FileInputStream Class</FONT></H3>
<P>The <I>FileInputStream</I> class resides in the <I>java.io</I> package. It can be constructed from a file name or <I>File</I> instance. <I>FileInputStream</I> is a subclass of the <I>InputStream</I> class and keeps a private copy of the <I>FileDescriptor</I> reference.</P>
<P>The read methods always block if there are no more bytes to read. When a read method blocks, it stops execution of the thread. This indicates that I/O bound tasks should probably be placed in their own threads. If a file will open quickly, on the other hand, placing the file I/O in its own thread may needlessly complicate the program. Thus, the decision to use threaded I/O depends on the application.</P>
<H4 ALIGN="LEFT"><A NAME="Heading37"></A><FONT COLOR="#000077">Class Summary</FONT></H4>
<!-- CODE //-->
<PRE>
public class FileInputStream extends InputStream {
  public FileInputStream(String name) throws FileNotFoundException
  public FileInputStream(File file) throws FileNotFoundException
  public FileInputStream(FileDescriptor fdObj)
  public int read() throws IOException;
  public int read(byte b[]) throws IOException
  public int read(byte b[], int off, int len) throws IOException
  public long skip(long n) throws IOException
  public int available() throws IOException
  public void close() throws IOException
  public final FileDescriptor getFD() throws IOException
}
</PRE>
<!-- END CODE //-->
<H4 ALIGN="LEFT"><A NAME="Heading38"></A><FONT COLOR="#000077">Class Usage</FONT></H4>
<P>Suppose the following variables are predefined:
</P>
<!-- CODE SNIP //-->
<PRE>
String fileName;
FileInputStream fis;
File file;
FileDescriptor fd;
byte b bytes[];
int length, offset;
int amountRead;
int n, numberSkipped;
</PRE>
<!-- END CODE SNIP //-->
<P>To make an instance of the <I>FileInputStream</I> class from a file name, <I>File</I> instance, or <I>FileDescriptor</I> instance, use this code:</P>
<!-- CODE SNIP //-->
<PRE>
fis = new FileInputStream(fileName);
fis = new FileInputStream(file);
fis = new FileInputStream(fd);
</PRE>
<!-- END CODE SNIP //-->
<P>To read a byte of data (-1 returned at end of stream):
</P>
<!-- CODE SNIP //-->
<PRE>
b = fis.read();
</PRE>
<!-- END CODE SNIP //-->
<P>To read an array of bytes (-1 returned at end of stream):
</P>
<!-- CODE SNIP //-->
<PRE>
amountRead = fis.read(bytes);
</PRE>
<!-- END CODE SNIP //-->
<P>To read a subarray of bytes:
</P>
<!-- CODE SNIP //-->
<PRE>
amountRead = fis.read(bytes, offset, length);
</PRE>
<!-- END CODE SNIP //-->
<P>To skip n bytes:
</P>
<!-- CODE SNIP //-->
<PRE>
numberSkipped = fis.skip(n);
</PRE>
<!-- END CODE SNIP //-->
<P>To find out how many bytes can be read (used to test the length of the file):
</P>
<!-- CODE SNIP //-->
<PRE>
n = fis.available();
</PRE>
<!-- END CODE SNIP //-->
<P>To close the stream:
</P>
<!-- CODE SNIP //-->
<PRE>
fis.close();
</PRE>
<!-- END CODE SNIP //-->
<P>To get the file descriptor:
</P>
<!-- CODE SNIP //-->
<PRE>
fd =  fis.getFD();
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading39"></A><FONT COLOR="#000077">Futil.getFileInputStream</FONT></H4>
<P>To localize the exception handling within the <I>Futil</I> class, a static public method has been devised called <I>Futil.getFileInputStream</I>. <I>getFileInputStream</I> is overloaded to take either an absolute path name or no argument at all. When no argument is passed, a standard file dialog box is presented to the user for file selection. After a valid file is selected, <I>getFileInputStream</I> invokes the overloaded version of itself that takes a string. Should the user cancel the dialog box selection, an exception prints an error message and program execution continues.</P>
<!-- CODE //-->
<PRE>
public static FileInputStream getFileInputStream() {
         return  getFileInputStream(getReadFileName());
    }

public static FileInputStream getFileInputStream(String name) {
 FileInputStream fis = null;
    try
         {fis =  new FileInputStream(name);}
    catch (IOException e)
         {System.out.println("futil:Could not open file");}
         return fis;
    }
</PRE>
<!-- END CODE //-->
<P>Recall that <I>FileInputStream</I> also has a constructor that works with a <I>File</I> instance:</P>
<!-- CODE SNIP //-->
<PRE>
public static FileInputStream getFileInputStream(File file) {
 FileInputStream fis = null;
    try
         {fis =  new FileInputStream(file);}
    catch (IOException e)
         {System.out.println("futil:Could not open file");}
         return fis;
    }
</PRE>
<!-- END CODE SNIP //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="184-188.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="192-195.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>

<hr width="90%" size="1" noshade><div align="center"><font face="Verdana,sans-serif" size="1">Copyright &copy; <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 + -