File Class
file (file): Files are used to store data .
route (path): Represents a location in a computer , This location can be a folder , It can also be a document .
stay Java in , There is a class called File, This class can represent 【 A file or folder in a computer 】
Be careful ,File The original meaning of this word is document , But in Java in , It's not just about documents , It can also represent a folder . We can go through File Class to operate on a file or folder .
Relative path and absolute path
Absolute path : It's a very detailed path , The absolute path starts with the drive letter . such as : D:\demo\HelloWorld.java
Relative paths : It's a short path , The relative path does not start with a drive letter , The relative path is idea The middle refers to the content under the current project . such as :Demo.java
File Construction method of
File Construction method of class :
- File(String pathname): Create a... Based on the path of a string File object . This path can be a file , It could be a folder .
- File(String parent, String child): Create a... Based on the combination of parent and child paths File object .
HelloWorld.java File path :D:\demo\HelloWorld.java
HelloWorld.java Parent path :D:\demo\
HelloWorld.java Subpath :HelloWorld.java
- File(File parent, String child): Create a... Based on the combination of parent and child paths File object .
- File Represents a file or folder in a computer , This file or folder can exist , It can also be nonexistent .
File The method to get the class
File Class is used to obtain the method
- String getAbsolutePath(): Used to get File The absolute path represented by the object .
- String getPath(): obtain File The path represented by the object .
- String getName(): obtain File Object represents the name of the file or folder .
- long length(): obtain File Object represents the size of the file in bytes . Can't get folder size , If File Object represents a folder , So called length The result of this method is an uncertain result .
If you're creating File When the object is constructed, the relative path is written in the construction method , call getPath What you get is the relative path
If you're creating File When the object is constructed, the absolute path is written in the construction method , call getPath What you get is the absolute path
File Class judgment method
File Class judgment method :
- boolean exists(): Judge File Whether the file or folder represented by the object exists .
- boolean isDirectory(): Decide if it's a folder
- boolean isFile(): Determine if it's a file
File Class creation method
File Create method in class
- boolean createNewFile(): Create a file , If the file already exists , So creation failed . If the creation is successful, return true, Otherwise return to false.
- boolean mkdir(): Create a folder , If the folder already exists , So creation failed . If the creation is successful, return true, Otherwise return to false.
- boolean mkdirs(): Create a folder , If the folder already exists , So creation failed . If the creation is successful, return true, Otherwise return to false.
- mkdir Only single level directories can be created , mkdirs You can create multi-level directories .
- boolean mkdirs(): Create a folder , If the folder already exists , So creation failed
Be careful : If you use mkdirs When creating a file , The superior directory does not exist , Then we will create the superior directory together . - boolean mkdir(): Create a folder , If the folder already exists , So creation failed . Be careful : If you create a file , The superior directory does not exist , Then the creation fails , But there is no error .
- boolean createNewFile(): Create a file , If the file already exists , So creation failed . Be careful : If you create a file , The superior directory does not exist , Then the creation fails . And throw an exception .
File Class deletion method
File Delete method in class
boolean delete(): Delete File The file or folder represented by an object , If the deletion succeeds, return true.
File Medium delete Method if you delete a folder , Only empty folders can be deleted .
Use delete Method to delete the content does not go to the recycle bin
File Class traversal method
Methods for traversal :
- String[] list(): Get all files and folders in the specified directory , And put it into the string array to return .
- File[] listFiles(): Get all the files and folders in the specified directory , And into the File Array returns .
Be careful :
- If File Object represents a file , So called listFiles The result is a null value .
- If File Object representation does not exist in the computer , So called listFiles The result is also a null value
- If File There is nothing in the folder represented by the object , So called listFiles The result is an empty array .