The basic usage of Java in the File class

The basic usage of the File class

  java.io.File class: representatives of files and directories. In the development, read files, generated files, delete files, often used when such property to modify the file.

Common File class constructor: public File (String pathname)

  Pathname to create a File object to the path, if pathname is a relative path, the default path is stored in the current system properties in user.dir.

Create a file

Import java.io.File;
 Import java.io.IOException; 

public  class the Test {
     public  static  void main (String [] args) throws IOException { 
        System.out.println (the System.getProperty ( "user.dir")); / / output current project is the absolute path 
        File f1 = new new File ( "a.txt");     // relative path, the default directory System.out.println (System.getProperty ( "user.dir")); 
        boolean FLAG1 = f1 .createNewFile (); 
        System.out.println (FLAG1); 
        File F2 = new new File ( "F: /b.txt"); // absolute path 
        boolean flag2 = f2.createNewFile();
        System.out.println(flag2);
    }
}
//输出
G:\IntelliJ IDEA 2018.2.4\IdeaProjects
true
true

You can access the properties file by FIle class object:

  Table 8-3 File class to access the list of methods of property

Create an empty file or directory via the File object (in the case of the object within the meaning of the file or directory does not exist)

Table 8-4 File class to create a list of files or directories methods

 

 

 

Guess you like

Origin www.cnblogs.com/chiweiming/p/11308229.html