Java File class method uses detailed

what 

You must create a link to a file or a file object is instantiated before the operation file, you can specify a file that does not exist in order to create it.

File object represents the actual existence of the disk files and directories .

Java  in the File class is an abstract form of file and directory path names. Use the File class can get some information about the file itself, for example, to find and delete files created, files can be used for file and directory file's directory, file size, file read and write permissions, etc.

About the File class

In Java, File class java.io package is the only object that represents a disk file itself. File class defines a number of platform-independent method to manipulate files, the File class is mainly used to acquire or process information related to a disk file, such as file name, file path, access and modification dates, etc. can also browse the subdirectory hierarchy .
File class represents information processing files and file systems. In other words, File class does not have the ability to read information from and write information to a file file, which describes only the properties of the file itself.

File class provides the following three forms of the constructor.

    1. File (File parent, String child): Creates a new File instance from a parent abstract pathname and a child pathname string. 
      File f4 = new File("C:\\Users\\Administrator\\Desktop\\");
      或者
      File f4 = new File("C:\\Users\\Administrator\\Desktop");
      或者
      File f4 = new File("C:/Users/Administrator/Desktop/");
       
      File f5 = new File(f4, "FileWord.txt");
    2. File (String pathname): converting the given pathname string into an abstract pathname to create a new File instance. If the given string is a null string, the result is empty abstract pathname.
      File f2 = new File("C:\\Users\\Administrator\\Desktop\\FileWord.txt");
      或者:
      File f2 = new File("C:/Users/Administrator/Desktop/FileWord.txt");
    3. File (String parent, String child): Creates a new File instance from a parent pathname string and a child pathname string
      File f3 = new File("C:\\Users\\Administrator\\Desktop\\", "FileWord.txt");
      或者
      File f3 = new File("C:/Users/Administrator/Desktop/", "FileWord.txt");

      NOTE: \ single backslash has a special meaning in java, so it is necessary to become \\ double slash.

Using any of a constructor can create a File object, then call the method that provides the file operation. In Table 1 lists the common File class and description.

Table 1 File class conventional method
Method name Explanation
boolean canRead() Tests whether the application from the specified file for reading
boolean canWrite () Tests whether the application can write the current file
boolean delete() You specify the file to delete the current.
boolean exists() Test whether there is this File
String getAbsolutePath() Returns the file represented by this object is an absolute pathname
String getName() Returns the file name of the current object
String getParent() Returns the current File object path name of the Father pathname, if no parent path name or null
boolean isAbsolute() File File object represents the current test of whether it is an absolute pathname
boolean isDirectory() Test whether the current file File object represents a path
boolean isFile() Test whether the current file File object represents a "normal" file
long lastModified() Returns the current file File object represents a time of last modification
long length() Returns the file length of the File object represents the current
String[] list() Returns a list of this File object specifies the path to the file
String[] list(FilenameFilter) File object returns a list of files specified in the current directory that satisfy the specified filter
boolean mkdir() Create a directory whose pathname is specified by this File object
boolean mkdirs() Create a directory whose pathname is specified by this File object
boolean renameTo(File) The current file is renamed File object specified for a given parameter File specifies the pathname


Note: suppose you have a file in the Windows operating system  D:\javaspace\hello.java, when used in Java, its path to be written  D:/javaspace/hello.java or  D:\\javaspace\\hello.java.

The following is a java test:

Project structure:

 

java code:

package FileDemo;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class FileAction{

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        //file 操作文件
        
        File f1 = new File("src/FileWord.txt");
        System.out.println("得到f1的绝对路径:"+f1.getAbsolutePath());
        
        File f2 = newFile ( "C: \\ Administrator the Users \\ \\ \\ FileWord.txt Desktop" ); 
        File F3 = new new File ( "C: \\ Desktop the Users \\ \\ \\ Administrator", "FileWord.txt" ) ; 
        File F4 = new new File ( "C: \\ the Users Desktop \\ \\ \\ Administrator" ); 
        File F5 = new new File (F4, "FileWord.txt" ); 
        
        System.out.println ( "get the absolute f2 path: "f2.getAbsolutePath + ()); // getAbsolutePath () to get the absolute path to the file, even if the absolute path in the project file, the resulting file is 
        System.out.println (" get f3 absolute path: "+ f3.getAbsolutePath ()); 
        System.out.println ( "f5 obtained absolute path:" + f5.getAbsolutePath());
        
        System.out.println (F2 == F3); 
        System.out.println (f2.equals (F5)); 
        
        a Date D = new new a Date (f2.lastModified ()); 
        the SimpleDateFormat S = new new the SimpleDateFormat ( "YYYY-DD- HH the mM: mm: SS " ); 
        
        System.out.println ( " get the last modified the file: "+ s.format (D)); // the lastModified () to get the last modified time of the file 
        
        System.out.println ( "determine whether a file exists:" + f2.exists ()); // eXISTS () to determine whether a file exists If the file does not exist, it returns false, file modification time to appear 1970-01-01 08:00:00 computer time 
        System.out.println ( "get file length in bytes:" + f2.length ()); // length () to get the length of the file in bytes
        System.out.println ( "to determine whether the file is hidden:" + f2.isHidden ()); // isHidden () to determine whether hidden files 
        
        System.out.println (f2.getName ()); // getName () to get the file names, only the name, not including the file path 
        System.out.println (f2.getPath ()); // path getPath () to get the file, when the file is located in the project, the resulting relative path, when the file is located in the project outside, the resulting absolute path 
        System.out.println (f2.getParent ()); // getParent () to get the parent directory of the file when the file is located in the project, the project returns the parent directory, when files when the project is located outside, get files parent directory 
        System.out.println (f1.delete ()); // the delete () to delete the original file when the file exists, delete the file, returns true, if the file does not exist when the original , returns to false 
        System.out.println (f2.canRead ()); // canRead () is readable 
        System.out.println (f2.canWrite ()); //canWrite () is writable 
        System.out.println (f2.isDirectory ()); // isDirectory () is a directory true: that, false: not 
        System.out.println (f2.isFile ()); // isFile () if the file is true: that, false: not 
        System.out.println (f2.isAbsolute ()); // isAbsolute () 
        
        file F6 = new new file ( "C: \\ MyFileWord.txt" );
         // first determine whether a file exists, exists () true: there is, false: does not exist, getAbsolutePath () to get the absolute path to the file, delete () delete the file, when createNewFile () file does not exist, create a file 
        iF (f6.exists ()) { 
            System.out.println ( "f6 obtained absolute path:" + f6.getAbsolutePath ()); 
            f6.delete (); 
        } the else{
             The try { 
                f6.createNewFile (); // createNewFile () when the file does not exist, create a file 
                System.out.println ( "f6 file is successfully created:" + f6.getAbsolutePath ()); 
            } the catch (IOException E) {
                 / / the TODO Auto-Generated Block the catch 
                e.printStackTrace (); 
            } 
            
        } 
        
        
        // File directory operation 
        
        // first determine whether a directory exists, exists () true: present, false: absent, getAbsolutePath () to obtain the absolute path of the directory, delete () deletes a directory, mkdir () to create the directory 
        File f7 = new new File ( "C: \\ FileDemo" );
         IF (f7.exists ()) { 
            System.out.println ("Directory exists:" + f7.getAbsolutePath ()); 
            f7.delete (); 
        } the else { 
            f7.mkdir (); 
            System.out.println ( "exist directory does not exist, a new directory:" + f7.getAbsolutePath () ); 
        } 
        
        // traverse directories
         // can find files by traversing the directory specified directory, or display a list of all the files. List File class () method provides a directory traversal features     
        File F8 = new new File ( "C: \\ FileDemo" ); 
        String [] Filelist = f8.list (); // call to the non-reference list () method to return String [] array
         // through the returned array 
        for ( int I = 0; I <filelist.length; I ++ ) {
             //filelist [i] only returns the name of the directory or file folder 
            System.out.println (filelist [i]);
             // If you need other attributes of the file, you need to convert the File object, according to its parent directory position, and then call a method other attributes, as follows 
            System.out.println ( "absolute path of the file:" + new new file ( "C: \\ FileDemo" , Filelist [I]) getAbsolutePath ().); 
            System.out.println ( "whether the document:" + new new file ( "C: \\ FileDemo" , filelist [i]) isFile ().); 
            System.out.println ( "is a directory:" + new new file ( "C: \ \ FileDemo " , Filelist [I]) the isDirectory ());. 
        } 
        System.out.println ( " *************************** *********** " );
         //If you want to list some of the files in the directory, you need to call the list with a filter parameter () method, you first need to create a file filter, the filter must implement java.io.FilenameFilter interface, and accept () method specified allowed document types 
        file F9 = new new file ( "C: \\ FileDemo" );
         // implement java.io.FilenameFilter interface and allows the specified file type accept () method 
        String [] filelist1 = f9. List ( new new FilenameFilter () { 
            @Override 
            public  boolean the Accept (file dir, String name) {
                 // TODO Auto-Generated Method, Stub
                 // allowed txt, docx file types 
                return name.endsWith (. "TXT") || name.endsWith ( "docx." ); 
            } 
        }); 
        
        // traverse the array returned 
        for( Int I = 0; I <filelist1.length; I ++ ) {
             // Filelist [I] only returns the name of the file or directory folder 
            System.out.println (filelist1 [I]);
             // if need other attributes of the file, need to be converted into a file object, according to the position of its parent directory, then call a method other attributes, as follows 
            System.out.println ( "absolute path of the file:" + new new file ( "C: \\ FileDemo" , filelist1 [I]) getAbsolutePath ());. 
            System.out.println ( "whether the document:" + new new file ( "C: \\ FileDemo" ., filelist1 [I]) isFile ()); 
            the System.out .println ( "it is a directory:" + new new File ( "C: \\ FileDemo" ., filelist1 [I]) the isDirectory ());
        }
        
        
    }

    

}
C: \\ FileDemo file structure:

 

 Console output print:

F1 to get the absolute path: E: \ workplace_mslocationserver_xin \ FileServer \ src \ FileWord.txt

F2 get the absolute path: C: \ Users \ Administrator \ Desktop \ FileWord.txt

F3 get the absolute path: C: \ Users \ Administrator \ Desktop \ FileWord.txt

F5 get the absolute path: C: \ Users \ Administrator \ Desktop \ FileWord.txt

false

true

Get the file was last modified time: 2019-06-06 09:52:23

Determine whether a file exists: true

The resulting file length in bytes: 6

To determine whether hidden files: false

FileWord.txt

C:\Users\Administrator\Desktop\FileWord.txt

C:\Users\Administrator\Desktop

false

true

true

false

true

true

F6 get the absolute path: C: \ MyFileWord.txt

Directory exists: C: \ FileDemo

Filea

Absolute path of the file: C: \ FileDemo \ filea

Whether the file: false

Is a directory: true

fileb.txt

Absolute path of the file: C: \ FileDemo \ fileb.txt

Whether the file: true

Is a directory: false

filec.docx

Absolute path of the file: C: \ FileDemo \ filec.docx

Whether the file: true

Is a directory: false

**************************************

fileb.txt

Absolute path of the file: C: \ FileDemo \ fileb.txt

Whether the file: true

Is a directory: false

filec.docx

Absolute path of the file: C: \ FileDemo \ filec.docx

Whether the file: true

Is a directory: false

 

 

Reference documents: http://c.biancheng.net/view/1133.html

Guess you like

Origin www.cnblogs.com/ttqi/p/10984245.html