Java IO (a) ------ File class

 IO Overview

When the need to store the data in memory to the persistent action is called an output device (write) the Output operations.

When reading data on the persistent memory device to the action called an input (read) the Input operation.

Therefore, we call this the input and output operation is called IO operations.

File class constructor

 

How to use the above-described construction method:

public  class FileDemo {
     public  static  void main (String [] args) {
         // File presentation constructor 
        String pathName = "E: day22e \\ \\ \\ java_code Hello.java" ; 
        File F1 = new new File (pathName); / / the files packaged as Test22 file object. Note; there can be encapsulated file or folder does not exist, the object becomes. 
        System.out.println (F1); 
        
        File F2 = new new File ( "E: \\ \\ day22e java_code", "Hello.java" ); 
        System.out.println (F2); 
        
        // be packaged as parent file object . 
        The dir = File new new File ( "E: \\ \\ day22e java_code" ); 
        File F3= new File(dir,"hello.java");
        System.out.println(f3);
    }
}

 

 

 

 

 

3, the File class common method

 

  ①, create a method

 

    1.boolean createNewFile () returns true there is no return to the presence of false
    2.boolean mkdir () to create the directory, if the parent directory does not exist, it will fail to create
    3.boolean mkdirs () to create multi-level directory, if the parent directory does not exist will be created automatically

 

 

 

  ②, delete method

 

    1.boolean delete () delete a file or directory, if indicates a directory, delete the directory must be empty before
    2.boolean deleteOnExit () to delete files after completion

 

 

 

  ③, determination method

 

    Whether 1.boolean canExecute () determines an executable file
    2.boolean canRead () determines whether the file is readable
    3.boolean canWrite () determines whether the file is writable
    4.boolean exists () whether a file or directory exists
    5.boolean isDirectory ( ) to determine whether this path is a directory
    6.boolean isFile () to determine whether a file
    7.boolean isHidden () to determine whether a hidden file
    8.boolean isAbsolute () to determine whether the absolute path of the file does not exist can judge

 

 

 

   ④, acquisition method

 

    1.String getName () Gets file or directory name this path represented
    2.String getPath () Converts this path name for pathname string
    3.String getAbsolutePath () Returns the absolute pathname of this abstract form
    4.String getParent ( ) // If no parent returns null
    5.long lastModified () // get the last modification time
    6.long length () returns the length of the file denoted by this abstract pathname.
    7.boolean renameTo (File f) Rename the file thus abstract pathname represented.
    8.File [] liseRoots () // Get Machine letter
    9.String [] list () Returns a string array, whereby a directory named abstract pathname in the file and directory.
    10.String [] list (FilenameFilter filter) Returns a string array, the directory name thus satisfy the specified filter the files and directories of abstract pathname.

 listFiles () method

 

    public  static  void main (String [] args) { 
        File the dir = new new File ( "E: \\ java_code" );
         // Get the name of the current directory and the file folder. 
        String [] = names dir.list ();
         for (String name: names) { 
            System.out.println (name); 
        } 
        // get the current file and directory file object, as long as the object to get the file, then it can be acquired wherein information desired 
        File [] = Files dir.listFiles ();
         for (File File: Files) { 
            System.out.println (File); 
        } 
    } 
}

1 , the specified directory must exist,

2 , must be specified directory. Otherwise easily lead to the return array is null , appear NullPointerException

 

 

 

   

 

Guess you like

Origin www.cnblogs.com/-lwl/p/11260961.html