java.io.File 根据给定路径名创建一个新File对象。这里的路 +File(pathname:String) 径名可以是一个目录,也可以是一个文件。 +File(parent:String,child:String) 为目录parent下的child创建一个File对象。这个child 可以是一个文件名,也可以是一个子目录。 +File(parent:File,child:String) +exists():boolean- 测试此路径名表示的文件或目录是否存在。存在时, +canRead():boolean 返回true;否则返回false。 +canWrite():boolean Returns true if the file represented by the File object exists and can be written. +isDirectory():boolean Returns true if the File object represents a directory. +isFileO:boolean Returns true if the File object represents a file. +isAbsolute():boolean Retums true if the File object is created using an absolute path name. +isHidden():boolean Returns true if the file represented in the File object is hidden.The exact definition of hidden is system-dependent.On Windows,you can mark a file hidden in the File Properties dialog box.On Unix systems,a file is hidden if its name begins with a period character'.'. +getAbsolutePath():String Returns the complete absolute file or directory name represented by the File object. +getCanonicalPath():String Returns the same as getAbsolutePath()except that it removes redundant 返回文件名。如:new +getName():String File(“c.:llbooklltest.dat.getName()返▣test.dat 返回文件的完整路径名和文件名。如:new +getPath():String File(“c:lbooklltest.dat).getPath(0返▣c:\bookltest.dat +getParent():String 返回文件的上级目录。如:new File("c:\\booklltest.dat").getParent()c:\book +lastModified():long Returns the time that the file was last modified. +delete():boolean Deletes this file.The method returns true if the deletion succeeds. +renameTo(dest:File):boolean Renames this file.The method returns true if the operation succeeds
8 java.io.File +File(pathname: String) +File(parent: String, child: String) +File(parent: File, child: String) +exists(): boolean +canRead(): boolean +canWrite(): boolean +isDirectory(): boolean +isFile(): boolean +isAbsolute(): boolean +isHidden(): boolean +getAbsolutePath(): String +getCanonicalPath(): String +getName(): String +getPath(): String +getParent(): String +lastModified(): long +delete(): boolean +renameTo(dest: File): boolean Creates a File object for the specified pathname. The pathname may be a directory or a file. Creates a File object for the child under the directory parent. child may be a filename or a subdirectory. Creates a File object for the child under the directory parent. parent is a File object. In the preceding constructor, the parent is a string. Returns true if the file or the directory represented by the File object exists. Returns true if the file represented by the File object exists and can be read. Returns true if the file represented by the File object exists and can be written. Returns true if the File object represents a directory. Returns true if the File object represents a file. Returns true if the File object is created using an absolute path name. Returns true if the file represented in the File object is hidden. The exact definition of hidden is system-dependent. On Windows, you can mark a file hidden in the File Properties dialog box. On Unix systems, a file is hidden if its name begins with a period character '.'. Returns the complete absolute file or directory name represented by the File object. Returns the same as getAbsolutePath() except that it removes redundant names, such as "." and ".", from the pathname, resolves symbolic links (on Unix platforms), and converts drive letters to standard uppercase (on Win32 platforms). Returns the last name of the complete directory and file name represented by the File object. For example, new File("c:\\book\\test.dat").getName() returns test.dat. Returns the complete directory and file name represented by the File object. For example, new File("c:\\book\\test.dat").getPath() returns c:\book\test.dat. Returns the complete parent directory of the current directory or the file represented by the File object. For example, new File("c:\\book\\test.dat").getParent() returns c:\book. Returns the time that the file was last modified. Deletes this file. The method returns true if the deletion succeeds. Renames this file. The method returns true if the operation succeeds. 根据给定路径名创建一个新 File对象。这里的路 径名可以是一个目录,也可以是一个文件。 测试此路径名表示的文件或目录是否存在。存在时, 返回 true;否则返回 false。 为目录parent下的child创建一个File对象。这个child 可以是一个文件名,也可以是一个子目录。 返回文件名。如:new File(“c:\\book\\test.dat”).getName()返回test.dat 返回文件的完整路径名和文件名。如:new File(“c:\\book\\test.dat”).getPath()返回c:\book\test.dat 返回文件的上级目录。如:new File(“c:\\book\\test.dat”).getParent()返回c:\book
DTestFileClass.java 1 import java.io.File; 2 public class TestFileclass 4 public static void main(String[]args)( 5 File file1 new File("d:\\a.mp3"); 6 File file2 new File("image/a.txt"); System.out.println("Is filel absolute?"file1.isAbsolute()); 8 System.out.println("Is file2 absolute?"file2.isAbsolute()); 9 10 11 曰Console☒ <terminated>TestFileClass [Java Applicat: Is file1 absolute?true Is file2 absolute?false
JTestFileClass.java 1 import java.io.File; 2 3 public class TestFileClass length(0方法返回文件的大小,如 % public static void main(String[]args)( 果文件不存在返回0。 5 Filefile new File("d:\\a.mp3"); 6 System.out.println("Does it exist?"file.exasts()); 7 y3tem.out.printin("The file has3"+file.length()+"bytes"):l/返回文件打下,不存在返回o 8 System.out.println("Can it be read?"file.canRead()); 9 System.out.println("Can it be written?"file.canWrite()); 10 System.out.println("Is it a directory?"file.isDirectory()); 11 System.out.println("Is it a file?"+file.isFile()); System.out.println("Is it absolute?"file.isAbsolute()) 13 System.out.println("Is it hidden?"file.isHidden()) 14 System.out.println("Absolute path is "file.getAbsolutePath()); 15 System.out.println("Last modified on 16 +new java.util.Date(file.lastModified() 旦Console3 18 <terminated>TestFileClass [Java Application]C:\Program Files\Genuitec\C 19 Doe3 it exis3t?true The file has 3894871 bytes Can it be read?true Can it be written?true Is it a directory?false I31taf11e?true Is it absolute?true Is it hidden?false Absolute path is d:\a.mp3 Last modified on Sun Apr 01 06:32:30 GMT 2012
length()方法返回文件的大小,如 果文件不存在返回0
7.1文件类File 如果文件夹或文件不存在会创建它 创建一个File类的对象file。 们吗? File file new File("c:\Ibook"); 既不创建文件也不创建文件夹 File file new File("c:\lbooklla.txt"), 可以用exists(0方法判断这个文件是 否存在
File file = new File(“c:\\book"); 创建一个File类的对象file。 既不创建文件也不创建文件夹 File file = new File(“c:\\book\\a.txt"); 如果文件夹或文件不存在会创建它 们吗? 可以用exists()方法判断这个文件是 否存在 7.1 文件类File
JTestFile.jawa☒ 1 import java.io.File; 2 3 public class TestFile( 4e public static void main(String[]args) File file1 new File("c:\\book"); 6 File file2 new File("c:\\book\\a.txt"); 7 System.out.println("Does file1 exist?"file1.exists()); 8 System.out.println("Does file2 exist?"file2.exists()); 9 10 11 貝Console <terminated>TestFile [Java Application]C: Does file1 exist?false Does file2 exist?false