Explanation and common methods of File class

All commonly used methods of the File class and their applications
Creation:
createNewFile() creates an empty file at the specified location and returns true if successful. If it already exists, it will not be created and then returns false.
mkdir() creates a single-level folder at the specified location.
mkdirs() creates a multi-level folder at the specified location.
renameTo(File dest) If the target file and the source file are in the same path, then the function of renameTo is to rename. If the target file and the source file are not in the same path, then the function of renameTo is to cut, and it cannot Manipulate folders. 

public boolean createNewFile() // 创建一个新的空的文件
public boolean mkdir() // 创建一个单级文件夹
public boolean mkdirs() // 创建一个多级文件夹
 
//如果只给定一个路径的话,是以当前的 project 作为起始路径的
//如果是\\file则是以当前盘符为起始路径
File file = new File("a");
System.out.println(file.getAbsolutePath()); //根据给定的字符串构造出来一个绝对路径,不检查存在与否
System.out.println(file.getPath());  //得到最后一级目录或者文件的名称
System.out.println(file.getName());
System.out.println(file.length());  //得到文件大小

Delete:
delete() deletes a file or an empty folder. Non-empty folders cannot be deleted. The file is deleted immediately and a Boolean value is returned.
deleteOnExit() deletes files or folders when jvm exits. It is used to delete temporary files and has no return value.
 Determine:
exists() whether the file or folder exists.
isFile() Whether it is a file, or always false if it does not exist.
isDirectory() Whether it is a directory, always false if it does not exist.
isHidden() Whether it is a hidden file or a hidden directory.
isAbsolute() Tests whether this abstract pathname is an absolute pathname.

        //File类对象的创建  文件---文件夹
        File f1=new File("d:/aa/bb/b.txt");
        //1,如果文件的前置目录路径不存在,则抛出异常;2,文件前置的目录路径存在,文件不存在则            
        创建一个空文件,文件存在则不做任何操作
        f1.createNewFile();
        //File类对象的创建   文件夹
        File f2=new File("D:/xx/yy");
        //该方法要求所有前置目录已经存在,才会创建对应的文件夹  zz
        //f2.mkdir();
        //3.该方法可以自动补全所有父目录,并创建文件夹,一般使用该方法!
        f2.mkdir();
 
        //File类对象的删除
        File f3=new File("D:xx/yy/zz");
        File f4=new File("D:xx");
        //删除文件夹必须为空。才可以成功删除成功
        f3.delete();
        //删除文件夹
        f4.delete();

 Get:
getName() Gets the name of the file or folder, excluding the upper-level path.
getAbsolutePath() gets the absolute path of the file, regardless of whether the file exists.
length() gets the size of the file (number of bytes). If the file does not exist, it returns 0L. If it is a folder, it also returns 0L.
getParent() Returns the pathname string of the parent directory of this abstract pathname; if this pathname does not specify a parent directory, returns null.
lastModified() gets the last modified time.

 

Folder related:
static File[] listRoots() lists all root directories (in Windows, it is the drive letter of all systems)
list() returns the file or directory name under the directory, including hidden files. This operation will return null for files.
listFiles() returns files or directory objects (File class instances) in the directory, including hidden files. This operation will return null for files.
list(FilenameFilter filter) returns the subfiles or subdirectories in the specified current directory that meet the filter conditions. This operation will return null for files.
listFiles(FilenameFilter filter) returns the subfiles or subdirectories in the specified current directory that meet the filter conditions. This operation will return null for files.

package ChangYL.FileTest;
 
import java.io.File;
import java.io.IOException;
 
public class FileTest01 {
    public static void main(String[] args) throws IOException {
 
        //练习题:在D盘中aa/bb/cc的目录中创建c. txt的文件,要求 全部使用程序创建
        File f1 = new File("d:/aa/bb/cc");
        f1.mkdirs();
        File f2 = new File(f1,"c.txt");
        f2.createNewFile();
 
 
 
    }
}
 

Guess you like

Origin blog.csdn.net/WJY898989/article/details/128242429