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

📄 172-177.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=172-177//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="169-172.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="178-180.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading8"></A><FONT COLOR="#000077">Futil.getReadFileName</FONT></H4>
<P><I>Futil.getReadFileName</I> is a static method that creates a file open dialog box, which is used to select a file. Once the file is selected, <I>Futil.getReadFileName</I> returns an absolute path name to the file as a <I>String</I> instance. An example of the standard file open dialog box is shown in Figure 4.1.</P>
<P><A NAME="Fig1"></A><A HREF="javascript:displayWindow('images/04-01.jpg',358,180 )"><IMG SRC="images/04-01t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/04-01.jpg',358,180)"><FONT COLOR="#000077"><B>Figure 4.1</B></FONT></A>&nbsp;&nbsp;The standard file open dialog box.</P>
<P>The appearance of the file open dialog box will be platform-dependent.
</P>
<!-- CODE //-->
<PRE>
public static  String getReadFileName() {
  FileDialog dialog = new FileDialog(new Frame(), "select file");
  dialog.show();
  String file_name = dialog.getFile();
  String path_name = dialog.getDirectory();
  String file_string = path_name + file_name;
  System.out.println("Opening file: "+file_string);
  dialog.dispose();
  return file_string;
}
</PRE>
<!-- END CODE //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE:&nbsp;&nbsp;</B>The parent field of the dialog box is set to <I>new Frame()</I> so that the user does not need to pass a <I>Frame</I> instance into the <I>getReadFileName</I> parameter list. This technique simplifies the <I>getReadFileName</I> call at the expense of efficiency. The path name for the file is explicitly concatenated into the <I>file_string</I> before the return.<HR></FONT>
</BLOCKQUOTE>
<P>Typically, a programmer would prompt the user to select a file by using this statement:
</P>
<!-- CODE SNIP //-->
<PRE>
String inputFileName = Futil.getReadFileName();
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading9"></A><FONT COLOR="#000077">Futil.getWriteFileName</FONT></H4>
<P>To get a file name to write, the mode of <I>FileDialog</I> must be set to <I>FileDialog.SAVE</I>. A sample of the save file dialog box is shown in Figure 4.2.</P>
<P><A NAME="Fig2"></A><A HREF="javascript:displayWindow('images/04-02.jpg',355,202 )"><IMG SRC="images/04-02t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/04-02.jpg',355,202)"><FONT COLOR="#000077"><B>Figure 4.2</B></FONT></A>&nbsp;&nbsp;Save file Dialog Box.</P>
<P>This mode is set in the following code:
</P>
<!-- CODE //-->
<PRE>
 public   static  String getWriteFileName() {
       FileDialog dialog = new FileDialog(new Frame(),
      "Enter file name",FileDialog.SAVE);
      dialog.show();
      String fs = dialog.getDirectory() + dialog.getFile();
      System.out.println("Opening file: "+fs);
      dialog.dispose();
      return fs;
   }
</PRE>
<!-- END CODE //-->
<P>To ask the user for a file string with a fully qualified path name:
</P>
<!-- CODE SNIP //-->
<PRE>
String writeFile = Futil.getWriteFileName();
</PRE>
<!-- END CODE SNIP //-->
<H3><A NAME="Heading10"></A><FONT COLOR="#000077">The File Class</FONT></H3>
<P>The <I>File</I> class resides in the <I>java.io</I> package. An instance of the <I>File</I> class keeps track of several file-related properties, including a path to the file, information about whether the path is relative or absolute, and a series of static constants that indicate the path separator. Unfortunately, on some systems (DOS, Windows, and so on) the path name separator is represented by a backslash (\), whereas on other systems (Macintosh and UNIX variants) the path name separator is represented by a forward slash (/).</P>
<P>The <I>File</I> class uses a <I>FilenameFilter</I> instance, which is introduced later in the chapter.</P>
<H4 ALIGN="LEFT"><A NAME="Heading11"></A><FONT COLOR="#000077">Class Summary</FONT></H4>
<!-- CODE //-->
<PRE>
public class File  {
   public static final String separator
   public static final char separatorChar
   public static final String pathSeparator
   public static final char pathSeparatorChar
   public File(String path)
   public File(String path, String name)
   public File(File dir, String name)
   public String getName()
   public String getPath()
   public String getAbsolutePath()
   public String getParent()
   public boolean exists()
   public boolean canWrite()
   public boolean canRead()
   public boolean isFile()
   public boolean isDirectory()
   public native boolean isAbsolute();
   public long lastModified()
   public long length()
   public boolean mkdir()
   public boolean renameTo(File dest)
   public boolean mkdirs()
   public String[] list()
   public String[] list(FilenameFilter filter)
   public boolean delete()
   public int hashCode()
   public boolean equals(Object obj)
   public String toString()
}
</PRE>
<!-- END CODE //-->
<H4 ALIGN="LEFT"><A NAME="Heading12"></A><FONT COLOR="#000077">Class Usage</FONT></H4>
<P>Suppose the following variables are predefined:
</P>
<!-- CODE //-->
<PRE>
String absPath = Futil.getReadFileName();
// The fileName is relative and
// does not include absPath
String fileName;
String dirName; // absolute path to directory
File dirFile; // dir File instance
File destFile; // a destination file
boolean aboolean, successful;
int i;
long timeInMilliseconds; // relative time.
long bytes; // size of the file
String path; // relative or absolute
String fileNames[];
FileNameFilter filter;
</PRE>
<!-- END CODE //-->
<P>To make an instance of the File class, use this code:
</P>
<!-- CODE SNIP //-->
<PRE>
file = new File(absPath);
file = new File(dirName,fileName);
file = new File(dirFile,fileName);
fileName = file.getName();
</PRE>
<!-- END CODE SNIP //-->
<P>To get the path to the file:
</P>
<!-- CODE SNIP //-->
<PRE>
path = file.getPath();
</PRE>
<!-- END CODE SNIP //-->
<P>To get the absolute path to the file:
</P>
<!-- CODE SNIP //-->
<PRE>
absPath = file.getAbsolutePath();
</PRE>
<!-- END CODE SNIP //-->
<P>To get the name of the parent directory:
</P>
<!-- CODE SNIP //-->
<PRE>
dirName = file.getParent();
</PRE>
<!-- END CODE SNIP //-->
<P>To see whether a file exists:
</P>
<!-- CODE SNIP //-->
<PRE>
aboolean = file.exists();
</PRE>
<!-- END CODE SNIP //-->
<P>To see whether a file is writable:
</P>
<!-- CODE SNIP //-->
<PRE>
aboolean = file.canWrite();
</PRE>
<!-- END CODE SNIP //-->
<P>To see whether a file is readable:
</P>
<!-- CODE SNIP //-->
<PRE>
aboolean = file.canRead();
</PRE>
<!-- END CODE SNIP //-->
<P>To see whether a <I>File</I> instance is a file or a directory:</P>
<!-- CODE SNIP //-->
<PRE>
aboolean = file.isFile();
aboolean = file.isDirectory();
</PRE>
<!-- END CODE SNIP //-->
<P>To see whether <I>getPath</I> is relative:</P>
<!-- CODE SNIP //-->
<PRE>
aboolean = file.isAbsolute();
</PRE>
<!-- END CODE SNIP //-->
<P>To get the relative time since modification:
</P>
<!-- CODE SNIP //-->
<PRE>
timeInMilliseconds = file.lastModified();
</PRE>
<!-- END CODE SNIP //-->
<P>To get the size of the file in bytes:
</P>
<!-- CODE SNIP //-->
<PRE>
bytes = file.length();
</PRE>
<!-- END CODE SNIP //-->
<P>To invoke <I>mkdir</I> and return true if successful:</P>
<!-- CODE SNIP //-->
<PRE>
successful = file.mkdir();
</PRE>
<!-- END CODE SNIP //-->
<P>To rename to a destination file:
</P>
<!-- CODE SNIP //-->
<PRE>
successful = file.renameTo(destFile);
</PRE>
<!-- END CODE SNIP //-->
<P>To make all directories in this path, including the filename itself:
</P>
<!-- CODE SNIP //-->
<PRE>
successful = file.mkdirs();
</PRE>
<!-- END CODE SNIP //-->
<P>To list the files in a directory:
</P>
<!-- CODE SNIP //-->
<PRE>
fileNames = file.list();
</PRE>
<!-- END CODE SNIP //-->
<P>To use a filter to list the files in a directory:
</P>
<!-- CODE SNIP //-->
<PRE>
fileNames = file.list(filter);
</PRE>
<!-- END CODE SNIP //-->
<P>To delete a file:
</P>
<!-- CODE SNIP //-->
<PRE>
success = file.delete();
</PRE>
<!-- END CODE SNIP //-->
<P>To compute a <I>hashcode</I>:</P>
<!-- CODE SNIP //-->
<PRE>
i = file.hashCode();
</PRE>
<!-- END CODE SNIP //-->
<P>To see whether two files are equal:
</P>
<!-- CODE SNIP //-->
<PRE>
aboolean = file.equals(destFile);
</PRE>
<!-- END CODE SNIP //-->
<P>To get the <I>path</I> string:</P>
<!-- CODE SNIP //-->
<PRE>
path = file.toString();
</PRE>
<!-- END CODE SNIP //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="169-172.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="178-180.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 + -