Java basic knowledge of the Io-file class

Source: Internet
Author: User
Tags parent directory

File class Introduction

File is a class under the Java.io package that represents a platform-independent file or directory. In Java, files and directories can be considered an object of the file class. The file class can create new, delete, and get properties for files or directories, but cannot manipulate the contents of the file directly (the contents of the file need to be accessed with a data stream).

The JVM defaults to workspace as a relative path, the path that the USER.DIR system variable refers to, that is, if you initialize the file object, file File = new file ("."); is to get the User.dir path.

Common methods for file classes

String GetName ()-If the file object is a document, the file name is returned, and if it is a path, the last level of the path is returned

File getabsolutefile ()-Returns the absolute path

String getParent ()-Returns the parent directory of the directory where the file object resides

File class Check Files

Exists ()-whether the file or directory exists

CanRead ()-Readable

Isfile ()-Whether it is a file

Isdirectory ()-Whether it is a directory

The file class gets the Files property

Long LastModified ()-Last modified time

Long length ()-File length

File class for files operation

CreateFile ()-Success true, fail false

Delete ()

mkdir ()-Create directory, file object must correspond to a path

String[] List ()-If the file object is a path, list () returns an array that is empty if there are no files and subdirectories under the path, if the file object is a file, or the file path does not exist, or an IO error occurs, the list () Returns null

File[] Listfiles ()-ditto, except that the file type array is returned

File Filter FilenameFilter interface

If FilenameFilter is passed as a parameter of File.list (), implementing the Accept method of the FilenameFilter interface can implement file filtering.

The Accept method has two parameters, dir and name, which can usually be conditionally filtered.

The following is a comprehensive demonstration of the file class,

1  Packageio;2 3 ImportJava.io.File;4 ImportJava.io.FilenameFilter;5 Importjava.io.IOException;6 7  Public classFiletest {8      Public Static voidMain (string[] args)throwsIOException {9         //The JVM defaults to workspace as a relative path, that is, the path indicated by the USER.DIR system variableTenFile File =NewFile ("."); OneFile file2 =NewFile ("C:/project/javabasic/project_javabasic/tmp.txt"); AFile File3 =NewFile ("./tmp.txt")); -         //If the file object is a document, the file name is returned, and if it is a path, the last level of the path is returned - System.out.println (File.getname ()); the         //getting the parent path can be an error when file is a relative path - File3.delete (); -SYSTEM.OUT.PRINTLN ("Parent path:" +file3.getparent ()); -         //Get absolute path + System.out.println (File.getabsolutefile ()); -         //getabsolutefile Returns a file type, GetAbsolutePath returns a string type + System.out.println (File.getabsolutefile (). GetParent ()); A         //Create a temporary file under the current path, the third parameter must be a path, or an error will be atFile tmpfile = file.createtempfile ("AAA", ". txt", file); -         //Delete this file when the JVM exits - tmpfile.deleteonexit (); -         //create a file with system time as a name -File NewFile =NewFile (System.currenttimemillis () + ""); -System.out.println ("NewFile exists:" +newfile.exists ()); in         //to specify a file object to create files - newfile.createnewfile (); toSystem.out.println ("NewFile is a file:" +newfile.isfile ()); +System.out.println ("NewFile is directory:" +newfile.isdirectory ()); -System.out.println ("The creation of a directory with a NewFile object is successful:" +Newfile.mkdir ()); the         //If the file object is a path, list () returns an array if there are no files and subdirectories under the path , and the array is empty *         //list () returns null if the file object is a document, or the file path does not exist, or an IO error occurs $string[] FileList =file.list ();Panax NotoginsengSystem.out.println ("= = = = = = All files and directories below the current path = = =); -          for(String filename:filelist) { the System.out.println (fileName); +         } A         //Listroots static method lists all disk root paths theFile[] Roots =file.listroots (); +System.out.println ("= = = System all disk root path as follows = = ="); -          for(File root:roots) { $ System.out.println (root); $         }     -          -string[] NameList = File.list (NewFilenameFilter () { the @Override -              Public BooleanAccept (File dir, String name) {Wuyi                 //TODO auto-generated Method Stub the                 returnName.endswith (". txt") | |NewFile (name). Isdirectory (); -             } Wu         }); -System.out.println ("= = = = = = = = = = = = =); About          for(String filtername:namelist) { $ System.out.println (filtername); -         } -          -     } A}

The output results are as follows,

1 .2 parent path:.3 C:\PROJECT\JavaBasic\PROJECT_JavaBasic\.4 C:\PROJECT\JavaBasic\PROJECT_JavaBasic5NewFile is present:false6NewFile whether the file is stored:true7NewFile whether the directory is stored:false8Whether the directory was successfully created with the NewFile object:false9= = = = = = All files and directories below the current path = = =Ten . Classpath One . Project A . Settings -1480521332106 -1480521357843 the1480521378187 -1480522448612 -1480522471511 -1480522477413 +1480522484941 -1480522505253 + Aaa2174524463556527449.txt A bin at src -= = = System All disk root paths are as follows = = - C: - D: - E: - F: in= = = = = = = = After filter - . Settings to Aaa2174524463556527449.txt + bin -Src

Java basic knowledge of the Io-file class

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.