This is an example of a common Java class called Date. Stop gossiping and return to the truth. Let’s Talk Android!

See officers, write a program is inseparable from data, data has a variety of carriers, the file is one of the carriers. Therefore, we often use files in the program. Java encapsulates files as a File class that allows you to perform operations on files. Let’s take a closer look at the File class.

Files and Directories

Files are usually located in the corresponding directory, we can create files or directories separately. How to create files and directories will be explained later. If we want to distinguish between files and directories, we can use the isDirectory() and isFile() methods to determine whether an object of type File is a directory or a File, but they can only be used if the File or directory already exists, otherwise they always return false.

Create a file

To create a File, we simply use the createNewFile() method of the File class, but we need to create an object of type File as follows: File File = new File(filePath); If the parameter filePath contains directory information, ensure that the directory already exists. If the directory does not exist, create it first. This method can only create files, not directories.

Create a directory

To create a directory, just use the makeDire() method of the File class, and just as for creating a File, we need to create an object of type File.

Delete files and directories

To delete a File, we simply use the delete() method of the File class, and create an object of type File as we did when we created the File. This method can also delete directories.

Get a list of files

Sometimes you need to get a list of files in a directory. You can use the list() or listFiles() methods of the File class. The list() method returns a String array and stores the names of files in the directory into the array. The latter returns an array of type File, which stores the filename and directory of the File in the array as an object of type File.

Guys, this is all part of the theory, but let’s take a look at the actual code to see how file manipulation works.

public class FileOp {

    public static String filePath = "H:"+File.separator+"a.txt";
// public static String filePath = "H:"+File.separator+"t"+File.separator+"a.txt";
    public static void main(String args[]) throws Exception
    {

        File file = new File(filePath);

        if( !file.exists() )    // must check the file state
        {
            if(new File(file.getParent()).exists())
                file.createNewFile();
            else
                System.out.println("Parent dir is not exist");
        }
        else
            System.out.println("File is "+filePath+"is existed");

        if(file.isDirectory())
            System.out.println("it is diectory");
        else
            System.out.println("it is a file");


        filePath =  "H:"+File.separator;        
        String fileList[] = new File(filePath).list();
        if(fileList ! =null)
        {
            System.out.println("--- File name as following:");
            for(String i: fileList)
                System.out.println(i);
        }

        File fileFullList[] = new File(filePath).listFiles();
        if(fileFullList ! =null)
        {
            System.out.println("--- File name with path as following:");
            for(File i: fileFullList)
                System.out.println(i);
        }

        file.delete();

        if(file.exists())
            System.out.println("File deleted Failed");
        else
            System.out.println("File deleted OK"); }}Copy the code

Separator in the above program, there is a File. Separator is not introduced, it represents the directory separator, we all know that Windows and Linux system directory separator is not the same, use this constant to ensure that the separator is the same in different systems, thus improving the portability of the program.

The following is the running results of the program, please refer to:

it is a file
--- File name as following:
a.txt
code
download
--- File name with path as following:
H:\a.txt
H:\code
H:\download
File deleted OK
Copy the code

As you can see from the above program running results, we created an A.txt file in the root directory of disk H, and deleted the file at the end of the program for “environmental protection” considerations. We also list the files and directories on disk H, some with directory information, thanks to the listFile() method.

The File class makes it easy to manipulate Files, but as technology has evolved it has become a bit inconvenient. A new File class has been added in Java SE7 :Files. It’s much more powerful. We’ll leave it out for now, but anyone interested can refer to the DOCUMENTATION in the JDK.

You see officer, about Java common class file operation examples we are introduced here, want to know what examples, and listen to the next decomposition!