Although most classes defined by Java.io are used for manipulating flows, the file class is not. The file class processes files and file systems directly. That is, the file class does not specify how to retrieve information from a file and how to store it in a file, but rather describes the properties of the files themselves. The file object is used to get and manipulate information associated with disk files, such as permissions, time dates, and directory paths, and you can also browse the subdirectory hierarchy.
Note: The path interface and the file class that are added by JDK7 to the NIO system, in many cases, provide a powerful alternative to the file class, depending on the NIO section that follows.
The following constructor can be used to create a file object:
File (string directorypath) file (string directorypath,string filename) file (file dirobj,string filename) file (URI uriobj )
Where DirectoryPath is the pathname of the file, filename is the name of the file or subdirectory, DirObj is the document object for the specified directory, and Uriobj is the URI object that describes the file.
Note: Java uses a path delimiter between UNIX and Windows Conventions. If you use a forward slash (/) in the Windows version of Java, the path will still be parsed correctly. Keep in mind that if you use the Windows Convention backslash (\), you need to use an escape sequence (\ \) in the string.
Let's look at an example of a file invocation:
Package io;import java.io.file;public class filedemo { public static void main (String[] args) { File f1 = new file ("/home/fuhd/apache/apr-1.5.1.tar.gz"); //returns the file name system.out.println ("File name:" + f1.getname ()); //return to file path system.out.println ("Path:" + f1.getpath ()); //returns the absolute path system.out.println ("Abs Path:" + f1.getabsolutepath ()); //returns the parent directory path system.out.println ("Parent:" + f1.getparent ()); Is there system.out.println (f1.exists () ? "exists" : "Does not exist"); //have permission to write system.out.println (F1.canwrite () ? "is writeable"  : "is not writeable"); //have permission to read system.out.println (F1.canread () ? "is readable" : "is not readable "); //is file system.out.println (F1.isfile () ? "Is normal file" : " Might be a maned pipe ") //whether there is an absolute path system.out.println (F1.isabsolute () ? "Is absolute" : "IS NOT ABSOlute "); //file last modified on system.out.println ("File last modified:" + f1.lastmodified ()); The //file size system.out.println ("file size: " + f1.length () + " bytes "); }}
The file class also provides two useful practical methods. The first method is Renameto (), as follows:
Boolean Renameto (File newName)
where the filename specified by newname becomes the new name of the calling file object. Returns true if the operation succeeds and returns False if the file cannot be re-ordered. The second practical method is Delete (), which removes the disk file represented by the path that invokes the file object. The method is as follows:
Boolean Delete ()
If the directory is empty, you can use the Delete () method to delete the directory. If the deletion succeeds, the delete () method returns True, or False if the file cannot be deleted.
JDK7 adds a method Topath () to the file class, as follows:
Path Topath ()
The path object returned by the Topath () method represents the file that is encapsulated by the calling file object (in other words, the Topath () method can convert file objects to path objects). Path is a newly added interface by JDK7. The interface is packaged into the Java.nio.file package and is part of the NIO. Therefore, the Topath () method builds a bridge between the old file class and the new path interface. (Of course, the previous method is not all of the file class, this is just a partial introduction)
Directory
The
Directory is a file object that contains a series of other files and directories. The Isdirectory () method returns True when a file object is created for the directory. In this case, you can call the list () method on the file object to fetch a list of other files and directories inside. As an example:
Package io;import java.io.file;public class dirlist { public static void main (String[] args) { string dir = "/home/fuhd/apache"; file file = new file (dir); if (File.isDirectory ()) { string[] filelist = file.list (); if (filelist != null && filelist.length > 0) { for (string f1 : filelist) { system.out.println (" File name: " + f1); } } } }}
Using the FilenameFilter interface
Explore the Java IO file class