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

📄 178-180.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=178-180//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="172-177.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="181-184.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading13"></A><FONT COLOR="#000077">Ls.getDirName</FONT></H4>
<P>UNIX-type operating systems have a command called <I>ls</I>. We have modeled this command in the <I>futils</I> package using a class called <I>Ls</I>. It contains static methods and is declared as public and final. <I>Ls.getDirName</I> opens a standard file dialog box and asks the user to select a file (there is no way to select a directory as far as we know). Once the file is selected, the name of the directory that contains the file is returned:</P>
<!-- CODE SNIP //-->
<PRE>
   static public String getDirName() {
           FileDialog fileDialog =
                    new FileDialog(new Frame(), "select file");
           fileDialog.show();
           String dirName = fileDialog.getDirectory();
           dialog.dispose();
           return dirName;
   }
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading14"></A><FONT COLOR="#000077">Ls.deleteFile</FONT></H4>
<P>The <I>Ls.deleteFile</I> method takes an absolute path name (a string), converts it to a <I>File</I> instance, and then deletes the file, with error reporting to the console.</P>
<!-- CODE //-->
<PRE>
   static public void deleteFile(String absPath) {
        File fileToDelete = new File(absPath);
        System.out.print("deleting file " + absPath);
        if (fileToDelete.exists()) {
              System.out.println(" deleted!");
              fileToDelete.delete();
              }
         else
              System.out.println(" does not exist");
   }
</PRE>
<!-- END CODE //-->
<H4 ALIGN="LEFT"><A NAME="Heading15"></A><FONT COLOR="#000077">Futil.getReadFile, Futil.getWriteFile, and Futil.getDirFile</FONT></H4>
<P>The static method <I>Futil.getReadFile</I> opens a dialog box for the user and returns a <I>File</I> instance, whereas <I>Futil.getDirFile</I> works by getting a file based on the <I>Ls.getDirName</I> method:</P>
<!-- CODE //-->
<PRE>
public  static  File getReadFile() {
                return new File(getReadFileName());
           }
public   static  File getWriteFile() {
                return File(getWriteFileName(fs));
   }
 public   static  File getDirFile() {
                return new File(Ls.getDirName());
    }
</PRE>
<!-- END CODE //-->
<P>An example of the File Open dialog box created by getWriteFill is shown in Figure 4.2.
</P>
<H3><A NAME="Heading16"></A><FONT COLOR="#000077">The FilenameFilter Interface</FONT></H3>
<P><I>FilenameFilter</I> is an interface reference data type that requires that an <I>accept</I> method be implemented. Once the <I>FilenameFilter</I> instance is created, it can be passed to utilities that will determine whether a file name should be allowed on a list.</P>
<H4 ALIGN="LEFT"><A NAME="Heading17"></A><FONT COLOR="#000077">Class Summary</FONT></H4>
<!-- CODE SNIP //-->
<PRE>
public interface FilenameFilter
    boolean accept(File dir, String name);
}
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading18"></A><FONT COLOR="#000077">Class Usage</FONT></H4>
<P>The best way to illustrate the usage of <I>FilenameFilter</I> is to show some examples. All the examples are taken from the <I>futils</I> package.</P>
<P>Typically, a more efficient approach is to keep all file names in <I>File</I> instances. There are reasons, however, for maintaining lists of files as arrays of <I>String</I> instances. This approach is particularly efficient when you&#146;re attempting to trade space for time. For example, the storage of a <I>String</I> instance is based on a dictionary that assumes the <I>String</I> is immutable, thereby permitting the reuse of substrings. Therefore, redundancy in absolute path name storage should not be very memory inefficient, because the <I>String</I> storage facility has been highly optimized. By emphasizing the storage of absolute path names in <I>String</I> instances, we have gained space efficiency. However, having to create and dispose of many <I>File</I> instances creates CPU inefficiency. Questions remain concerning the quantitative aspects of this trade-off.</P>
<H4 ALIGN="LEFT"><A NAME="Heading19"></A><FONT COLOR="#000077">DirFilter</FONT></H4>
<P><I>DirFilter</I> takes a file name and returns true if the file is a directory. It first makes an instance of the <I>File</I> class, using the file name, and then targets the instance with an <I>isDirectory</I> invocation.</P>
<!-- CODE SNIP //-->
<PRE>
package futils;
import java.io.*;
import java.util.*;
public class DirFilter implements FilenameFilter {
     public boolean accept(File dir, String name) {
          return new File(dir, name).isDirectory();
     }
}
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading20"></A><FONT COLOR="#000077">The FileFilter Class</FONT></H4>
<P>The <I>FileFilter</I> class, like the <I>DirFilter</I> Class, creates a temporary <I>File</I> instance to determine whether the dir&#43;name constitutes a valid file.</P>
<!-- CODE SNIP //-->
<PRE>
package futils;
import java.io.*;
import java.util.*;
public class FileFilter implements FilenameFilter {
     public boolean accept(File dir, String name) {
          return new File(dir, name).isFile();
        }
}
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading21"></A><FONT COLOR="#000077">The WildFilter Class</FONT></H4>
<P>The <I>WildFilter</I> class contains the suffix string of the file name string that the programmer is looking for. Once the <I>WildFilter</I> is instantiated, the suffix is unchangeable (because of the private visibility of the suffix class variable).</P>
<!-- CODE //-->
<PRE>
package futils;
import java.io.*;
import java.util.*;
public class WildFilter implements FilenameFilter {
      private String suffix;
      WildFilter (String suffix_) {
           suffix = suffix_;
      }
public boolean accept(File dir, String name) {
return name.endsWith(suffix);
     }
 }
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="172-177.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="181-184.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 + -