File class details in Java
- 2021-12-11 17:39:56
- OfStack
1. A brief introduction to the File class
Why do you want to learn File class, and what is its role?
Most of the IO stream operations are on files, so
Java
It provides an File class for us to operate files, which represents file names and directory pathnames in an abstract way. This class mainly creates files or directories, searches and deletes files, etc. As far as File is concerned, what it encapsulates is not a real file, but just a pathname. It can exist or it can not exist. In the future, it is necessary to transform the contents of this path into concrete existence through concrete operations.
2. Classification of paths
Absolute path: Path with disk symbol Relative path: Path without disk symbol, default in root directory
3. Construction method
File
(String pathname): Get an File object from 1 path
File
(String parent, String child): The File object is obtained from 1 directory and 1 sub-file/directory
File
(File parent, String child): The File object is obtained from a parent File object and a child file/directory
Sample code:
package org.westos.demo2;
import java.io.File;
public class MyTest2 {
public static void main(String[] args){
// Get an object through a path
File file = new File("E:\\aaa\\");
// Object is obtained by parent class path and subclass name
File file1 = new File("E:\\", "aaa");
// Object is obtained by parent class object and subclass name
File file2 = new File("E:\\");
File file3 = new File(file2, "aaa");
}
}
4. Membership approach
Create functionality:
public boolean createNewFile()
When a file with that name does not exist, a new empty file named by the abstract pathname is created.
public boolean mkdir()
Creates a directory named by this abstract pathname.
public boolean
mkdirs()
Create a directory named by this abstract pathname, including any required but nonexistent parent directories.
Sample code:
public class FileDemo02 {
public static void main(String[] args) throws IOException {
// Demand 1 : I want to be in E:\\itcast Directory 1 Files java.txt
File f1 = new File("E:\\itcast\\java.txt");
System.out.println(f1.createNewFile());
System.out.println("--------");
// Demand 2 : I want to be in E:\\itcast Directory 1 Directory JavaSE
File f2 = new File("E:\\itcast\\JavaSE");
System.out.println(f2.mkdir());
System.out.println("--------");
// Demand 3 : I want to be in E:\\itcast Directory 1 Multilevel directories JavaWEB\\HTML
File f3 = new File("E:\\itcast\\JavaWEB\\HTML");
// System.out.println(f3.mkdir());
System.out.println(f3.mkdirs());
System.out.println("--------");
// Demand 4 : I want to be in E:\\itcast Directory 1 Files javase.txt
File f4 = new File("E:\\itcast\\javase.txt");
// System.out.println(f4.mkdir());
System.out.println(f4.createNewFile());
}
}
Delete function:
public boolean delete() ;
Note:
To delete 1 folder, please note that the folder cannot contain files or folders Delete the Recycle Bin in java
Sample code:
public class FileDemo03 {
public static void main(String[] args) throws IOException {
// File f1 = new File("E:\\itcast\\java.txt");
// Demand 1 Create in the current module directory java.txt Documents
File f1 = new File("myFile\\java.txt");
// System.out.println(f1.createNewFile());
// Demand 2 Delete the java.txt Documents
System.out.println(f1.delete());
System.out.println("--------");
// Demand 3 Create in the current module directory itcast Directory
File f2 = new File("myFile\\itcast");
// System.out.println(f2.mkdir());
// Demand 4 Delete the itcast Directory
System.out.println(f2.delete());
System.out.println("--------");
// Demand 5 Create under the current module 1 Directory itcast, Then create a 1 Files java.txt
File f3 = new File("myFile\\itcast");
// System.out.println(f3.mkdir());
File f4 = new File("myFile\\itcast\\java.txt");
// System.out.println(f4.createNewFile());
// Demand 6 Delete the directory under the current module itcast
System.out.println(f4.delete());
System.out.println(f3.delete());
}
}
Rename function:
public boolean renameTo(File dest):
If the pathname is the same, it is renamed; If the pathnames are different, rename and cut
Judgment function:
public boolean isDirectory():
Determine whether it is a folder
File
0
Determine whether it is a file
public boolean exists():
Determine if a file or folder exists
public boolean canRead():
Determine whether it is readable
public boolean canWrite():
Determine whether it is writable or not
public boolean isHidden():
Determine whether a file or folder is hidden
Sample code:
public class FileDemo04 {
public static void main(String[] args) {
// Create 1 A File Object
File f = new File("myFile\\java.txt");
// public boolean isDirectory() Object represented by this abstract pathname File Is it a directory
// public boolean isFile() Object represented by this abstract pathname File Whether it is a file or not
// public boolean exists() Object represented by this abstract pathname File Does it exist
System.out.println(f.isDirectory());
System.out.println(f.isFile());
System.out.println(f.exists());
}
}
Get functionality:
Basic Acquisition Features:
public String getAbsolutePath (): Gets the absolute path to a file or folder
public String getPath (): Gets the relative path of a file or folder
public String getName (): Gets the file or folder name
public long length (): Get the length, number of bytes, you can get the size of the file for judgment
public long lastModified (): Gets the time of the last modification and returns the millisecond value to judge how many times the file has been modified
Advanced access features:
public String [] list (): Gets an array of the names of all files or folders in a directory
public File [] listFiles (): Gets an array of File objects for all folders in the specified directory, and returns File objects that indicate the methods that can be called File
Sample code:
public class FileDemo04 {
public static void main(String[] args) {
// Create 1 A File Object
File f = new File("myFile\\java.txt");
// public String getAbsolutePath() Returns the absolute pathname string for this abstract pathname
// public String getPath() Converts this abstract pathname to a pathname string
// public String getName() Returns the name of the file or directory represented by this abstract pathname
System.out.println(f.getAbsolutePath());
System.out.println(f.getPath());
System.out.println(f.getName());
System.out.println("--------");
// public String[] list() Returns an array of file and directory name strings in the directory represented by this abstract pathname
// public File[] listFiles() Object for the files and directories in the directory represented by this abstract pathname File Object array
File f2 = new File("E:\\itcast");
String[] strArray = f2.list();
for(String str : strArray) {
System.out.println(str);
}
System.out.println("--------");
File[] fileArray = f2.listFiles();
for(File file : fileArray) {
// System.out.println(file);
// System.out.println(file.getName());
if(file.isFile()) {
System.out.println(file.getName());
}
}
}
}
File filtering interface:
When you want to get it, you will meet the conditions and implement the file filtering interface:
public String[ ] listFiles(new FilenameFilter)