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

📄 tij0111.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 3 页
字号:
            }
          });
    sort();
  }
  <font color="#0000ff">void</font> print() {
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; list.length; i++)
      System.out.println(list[i]);
  }
  <font color="#0000ff">private</font> <font color="#0000ff">void</font> sort() {
    StrSortVector sv = <font color="#0000ff">new</font> StrSortVector();
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; list.length; i++)
      sv.addElement(list[i]);
    <font color="#009900">// The first time an element is pulled from</font>
    <font color="#009900">// the StrSortVector the list is sorted:</font>
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; list.length; i++)
      list[i] = sv.elementAt(i);
  }
  <font color="#009900">// Test it:</font>
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
    SortedDirList sd;
    <font color="#0000ff">if</font>(args.length == 0)
      sd = <font color="#0000ff">new</font> SortedDirList(<font color="#0000ff">null</font>);
    <font color="#0000ff">else</font>
      sd = <font color="#0000ff">new</font> SortedDirList(args[0]);
    sd.print();
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
few other improvements have been made. Instead of creating 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>path</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>list</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
as local variables to 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
they are members of the class so their values can be accessible for the
lifetime of the object. In fact, 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is now just a way to test the class. You can see that the constructor of the
class automatically sorts the list once that list has been created.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
sort is case-insensitive so you don&#8217;t end up with a list of all the words
starting with capital letters, followed by the rest of the words starting with
all the lowercase letters. However, you&#8217;ll notice that within a group of
file names that begin with the same letter the capitalized words are listed
first, which is still not quite the desired behavior for the sort. This problem
will be fixed in Java 1.2<A NAME="Index1076"></A>.</FONT><a name="_Toc375545392"></a><a name="_Toc408018619"></a><P></DIV>
<A NAME="Heading315"></A><H3 ALIGN=LEFT>
Checking
for and creating directories
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>File</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class is more than just a representation for an existing directory path, file,
or group of files. You can also use a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>File</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object to create a new <A NAME="Index1077"></A><A NAME="Index1078"></A>directory
or an entire directory path if it doesn&#8217;t exist. You can also look at the <A NAME="Index1079"></A><A NAME="Index1080"></A>characteristics
of files (size, last modification date, read/write), see whether a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>File</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object represents a file or a directory, and delete a file. This program shows
the remaining methods available with the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>File</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: MakeDirectories.java</font>
<font color="#009900">// Demonstrates the use of the File class to</font>
<font color="#009900">// create directories and manipulate files.</font>
<font color="#0000ff">import</font> java.io.*;

<font color="#0000ff">public</font> <font color="#0000ff">class</font> MakeDirectories {
  <font color="#0000ff">private</font> <font color="#0000ff">final</font> <font color="#0000ff">static</font> String usage =
    "Usage:MakeDirectories path1 ...\n" +
    "Creates each path\n" +
    "Usage:MakeDirectories -d path1 ...\n" +
    "Deletes each path\n" +
    "Usage:MakeDirectories -r path1 path2\n" +
    "Renames from path1 to path2\n";
  <font color="#0000ff">private</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> usage() {
    System.err.println(usage);
    System.exit(1);
  }
  <font color="#0000ff">private</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> fileData(File f) {
    System.out.println(
      "Absolute path: " + f.getAbsolutePath() +
      "\n Can read: " + f.canRead() +
      "\n Can write: " + f.canWrite() +
      "\n getName: " + f.getName() +
      "\n getParent: " + f.getParent() +
      "\n getPath: " + f.getPath() +
      "\n length: " + f.length() +
      "\n lastModified: " + f.lastModified());
    <font color="#0000ff">if</font>(f.isFile())
      System.out.println("it's a file");
    <font color="#0000ff">else</font> <font color="#0000ff">if</font>(f.isDirectory())
      System.out.println("it's a directory");
  }
  <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 &lt; 1) usage();
    <font color="#0000ff">if</font>(args[0].equals("-r")) {
      <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("-d")) {
      count++;
      del = <font color="#0000ff">true</font>;
    }
    <font color="#0000ff">for</font>( ; count &lt; args.length; count++) {
      File f = <font color="#0000ff">new</font> File(args[count]);
      <font color="#0000ff">if</font>(f.exists()) {
        System.out.println(f + " exists");
        <font color="#0000ff">if</font>(del) {
          System.out.println("deleting..." + 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("created " + f);
        }
      }
      fileData(f);
    }  
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>fileData(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
you can see the various file investigation methods put to use to display
information about the file or directory path.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
first method that&#8217;s exercised by 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is <A NAME="Index1081"></A><A NAME="Index1082"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>renameTo(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which allows you to rename (or move) a file to an entirely new path represented
by the argument, which is another 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>File</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object. This also works with directories of any length.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">If
you experiment with the above program, you&#8217;ll find that you can make a
directory path of any complexity because <A NAME="Index1083"></A><A NAME="Index1084"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>mkdirs(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
will do all the work for you. In Java 1.0<A NAME="Index1085"></A>,
the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>-d</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
flag reports that the directory is deleted but it&#8217;s still there; in Java
1.1<A NAME="Index1086"></A>
the directory is actually deleted.
</FONT><a name="_Toc375545393"></a><a name="_Toc408018620"></a><P></DIV>

<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0110.html">Prev</a> | <a href="tij0112.html">Next</a>
</div>
</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -