File class
One, File class overview
java.io.The File class is an abstract representation of file and directory names, Mainly
用于文件和目录的创建,查找,删除,重命名文件,查询文件最近的修改时间等操作
.
java files and folders (directories) in the computerEncapsulated as a File class that has nothing to do with the operating system。
Two, File class content organization
1. Constant
Four static member variables:
- 1.static
String pathSeparator
, the path separatorWindows → ;
,Linux → :
- 2.static
char pathSeparatorChar
, path separatorWindows → ;
,Linux → :
- 3.static
String separator
, name separatorWindows → \
,Linux → /
- 4.static
char separatorChar
, name separatorWindows → \
,Linux → /
Precautions:
- 1. It is not recommended to use constants to define the path separator directly, it shouldUse File.separator instead of slash(With cross-platform features, it can adapt to multiple operating systems)
- 2.pathSeparatorChar and pathSeparator with separatorChar 与 separatorThe difference between
the character and the pathSeparatorChar separatorChar and empty "double quotation marks" addition stars and separator pathSeparator
- public static final String pathSeparator =
"" + pathSeparatorChar
;- public static final String separator =
"" + separatorChar
;
2. Construction method
File(String pathname): String is used as a parameter to create a File object.
File(String parent, String child): The first and second string are spliced to create a File object.
File(File parent, String child): The first File object is spliced with the second string to create a File object.
File(URI uri) creates a File object by converting the given file: URI into an abstract path name.
3. Member method
Get file (directory) information
(1) getAbsolutePath method , which returns the file绝对路径 字符串
Method information:
public String getAbsolutePath()
//创建File对象
File file = new File("D:\\我的文件\\test.txt");
//返回此File的绝对路径名字符串。
File fileAbsoluteFile = file.getAbsoluteFile();
System.out.println("文件/文件夹的绝对路径为:"+ fileAbsoluteFile);
(2) getPath method , this File转换为 路径名 字符串
Information method:
public String getPath ()
File classestoString方法
is调用的getPath方法
.
//将此File转换为路径名字符串。
String filePath = file.getPath();
System.out.println("文件/文件夹的构造路径为:" + filePath);
(3) The getName method returns the file represented by this File文件或目录的名称
Method information:
public String getName()
passedFile对象的结尾部分
.
//返回由此File表示的文件或目录的名称。
String fileName = file.getName();
System.out.println("文件/文件夹的名称为:" + fileName);
(4) length method , get the file size,文件大小以字节为单位
Method information:
public long length()
if the file不存在
,返回 0
;
如果File对象表示一个目录
, then返回值是不确定的
long fileLength = file.length();
System.out.println("文件/文件夹的长度(大小)为:" + fileLength);
(5)getParent method , get the name of the parent folder
Method information:
public String getParent()
System.out.println("父文件夹名称为" + file.getParent());
(6) CanRead method , whether the file is readable.
Method information:
public boolean canRead()
System.out.println("文件是否可读?" + file.canRead());
(7) CanWrite method , whether the file can be written
Method information:
public String canWrite()
System.out.println("文件是否可写?" + file.canWrite());
(8) lastModified method , get the last modified date of the file
Method information:
public long lastModified()
用long类型的毫秒数表示
, why is the millisecond value returned? Click here .
System.out.println("文件最后修改日期为:" + file.lastModified());
Determine whether the file (directory) exists
(1) Exists method , whether the file or directory represented by this File actually exists.
Method information:
public boolean exists()
//创建对象
File dir = new File("D:\\我的文件\\");
File file = new File(dir+"test.txt");
//判断file实例是否存在
System.out.println("目录是否存在:" + dir.exists());
System.out.println("文件是否存在:" + file.exists());
(2) isDirectory method , whether this File represents a directory.
Method information:
public boolean isDirectory()
//判断是文件还是目录
System.out.println("dir是文件吗?" + dir.isFile());
(3) IsFile method , whether this File represents a file.
Method information:
public boolean isFile()
passedFile对象的结尾部分
.
//判断是文件还是目录
System.out.println("dir是目录吗?" + dir.isDirectory());
Create file (directory)
(1) The createNewFile method creates a new empty file if and only if the file with this name does not exist yet.
Method information:
public boolean createNewFile()
This method存在IOException异常
requires exception handling (throws, try...catch)
- When the file does not exist, create the file and return true, and return false if it exists without overwriting
- This method can only create files, not folders
- The path to create the file must exist, otherwise an exception will occur
//创建对象
File dir = new File("D:\\我的文件\\");
File file = new File(dir+"test01.txt");
//创建文件
System.out.println("是否创建:" + file.createNewFile());
(2) The mkdir method creates a directory represented by this File.
Method information:
public boolean mkdir()
只能创建单级空文件夹
- If the folder does not exist, create it and return true
- If the folder exists, return false, if the path in the constructor does not exist, it will also return false
//创建对象
File dir = new File("D:\\我的文件\\mkdir");
System.out.println("是否创建:"+dir.mkdir());
(3) The mkdirs method creates the directory represented by this File, including any necessary but non-existent parent directories.
Method information:
public boolean mkdirs() is
available创建多级文件夹
- Can only create folders (mkdir also (for example, xxx.txt will only create folders with this name instead of files))
//创建对象
File dir = new File("D:\\我的文件\\mkdirs");
System.out.println("是否创建:"+dir.mkdirs());
Delete file (directory)
(1) The delete method , delete the file or directory represented by this File.
Method information:
public boolean delete()
can delete files or folders
- Return true if the file or folder is deleted successfully
- There are files or folders in the folder return false
- File or folder path does not exist, return false
- Files deleted by this deletion method will not go to the recycle bin
- [Note] To avoid test use is to delete important files and operate the hard disk directly instead of the "fake deletion" of Windows
//创建对象
File dir = new File("D:\\我的文件\\");
File file = new File(dir+"test.txt");
//文件的删除
System.out.println("test.java是否删除" + file.delete());
//目录的删除
System.out.println("目录是否删除" + dir.delete());