Some commonly used methods in the File class

The File class provides many methods for manipulating files and directories. Listed below are some commonly used methods in the File class:

Basic information about files/directories:

String getName(): Get the name of the file or directory.
String getPath(): Get the path of a file or directory.
boolean exists(): Determine whether the file or directory exists.
boolean isFile(): Determines whether the current File object represents a file.
boolean isDirectory(): Determines whether the current File object represents a directory.
long length(): Get the size of the file (number of bytes).
long lastModified(): Get the last modification time of the file or directory.

File/directory operations:

boolean createNewFile(): Create a new empty file.
boolean mkdir(): Create a single-level directory.
boolean mkdirs(): Create multi-level directories.
boolean delete(): Delete a file or directory.
boolean renameTo(File dest): Rename the file or directory to the specified destination file or directory.

Directory content query:

String[] list(): Returns an array of names of all files and subdirectories in the directory.
File[] listFiles(): Returns an array of File objects for all files and subdirectories in the directory.
File[] listFiles(FileFilter filter): Returns an array of File objects for files and subdirectories in the directory that meet the filter conditions.
File[] listFiles(FilenameFilter filter): Returns an array of File objects in the directory for files and subdirectories that meet the specified file name filter conditions.

Path operations:

String getAbsolutePath(): Get the absolute path of a file or directory.
String getParent(): Gets the parent directory path of a file or directory.
File getParentFile(): Gets the File object of the parent directory of the file or directory.
The above only lists some commonly used methods. The File class also provides other methods for determining permissions, traversing directory trees and other operations.

Create an example

If the system cannot find the path specified when creating a file, this usually means that one or more directories on the path specified do not exist.

When creating a file using a relative path, Java will resolve the path in the current working directory. If a directory in the relative path does not exist, an error will be reported when creating the file.

Make sure you have created all required directories before creating the file. You can use the mkdirs() method to create multi-level directories.

Here is an example showing how to create a file and its required directories:

import java.io.File;
import java.io.IOException;

public class CreateFileWithDirectoriesExample {
    
    
    public static void main(String[] args) {
    
    
        String filePath = "relative/path/file.txt";
        File file = new File(filePath);

        // 获取父目录
        File parentDir = file.getParentFile();

        // 创建父目录及其所有上级目录
        if (parentDir != null && !parentDir.exists()) {
    
    
            boolean created = parentDir.mkdirs();
            if (created) {
    
    
                System.out.println("父目录创建成功!");
            } else {
    
    
                System.out.println("父目录创建失败!");
            }
        }

        try {
    
    
            boolean created = file.createNewFile();
            if (created) {
    
    
                System.out.println("文件创建成功!");
            } else {
    
    
                System.out.println("文件已存在或创建失败!");
            }
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}

In the above code, we first obtain the parent directory of the file, and then use the mkdirs() method to create all superior directories. Next, we create the file using the createNewFile() method.

By creating the parent directory before creating the file, you ensure that all directories in the path exist and that the file can be created successfully.

Please note that when using the mkdirs() method to create a directory, it will not throw an error or overwrite the existing directory if it already exists.

If you still have problems, you can check that the path is correct and make sure you have sufficient permissions to create files in the selected directory.

Guess you like

Origin blog.csdn.net/wang121213145/article/details/131602043