The file class of Java

Source: Internet
Author: User

1.File class overview

The file class is a java.io package that represents platform-independent files and directories. File can create new, delete, rename the box directory, but file cannot access the content itself. If you want to access the contents of the file itself, you need to use the input, output stream.

2. Accessing Files and directories

The file class can use a path string to create a document instance, either an absolute path or a relative path. By default, the system always interprets relative paths based on the user's working path, which is specified by the system property "User.dir".

Import Java.io.file;import Java.io.ioexception;public class Filetest {public static void main (string[] args) {String path = "Src/com/t/file.txt"; File File=new file (path);//Create a file instance file Dir=new file ("src");//Create a new file instance/* * Access the method associated with the file name or directory name */file.getna Me ();//Gets the file name File.getpath ();//Gets the relative path name File.getabsolutepath ();//Gets the Absolute pathname file.getparent ();//Gets the parent pathname (that is, the directory in which the file or directory resides) File.renameto (New file ("Src/com/t/lavor_zl.txt"));//Rename the file or directory corresponding to this files object, or return the false/* * file or directory detection method if the rename succeeds, The return type is Boolean, if true, if False returns False */file.exists (),//Determines whether the file or directory corresponding to the files object exists file.canwrite ();// Determines whether the file or directory corresponding to the files object can be written file.canread ();//Determines whether the file or directory corresponding to the files object is readable File.canexecute ();// Determines whether the file or directory corresponding to the files object can execute File.isfile ();//Determines whether the object corresponding to the file object is File.isdirectory ();// Determines whether the object corresponding to the file object is a directory File.isabsolute ();//Determines whether the files or directories corresponding to the Document object are absolute paths/* * Get regular file or directory information */file.lastmodified ();// Returns the file or directory last modified time file.length ();//Returns the length of the contents of a file or directory */* file and directory operations related methods */try {file.createnewfile (); Creates a new file specified by the file object, returns True if the creation succeeds, or false} catch (Ioexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Dir.mkdir ();//try to create a directory corresponding to the file object, or return the falsedir.list () if the creation succeeds, or list all the sub-filenames and path names of the file object. Returns a string array dir.listfiles ();//Lists all the child files and paths of the file object, returns the file array File.deleteonexit ();//Registers a delete hook that specifies when the Java virtual machine exits, Delete files or directories corresponding to file objects file.delete ();//delete files or directories corresponding to file objects}}

3. File Filteryou can accept a filenamefilter parameter in the Lsit () method of the file class to list only files that match the criteria.
The FilenameFilter interface contains a accep (file Dir,string name) method that iterates through all subdirectories or files of the specified file, and if the method returns True, the list () method lists the subdirectory or file.

Import Java.io.file;import Java.io.filenamefilter;public class Filenamefiltertest {public static void main (string[] args) {file dir=new file ("src");//Create an instance of a directory System.out.println (Dir.getabsolutepath ()); String[] Namelist=dir.list (new FilenameFilter () {@Overridepublic Boolean accept (File dir, String name) {return Name.endswith (". Java");//If the file name ends with. Java, returns True}}); Try{for (String name:namelist) {System.out.println (name);}} catch (NullPointerException e) {e.printstacktrace ();}}}

the path delimiter for the ps:windows is a backslash (\), and the backslash (\) represents the transfer character in the Java program, so use a double backslash (\ \) When using the path delimiter under Windows, or use the Java Platform-independent path divider: slash (/).

The file class of Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.