File class for Java's IO stream

Posted by phpmady on Wed, 19 Jan 2022 03:09:22 +0100

Why use the File class?

If you use arrays, all data is lost at the end of the program. Computers can use hard disks to store data permanently, and Java provides a file storage-related class, the File class.

What is an IO stream?

  1. Data can be extracted from local files
  2. You can save data to a local file

Overview and construction of File

  • It is an abstract representation of file and directory path names

  • Files and directories can be encapsulated as objects through File

  • For File, the encapsulation is not a real file, just a path name. It may or may not exist. The future is to translate the contents of this path into concrete existence through specific operations.

Method NameExplain
File(String pathname)Create a new File instance by converting a given path name string to an abstract path name
File(String parent, String child)Create a new File instance from parent and child path name strings
File(File parent, String child)Create a new File instance from parent Abstract path name and child path name strings
public class FileDemo01 {
    public static void main(String[] args) {
        //File(String pathname): 
        //Create a new File instance by converting a given path name string to an abstract path name
        File f1 = new File("C:\\Users\\Harmony\\Desktop\\Test\\a.txt");
        System.out.println(f1);

        //File(String parent, String child): 
        //Create a new File instance from parent and child path name strings
        File f2 = new File("C:\\Users\\Harmony\\Desktop\\Test", "a.txt");
        System.out.println(f2);

        //File(File parent, String child): 
        //Create a new File instance from parent Abstract path name and child path name strings
        File f3 = new File("C:\\Users\\Harmony\\Desktop\\Test");
        File f4 = new File(f3, "a.txt");
        System.out.println(f4);
    }
}

File Creation Function

Method NameExplain
public boolean createNewFile()When a file with that name does not exist, create a new empty file named by the abstract path name
public boolean mkdir()Create a directory named by this abstract path name
public boolean mkdirs()Create a directory named by this abstract path name, including any required but non-existent parent directories

(1) createNewFile() method

public class FileDemo02 {
    public static void main(String[] args) throws IOException {
        File f1 = new File("C:\\Users\\Harmony\\Desktop\\Test\\a.txt");
        boolean result = f1.createNewFile();
        System.out.println(result);
    }
}

Note:

  • If the file exists, the creation using createNewFile() fails, returning false
  • If the file does not exist, it is successfully created using createNewFile(), returning true
  • With the createNewFile() method, only files can be created, regardless of whether the caller has a suffix name or not.

(2) mkdir() and mkdirs()

public class FileDemo02 {
    public static void main(String[] args) throws IOException {
        File f2 = new File("C:\\Users\\Harmony\\Desktop\\Test\\A");
        boolean res = f2.mkdir();
        System.out.println(res);
    }
}

Note: Where the "C:\Users\Harmony\\Desktop\Test" directory already exists! Here's just an A directory created

Note:

  • With the mkdir() method, only single-level folders (directories) can be created, not multilevel folders (directories).
  • You can only create folders (directories) with or without suffix names, not files!
public class FileDemo02 {
    public static void main(String[] args) throws IOException {
        File f2 = new File("C:\\Users\\Harmony\\Desktop\\Test\\A\\B\\C");
        boolean res = f2.mkdirs();
        System.out.println(res);
    }
}

Note:

  • Using the mkdirs() method, you can create either a single-level folder (directory) or a multilevel folder (directory).
  • You can only create folders (directories) with or without suffix names, not files!

Obviously, mkdirs() can be used more extensively than MKDIR ()! Therefore, mkdir() is not applicable in our practical application.

mkdirs() source

    public boolean mkdirs() {
        if (exists()) {
            return false;
        }
        if (mkdir()) {
            return true;
        }
        File canonFile = null;
        try {
            canonFile = getCanonicalFile();
        } catch (IOException e) {
            return false;
        }

        File parent = canonFile.getParentFile();
        return (parent != null && (parent.mkdirs() || parent.exists()) &&
                canonFile.mkdir());
    }

Let's look at the mkdirs source code as above, where the second if calls the mkdir method.

File Delete Function

Method NameExplain
public boolean delete()Delete the file or directory represented by this abstract path name

Topics: Java Back-end