File class, javafile class

Source: Internet
Author: User

File class, javafile class

File class: the only operation class related to the File itself.

The following three constructor methods can be used to generate File objects.

File (String directoryPath );

File (String directoryPath, String fileName );

File (File dirObj; String fileName );

For example:

File f1 = new File ("/");

File f2 = new File ("/", "abc.txt ");

File f3 = new File (f1, "abc.txt ");

Note:

DirectoryPath: the path name of the file.

FileName: File Name

DirObj: Specifies the File object of the directory.

Separator Problems

In Windows, "\" is used for path separators, while "/" is used for Linux separators. To make java programs more portable, you must consider separators, therefore, the File class provides a constant: public static final String separator.

File f1 = new File ("c:" + File. separator + "abc.txt ");

Common File Methods

 

Package cn.com. file; import java. io. file; import java. io. IOException;/*** FileDemo * @ author: hushaoyu * 12:29:39 * @ Description: use the file class */public class FileDemo {public static void main (String [] args) throws IOException {// construct a folder to create a fileobject, define an edisk, and File name: hello.txt File f1 = new File ("E:" + File. separator + "HelloFile.txt"); if (f1.exists () {f1.delete () ;}else {f1.createNewFile () ;}// getName () method, get the file name System. out. printl N ("file name:" + f1.getName (); // getPath () method to obtain the file name path System. out. println ("file name path" + f1.getPath (); // getA () method to obtain the absolute file name path System. out. println ("absolute path of file name" + f1.getAbsolutePath (); // getParent (), obtain the parent folder name System. out. println ("parent folder name" + f1.getParent (); System. out. println (f1.exists ()? "File exists": "file does not exist"); System. out. println (f1.canWrite ()? "File writable": "file writable"); System. out. println (f1.canRead ()? "File readable": "file unreadable"); System. out. println (f1.isDirectory ()? "Yes": "not" + "directory"); System. out. println (f1.isFile ()? "Is a file": "Not a file"); System. out. println (f1.isAbsolute ()? "Is absolute path": "Not absolute path"); System. out. println ("last modification time" + f1.lastModified (); System. out. println ("file size" + f1.length (); System. out. println ();/** result output: File Name: HelloFile.txt file name path E: \ HelloFile.txt file name absolute path E: \ HelloFile.txt parent folder name E: \ The file contains writable files. readable files are not directories. Files are absolute paths. last modification time: 1487139283879 file size: 0 */}}
Create a directory file
Package cn.com. file; import java. io. file; import java. io. IOException; import com.sun.org. apache. bcel. internal. generic. f2D;/*** FileDemo * @ author: hushaoyu * 12:29:39 * @ Description: use the file class */public class FileMakeDirs {public static void main (String [] args) throws IOException {// construct a folder to create a fileobject, define an edisk, and File name: hello.txt File f1 = new File ("E:" + File. separator + "makedir" + File. separator + "makeFile.txt"); if (! F1.getParentFile (). exists () {// The parent path does not exist f1.getParentFile (). mkdir (); // create a directory} f1.createNewFile (); // create a file System. out. println ("directory File name (type File)" + f1.getParentFile (); // The returned type is FileSystem. out. println ("directory file name (type String)" + f1.getParent (); // return type StringSystem. out. println ("File name path" + f1.getPath ();/** output result: directory File name (type File) E: \ makedir directory File name (type String) E: \ makedir file name path E: \ makedir \ makeFile.txt */}}

  

  

 

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.