IO streams a

Use 1 File class

The concept of class 1.1 File

  • java.io.File class is an abstract representation of unrelated, and platform file or directory paths.
  • File class can create, delete, rename files and directories, but the File class can not access the contents of the file itself. If you need to access the contents of the file itself, you need to use input / output streams.
  • Want to show in a real Java program file or directory, you must have a corresponding File object, but Java programs File object, may not have a real file or directory.
  • File object stream can be passed to the constructor as an argument.

1.2 File class constructor

  • File object is created based on the path (relative and absolute paths).
public File(String pathname){}

 

  • Example:
Package Penalty for day19; 

Import java.io.File; 

/ ** 
 * @motto: one fought three thousand miles, but when the sword million soldiers. 
 * @Author : not to disturb the past, the rest just love to laugh. 
 * @Version : 1.0 
 * @Since : 2019-06-30
  * / 
public  class FileTest {
     public  static  void main (String [] args) { 
        File File = new new File ( "D: \\ a.txt" ); 
    } 
}

 

  • Path parent to parent, child to create a File object for the sub-path.
public File(String parent, String child){}

 

  • Example:
Package Penalty for day19; 

Import java.io.File; 

/ ** 
 * @motto: one fought three thousand miles, but when the sword million soldiers. 
 * @Author : not to disturb the past, the rest just love to laugh. 
 * @Version : 1.0 
 * @Since : 2019-06-30
  * / 
public  class FileTest {
     public  static  void main (String [] args) { 
       File File = new new File ( "D: \\", "a.txt" ) ; 
    } 
}

 

  • Create a File object File object according to a parent and child file path.
public File(File parent, String child){}

 

  • Example:
Package Penalty for day19; 

Import java.io.File; 

/ ** 
 * @motto: one fought three thousand miles, but when the sword million soldiers. 
 * @Author : not to disturb the past, the rest just love to laugh. 
 * @Version : 1.0 
 * @Since : 2019-06-30
  * / 
public  class FileTest {
     public  static  void main (String [] args) { 
       File File = new new File ( new new File ( "D: \\"), "A .txt " ); 
    } 
}

1.3 File class path delimiter

  • Use path separator spaced between each stage in the directory path.
  • Path separators and related systems:
    • Windows and DOS systems using the default "\" to represent.
    • UNIX uses "/" to represent,  
  • Java programs support cross-platform operation, so the path separator to be used with caution.
  • In order to solve the paths of different platforms with different delimiters, File provides a constant, to represent the path separator.
public static final String separator = "" + separatorChar;

1.4 File class common method

  •  Gets the absolute path:
public String getAbsolutePath(){}

 

  • Example:
Package Penalty for day19; 

Import java.io.File; 

/ ** 
 * @motto: one fought three thousand miles, but when the sword million soldiers. 
 * @Author : not to disturb the past, the rest just love to laugh. 
 * @Version : 1.0 
 * @Since : 2019-06-30
  * / 
public  class FileTest {
     public  static  void main (String [] args) { 
       File File = new new File ( new new File ( "D: \\"), "A .txt " ); 

        System.out.println ( " get the absolute path: "+ file.getAbsolutePath ()); // actual case, the file or directory exists, it does not matter 
    } 
}

 

  • Get the path:
public String getPath(){}

 

  • Example:
Package Penalty for day19; 

Import java.io.File; 

/ ** 
 * @motto: one fought three thousand miles, but when the sword million soldiers. 
 * @Author : not to disturb the past, the rest just love to laugh. 
 * @Version : 1.0 
 * @Since : 2019-06-30
  * / 
public  class FileTest {
     public  static  void main (String [] args) { 
       File File = new new File ( new new File ( "D: \\"), "A .txt " ); 

        System.out.println ( " Get path: "+ file.getPath ()); // path here, refers to the path (relative or absolute path) File constructor, the actual situation , file or directory exists, in terms of this method, it does not matter 
    } 
}

 

  • Get the name:
Package Penalty for day19; 

Import java.io.File; 

/ ** 
 * @motto: one fought three thousand miles, but when the sword million soldiers. 
 * @Author : not to disturb the past, the rest just love to laugh. 
 * @Version : 1.0 
 * @Since : 2019-06-30
  * / 
public  class FileTest {
     public  static  void main (String [] args) { 
       File File = new new File ( new new File ( "D: \\"), "C .txt " ); 

        System.out.println ( " get the name: "+ file.getName ()); // name here, merely represents the name file constructor file or directory, the actual file or directory exists , this method is not important 
    } 
}

 

  • Get file or directory path to the top (if not, return null):
public String getParent(){}

 

  • Example:
Package Penalty for day19; 

Import java.io.File; 

/ ** 
 * @motto: one fought three thousand miles, but when the sword million soldiers. 
 * @Author : not to disturb the past, the rest just love to laugh. 
 * @Version : 1.0 
 * @Since : 2019-06-30
  * / 
public  class FileTest {
     public  static  void main (String [] args) { 
        File File = new new File ( new new File ( "D: \\"), "C .txt " ); 
        System.out.println ( " Get the upper path of the directory or file: "+ file.getParent ()); 
    } 
}

 

  • Get the length of the file (i.e. number of bytes, if the file does not exist, it returns 0, the length of the directory can not be obtained):
public long length(){}

 

  • Example:
Package Penalty for day19; 

Import java.io.File; 

/ ** 
 * @motto: one fought three thousand miles, but when the sword million soldiers. 
 * @Author : not to disturb the past, the rest just love to laugh. 
 * @Version : 1.0 
 * @Since : 2019-06-30
  * / 
public  class FileTest {
     public  static  void main (String [] args) { 
        File File = new new File ( new new File ( "D: \\"), "C .txt " ); 
        System.out.println ( " Get the length of the file: "+ file.length ()); 
    } 
}

 

  • Acquiring last modification time, millisecond value (if the file does not exist, returns 0):
public long lastModified(){}

 

  • Example:
Package Penalty for day19; 

Import java.io.File; 

/ ** 
 * @motto: one fought three thousand miles, but when the sword million soldiers. 
 * @Author : not to disturb the past, the rest just love to laugh. 
 * @Version : 1.0 
 * @Since : 2019-06-30
  * / 
public  class FileTest {
     public  static  void main (String [] args) { 
        File File = new new File ( new new File ( "D: \\"), "C .txt " ); 
        System.out.println ( " Get last modification time: "+ file.lastModified ()); 
    } 
}

 

  • Get the name of an array of all files or directories under a specified directory:
public String[] list(){}

 

  • Example:
Package Penalty for day19; 

Import java.io.File; 

/ ** 
 * @motto: one fought three thousand miles, but when the sword million soldiers. 
 * @Author : not to disturb the past, the rest just love to laugh. 
 * @Version : 1.0 
 * @Since : 2019-06-30
  * / 
public  class FileTest {
     public  static  void main (String [] args) { 
        File File = new new File ( "D: \\" ); 
        String [] fileName = File.List for ();
         for (String name: fileName) { 
            System.out.println ( "filename in the specified directory:" + name); 
        } 
    }
}

 

  • File obtain an array of all files or directories under the specified directory:
public File[] listFiles(){}

 

  • Example:
Package Penalty for day19; 

Import java.io.File; 

/ ** 
 * @motto: one fought three thousand miles, but when the sword million soldiers. 
 * @Author : not to disturb the past, the rest just love to laugh. 
 * @Version : 1.0 
 * @Since : 2019-06-30
  * / 
public  class FileTest {
     public  static  void main (String [] args) { 
        File [] Files = new new File ( "D: \\" ) .listFiles () ;
         for (file file: files) { 
            System.out.println ( "Get file object files or directories in the specified directory:" + file); 
        } 
    } 
}

 

Guess you like

Origin www.cnblogs.com/xuweiweiwoaini/p/11108553.html