Java File File operations

Source: Internet
Author: User

1. Basic Concepts

File: An abstract representation of the path names of files and directories, representing files or folders.

2. Construction method
        // 根据parent抽象路径名和child路径名字符串创建一个新File实例        File(File parent, String child)        // 通过将给定路径名字符串转换为抽象路径名来创建一个新File实例 File(String pathname) // 根据parent路径名字符串和child路径名字符串创建一个新File实例 File(String parent, String child) // 通过将给定的file:URI转换为一个抽象路径名来创建一个新的File实例 File(URI uri)
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
3. Common methods (1). Creating features

Creates the directory specified by this abstract path name
Boolean mkdir ()
Creates the directory specified by this abstract pathname, including all required but nonexistent parent directories
Boolean mkdirs ()
A new, empty file is not created when and only if there is no file with the specified name with this abstract pathname
Boolean CreateNewFile ()
Creates an empty file in the default temporary file directory, using the given prefix and suffix to generate its name
Static File createtempfile (string prefix, string suffix)
Creates a new empty file in the specified directory, using the given prefix and suffix string to generate its name
Static file Createtempfile (string prefix, string suffix, File directory)

        Construction Method A File file1 =New File ("F://file1");Construction Method Two File file2 =New File ("F://file1","File2");//construction method Three file File3 = new file (File2,  "File3.txt"); //creates the directory and returns whether it was created successfully, or returns False if the directory exists Boolean b1 = File1.mkdir (); System. out.println (B1); //true //Create directory Boolean b2 = File2.mkdir (); System. out.println (B2); //true //Create file //create/file1/under F drive File2/file3.txt file Boolean b3 = File3.createnewfile (); System. out.println (b3); //true //Create empty file and specify prefix and suffix //created under F disk /file1/file2/file4.....exe file File.createtempfile ( "File4", . EXE ", file2);                
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st

Note: The effect of three kinds of construction method is equal, there is no essential difference; When creating a directory with the directory mkdir (), Mkdirs () method, Mkdirs () method, if the previous level of directory to be created does not exist, mkdir () can only create a single-stage directory.

(2). Delete function

Delete the file or directory represented by this abstract path name
Boolean Delete ()

       //删除目录       System.out.println(file1.delete()); // false       //删除文件 System.out.println(file3.delete());// true
    • 1
    • 2
    • 3
    • 4

Note: When you delete an operation, the directory is deleted, and you must ensure that it is an empty directory.

(3). Judging function

Tests whether the file or directory represented by this abstract path name exists
Boolean exists ()
Tests whether the file represented by this abstract path name is a directory
Boolean isdirectory ()
Tests whether the file represented by this abstract path name is a standard file
Boolean isfile ()
Tests whether the file specified by this abstract path name is a hidden file
Boolean Ishidden ()
Tests whether the application can read the file represented by this abstract path name
Boolean CanRead ()
Tests whether the application can modify the file represented by this abstract path name
Boolean canWrite ()

File File =New File ("F://hello"); File file2 =New file (file,"Blog.txt");Determine if there isif (!file.exists ()) {Create directory File.mkdir (); }if (file2.exists ()) {Create file File2.createnewfile (); }Determines whether the directory System is.Out.println (File.isdirectory ());True//determine if it is the file System. out.println (File.isfile ()); //false System. out.println (File2.isdirectory ()); //false System. out.println (File2.isfile ()); //true System. out.println (File2.ishidden ()); //false //to determine if it is a hidden system.//false //determine if readable system.//true //determine if writable System.//true             
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26

Note: You can modify the file readability and view the different output independently.

(4). Get function (1). Basic Access function

Returns the name of the file or directory represented by this abstract path name
String GetName ()
Returns the absolute pathname form of this abstract path name
File Getabsolutefile ()
Returns the absolute pathname string for this abstract path name
String GetAbsolutePath ()
Converts this abstract pathname to a path name string
String GetPath ()
Returns the last time that the file was modified by this abstract pathname name
Long LastModified ()

File File =New File ("F://hello"); File file2 =New file (file,"Blog.txt");//determine if a file or directory exists if (!file.exists ()) { //does not exist then create FILE.MKDIR ();} if (file2.exists ()) {//Create file File2.createnewfile ();} //gets the file name or directory name System. out.println (File2.getname ()); //blog.txt //get absolute path to file or directory System.//F:\hello\blog.txt //Gets the path name of the file or directory (absolute path or relative path) System.out.println (File2.getpath ()); //F:\hello\blog.txt //get file or directory modification the last time to return a millisecond value System.out.println (File2.lastmodified ()); //1463734158963            
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
(2). Iterative acquisition function, filter function

Returns an array of strings that specify the files and directories in the directory represented by this abstract path name
String[] List ()
Returns an array of strings that specify the files and directories in the directory that this abstract pathname represents to satisfy the specified filter
String[] List (filenamefilter filter)
Returns an abstract path to the an array group that represents the files in the directory represented by this abstract path name
File[] Listfiles ()
Returns the abstract path to the an array group, which represents the files and directories in the directory that this abstract pathname represents to satisfy the specified filter
File[] Listfiles (filefilter filter)
Returns the abstract path to the an array group, which represents the files and directories in the directory that this abstract pathname represents to satisfy the specified filter
File[] Listfiles (filenamefilter filter)

Diagram: First look at the east of the F-plate

File File =New File ("f://");Gets the file and directory under the abstract path name string[] s = file.list ();Filter file or directory name string[] ss = File.list (New FilenameFilter () {public BooleanAccept (File dir, String name) {Returns a file or directory name ending with a. pngControls the return value to determine whether to add to the arrayReturn Name.endswith (". png"); } });Enhanced for outputfor (Stringstring:s) {System.Out.print (String +" ");$RECYCLE. BIN android4.0 Black Horse android video ... And so onEnhanced forfor (StringSTRING:SS) {System.Out.print (String +" ");Ic_ptr_loading.png Ic_ptr_pull.png Ic_ptr_release.png}Gets the file and directory objects under the abstract path name file[] files = file.listfiles ();Gets the file and directory object under the abstract path name, adds file filter file[] Files2 = File.listfiles (New FileFilter () {public BooleanAccept (File pathname) {Determine if the directory is hiddenReturn (Pathname.isdirectory () &&pathname.ishidden ()); } });Gets the file and directory object under the abstract path name, adding the filename filter file[] Files3 = File.listfiles (New FilenameFilter () {public BooleanAccept (File dir, String name) {Determine if a file ends in PNGreturn (new File (dir, name). Isfile ()) && Name.endswith ( ". png");}); for (File f:files) {system. Out.print (F.getname () + $RECYCLE. BIN android4.0 Black Horse android video ... Wait, System. out.println (); for (File f:files2) {system. Out.print (F.getname () + $RECYCLE. BIN Ghos} System. out.println (); for (File f:files3) {system. Out.print (F.getname ()); //ic_ptr_loading.pngic_ptr_pull.pngic_ptr_release.png}      
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
(5). Renaming function

Renames the file represented by this abstract path name
Boolean Renameto (File dest)

        Renames the file represented by this abstract path name//boolean renameto (file dest) file File = new file ( "f://"); file[] files = file.listfiles (); for (int i = 0; i < files.length; I + +) {File f = files[i]; //determine if the file ends with. png if (F.isfile () && f.getname (). EndsWith ( ". png") {//change the file name, Renameto () receives the file object, where the object does not specify a drive letter Boolean B = F.renameto (new File ( "pic" + i + ". png ")); System. out.println (b); //true //true //True}}  span>               
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

Icon:

Note: When changing a file or directory name, the Renameto () method Parameter object if you do not specify a disk, the file is cut to the project directory by default (as seen above), and the specified drive letter is cut to that position according to the specified position. The Renameto () method is equivalent to a cut-weighted name.

Java File File operations

Related Article

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.