This is the 10th day of my participation in the August More Text Challenge. For details, see:August is more challenging

File class overview and constructors

The File type is introduced

  • It is an abstract representation of file and directory pathnames
  • Files and directories can be encapsulated as objects using File
  • In the case of File, it encapsulates not an actual File, but just a path name. It may or may not exist. In the future, the contents of this path will be translated into concrete existence through concrete operations

The constructor of the File class

The method name instructions
File(String pathname) Creates a new File instance by converting the given pathname string to an abstract pathname
File(String parent, String child) Creates a new File instance from the parent pathname string and the child pathname string
File(File parent, String child) Creates a new File instance abstracted from the parent pathname and child pathname strings
public File(URI uri) Creates a new file instance by converting the given File: URI to an abstract pathname.
public class FileLearn {
  public static void main(String[] args) {
      //File(String pathname) : create a new File instance by converting the given pathname String to an abstract pathname.
      File f1 = new File("D:\itcast\java.txt");
      System.out.println(f1);
      //File(String parent, String Child) : create a new File instance from the parent pathname String and child pathname String.
      File f2 = new File("D:\itcast"."java.txt");
      System.out.println(f2);
      //File(File parent, String Child) : create a new File instance by abstracted pathname and child pathname strings from the parent.
      File f3 = new File("D:\itcast");
      File f4 = new File(f3,"java.txt"); System.out.println(f3); System.out.println(f4); }}Copy the code

File class creation function

The method name instructions
public boolean createNewFile() When a file with that name does not exist, a new empty file named by the abstract pathname is created
public boolean mkdir() Create a directory named from this abstract pathname
public boolean mkdirs() Create directories named from this abstract pathname, including any required but non-existent parent directories
// Requirement 1: I'm going to create a file java.txt in E:\Shixf
File f1 = new File("D:\Shixf\HelloWork.txt");
try {
  System.out.println(f1.createNewFile());
} catch (IOException e) {
  e.printStackTrace();
}
System.out.println("-- -- -- -- -- -- -- --");

// Requirement 2: I'm going to create a directory JavaSE under E:\Shixf
File f2 = new File("D:\Shixf\JavaSE");
System.out.println("-- -- -- -- -- -- -- --");

// Requirement 3: I'm going to create a multilevel directory JavaWEB\HTML under E:\Shixf
File f3 = new File("D:\Shixf\JavaWEB\HTML");
System.out.println(f3.mkdirs());
System.out.println("-- -- -- -- -- -- -- --");

// Requirement 4: I'm going to create a file javase.txt under E:\Shixf
File f4 = new File("D:\Shixf\javase.txt");
try {
  System.out.println(f4.createNewFile());
} catch (IOException e) {
  e.printStackTrace();
}
Copy the code

The File class determines and retrieves functions

Judgment function

The method name instructions
public boolean isDirectory() Tests whether File represented by this abstract pathname is a directory
public boolean isFile() Tests whether File represented by this abstract pathname is a File
public boolean exists() Tests whether the File represented by this abstract pathname exists

Access to functions

The method name instructions
public String getAbsolutePath() Returns the absolute pathname string for this abstract pathname
public String getPath() Converts this abstract pathname to a pathname string
public String getName() Returns the name of the file or directory represented by this abstract pathname
public String[] list() Returns an array of file and directory name strings in the directory represented by this abstract pathname
public File[] listFiles() Returns the files in the directory represented by this abstract pathname and an array of File objects for the directory
// Create a File object, I created a File folder under the project contains a helloWord.txt File
File f = new File("File\HelloWord.txt");

//public Boolean isDirectory() : tests whether File is a directory
//public Boolean isFile() : tests whether the abstract pathname represents a File
//public Boolean exists() : specifies whether a File exists
System.out.println(f.isDirectory());
System.out.println(f.isFile());
System.out.println(f.exists());

// Public String getAbsolutePath() : Returns the absolute pathname String for this abstract pathname
//public String getPath() : converts this abstract pathname to a pathname String
//public String getName() : returns the name of the file or directory represented by this abstract pathname
System.out.println(f.getAbsolutePath());
System.out.println(f.getPath());
System.out.println(f.getName());
System.out.println("-- -- -- -- -- -- -- --");

//public String[] list() : returns an array of files and directory names in the directory represented by this abstract pathname
//public File[] listFiles() : returns an array of File objects and files in the directory represented by this abstract pathname
File f2 = new File("D:\Shixf");

String[] strArray = f2.list();
for(String str : strArray) {
    System.out.println(str);
}
System.out.println("-- -- -- -- -- -- -- --");

File[] fileArray = f2.listFiles();
for(File file : fileArray) {
    // System.out.println(file);
    // System.out.println(file.getName());
    if(file.isFile()) { System.out.println(file.getName()); }}Copy the code

File class delete function

The method name instructions
public boolean delete() Deletes the file or directory represented by this abstract pathname
// Requirement 1: Create the helloWord. TXT file in the current module directory
File f1 = new File("File\HelloWord.txt");
try {
  System.out.println(f1.createNewFile());
} catch (IOException e) {
  e.printStackTrace();
}

// Requirement 2: Delete the HelloWord. TXT file from the current module directory
System.out.println(f1.delete());
System.out.println("-- -- -- -- -- -- -- --");

// Requirement 3: create the Shixf directory under the current module directory
File f2 = new File("File\Shixf");
System.out.println(f2.mkdir());

// Requirement 4: Delete the Shixft directory in the current module directory
System.out.println(f2.delete());
System.out.println("-- -- -- -- -- -- -- --");

// Requirement 5: Create a directory under the current module Shixf, and create a file helloWord.txt under the directory
File f3 = new File("File\Shixf");
System.out.println(f3.mkdir());
File f4 = new File("File\Shixf\HelloWord.txt");
try {
  System.out.println(f4.createNewFile());
} catch (IOException e) {
  e.printStackTrace();
}

// Requirement 6: Delete the helloWord.txt directory in the current module
System.out.println(f4.delete());
System.out.println(f3.delete());
Copy the code

  • The difference between absolute and relative paths

    Absolute path: The complete path name that requires no additional information to locate the file it represents. For example: D: \ Shixf \ Java. TXT

    Relative paths: Must be interpreted using information taken from other pathnames. For example: the File \ HelloWord. TXT