【正文】
this is indicated by the boolean result returned by accept( ). To make sure the element you’re working with is only the file name and contains no path information, all you have to do is take the String object and create a File object out of it, then call getName( ), which strips away all the path information (in a platformindependent way). Then accept( ) uses a regular expression matcher object to see if the regular expression regex matches the name of the file. Using accept( ), the list( ) method returns an array. Note that the argument to filter( ) must be final. This is required by the anonymous inner class so that it can use an object from outside its scope. This design is an improvement because the FilenameFilter class is now tightly bound to DirList2. However, you can take this approach one step further and define the anonymous inner class as an argument to list( ), in which case it’s even smaller: The argument to main( ) is now final, since the anonymous inner class uses args[0] directly. This shows you how anonymous inner classes allow the creation of specific, oneoff classes to solve problems. One benefit of this approach is that it keeps the code that solves a particular problem isolated together in one spot. On the other hand, it is not always as easy to read, so you must use it judiciously. The File class is more than just a representation for an existing file or directory. You can also use a File object to create a new directory or an entire directory path if it doesn’t exist. You can also look at the characteristics of files (size, last modification date, read/write), see whether a File object represents a file or a directory, and delete a file. This program shows some of the other methods available with the File class (see the HTML documentation from for the full set): In fileData( ) you can see various file investigation methods used to display information about the file or directory first method that’s exercised by main( ) is renameTo( ), which allows you to rename (or move) a file to an entirely new path represented by the argument, which is another 本科畢業(yè)設(shè)計(論文)外文翻譯 6 File object. This also works with directories of any length. If you experiment with the preceding program, you’ll find that you can make a directory path of any plexity, because mkdirs( ) will do all the work for you. I/O libraries often use the abstraction of a stream, which represents any data source or sink as an object capable of producing or receiving pieces of data. The stream hides the details of what happens to the data inside the actual I/O device. The Java library classes for I/O are divided by input and output, as you can see by looking at the class hierarchy in the JDK documentation. By inheritance, everything derived from the InputStream or Reader classes have basic methods called read( ) for reading a single byte or array of bytes. Likewise, everything derived from OutputStream or Writer classes have basic methods called write( ) for writing a single byte or array of bytes. However, you won’t generally use these methods。 不論你用哪種 I/O 設(shè)備,輸入的時候,最好都做緩沖。 (所有的方法都以 read打頭,比如 readByte( ), readFloat( ))。這兩個名字都起得不怎么樣。 Java 的 I/O 類庫需要提供很多功能的組合,于是decorator 模式就有了用武之地。這使得