IO stream---Use of File class

Preface

1. An object of the File class represents a file or folder

2. The File class is declared under the IO package

3. The File class involves file creation, deletion, renaming, modification time, file size, etc.; it does not involve data writing and reading operations. If you need to read or write data, you must use IO streams To be done

4. Subsequent Objects of the File class are often passed as parameters to the constructor of the stream, indicating where to read or write

One, create an object of the File class

Relative path: Compared to a path, the specified path

Absolute path: the path of the file or file directory including the drive letter

//构造器一:
File(String pathName)
//构造器二:
File(String parent, String child)
//构造器三:
File(File parent, String child)

2. Common methods

1. How to obtain

获取绝对路径
public String getAbsolutePath()
    
获取路径
public String getPath()
    
获取名称
public String getName()
    
获取上层文件目录路径,没有返回null
public String getParent()
    
获取文件长度(字节数),不能获取目录长度
public Long length()
    
获取最后一次修改时间
public Long lastModifide()
    
获取指定目录下所有文件和文件目录的名称数组
public String[] list()

获取指定目录下所有文件和文件目录的File数组
public File[] listFiles()

2. Judgment function

判断是否是文件目录
public boolean isDirectory()
    
判断是否是文件
public boolean isFile()
    
判断是否存在
public boolean exists()
    
判断是否可读
public boolean canRead()
    
判断是否可写
public boolean canWrite()
    
判断是否隐蔽
public boolean isHidden()

3. Create function

创建文件。若文件存在,则不创建,返回false
public boolean createNewFile()
    
创建一级文件目录,若存在,则不创建,返回false
public boolean mkdir()
    
创建多级文件目录
public boolean mkdirs()

4. Delete function

删除文件或者文件夹
public boolean delete()

Guess you like

Origin blog.csdn.net/weixin_45321793/article/details/110392861