Preface
As we all know, Java provides File class, let us operate on files. Let’s briefly organize the usage of File class. Without further ado, let's take a look at the detailed introduction
1. Basic concepts
File: An abstract representation of file and directory path names, representing files or folders.
2. Construction method
// Create a new File instance based on parent abstract pathname and child pathname string File(File parent, String child) // Create a new File instance by converting the given pathname string to an abstract pathname File(String pathname) // Create a new File instance based on parent pathname string and child pathname string File(String parent, String child) // Create a new File instance by converting the given file:URI to an abstract pathname File(URI uri)
3. Common methods
(1).Create function
// Create the directory specified by this abstract pathname
boolean mkdir()
// Create the directory specified by this abstract pathname, including all required but not existing parent directories
boolean mkdirs()
// Create a new empty file inseparably if and only if there is no file with the specified name of this abstract pathname
boolean createNewFile()
// Create an empty file in the default temporary file directory and generate its name using the given prefix and suffix
static File createTempFile(String prefix, String suffix)
// Create a new empty file in the specified directory and generate its name using the given prefix and suffix string
static File createTempFile(String prefix, String suffix, File directory)
// Construct method 1 File file1 = new File("f://file1"); // Construct method 2 File file2 = new File("f://file1", "file2"); // Construct method 3 File file3 = new File(file2, "file3.txt"); // Create directory and return whether it is created successfully. If the directory exists, return false boolean b1 = file1.mkdir(); System.out.println(b1);// true // Create directory boolean b2 = file2.mkdir(); System.out.println(b2);// true // Create file// Create /file1/file2/file3.txt file under F disk boolean b3 = file3.createNewFile(); System.out.println(b3);// true // Create an empty file and specify a prefix and suffix // Create /file1/file2/file4 under F disk......exe file File.createTempFile("file4", ".exe", file2); Note: The effects of the three construction methods are equivalent, without essential differences; when creating directories mkdir() and mkdirs() methods, when creating directories, if the previous levels of the directory to be created does not exist, they will be created together, and mkdir() can only create single-level directories.
(2). Delete function
// Delete the file or directory represented by this abstract pathname
boolean delete()
//Delete the directory System.out.println(file1.delete()); // false //Delete the file System.out.println(file3.delete());// true
Note: When deleting the directory, it must be guaranteed to be an empty directory.
(3).Judgement function
// Test whether the file or directory represented by this abstract pathname exists
boolean exists()
// Test whether the file represented by this abstract pathname is a directory
boolean isDirectory()
// Test whether the file represented by this abstract pathname is a standard file
boolean isFile()
// Test whether the file specified by this abstract pathname is a hidden file
boolean isHidden()
// Test whether the application can read the file represented by this abstract pathname
boolean canRead()
// Test whether the application can modify the file represented by this abstract pathname
boolean canWrite()
File file = new File("F://hello"); File file2 = new File(file, "blog.txt"); // Determine if (!file.exists()) { // Create directory file.mkdir(); } if (file2.exists()) { // Create file file2.createNewFile(); } // Determine if it is a directory System.out.println(file.isDirectory());// true // Determine if it is a file System.out.println(file.isFile());// false System.out.println(file2.isDirectory());// false System.out.println(file2.isFile());// true System.out.println(file2.isHidden());// false // Determine whether it is a hidden System.out.println(file2.isHidden());// false // Determine whether it is readable System.out.println(file2.canRead());// true // Determine whether it is a writable System.out.println(file2.canWrite());// true Note: You can modify the readability of files independently and view different outputs.
(4). Obtain function
(1). Basic acquisition function
// Return the name of the file or directory represented by this abstract pathname
String getName()
// Returns the absolute pathname form of this abstract pathname
File getAbsoluteFile()
// Return the absolute path name string of this abstract path name
String getAbsolutePath()
// Convert this abstract pathname to a pathname string
String getPath()
// Returns the last time the file represented by this abstract pathname was modified
long lastModified()
File file = new File("F://hello"); File file2 = new File(file, "blog.txt"); // Determine whether the file or directory exists if (!file.exists()) { // Create file.mkdir(); } if (file2.exists()) { // Create file file2.createNewFile(); } // Get the file name or directory name System.out.println(file2.getName()); // blog.txt // Get the absolute path of the file or directory System.out.println(file2.getAbsolutePath()); // F:/hello/blog.txt //Get the path name of the file or directory (absolute path or relative path) System.out.println(file2.getPath());// F:/hello/blog.txt //Get the last time of file or directory modification returns the millisecond value System.out.println(file2.lastModified());// 1463734158963(2). Iterative acquisition function, filter function
// Returns an array of strings that specify files and directories in the directory represented by this abstract pathname
String[] list()
// Returns an array of strings that specify files and directories in the directory represented by this abstract pathname that satisfy the specified filter.
String[] list(FilenameFilter filter)
// Return an array of abstract pathnames that represent files in the directory represented by this abstract pathname
File[] listFiles()
// Return an array of abstract pathnames, which represent files and directories in the directory represented by this abstract pathname that satisfy the specified filter.
File[] listFiles(FileFilter filter)
// Return an array of abstract pathnames, which represent files and directories in the directory represented by this abstract pathname that satisfy the specified filter.
File[] listFiles(FilenameFilter filter)
Illustration: Let's take a look at the things in the F disk first
File file = new File("F://"); // Get the file and directory under the abstract path name String[] s = file.list(); // Filter the file or directory name String[] ss = file.list(new FilenameFilter() { public boolean accept(File dir, String name) { // Return a file or directory name ending in .png// Control the return value to determine whether to add to the array return name.endsWith(".png"); } }); // Enhance the for output for (String string : s) { System.out.print(string + " ");// $RECYCLE.BIN android4.0 dark horse android video...etc} // Enhanced for for (String string : ss) { System.out.print(string + " ");// ic_ptr_loading.png ic_ptr_pull.png ic_ptr_release.png } // Get the file and directory object under the abstract path name File[] files = file.listFiles(); // Get the file and directory object under the abstract path name, add file filtering File[] files2 = file.listFiles(new FileFilter() { public boolean accept(File pathname) { // Determine whether it is a hidden directory return (pathname.isDirectory()&&pathname.isHidden()); } }); // Get the file and directory objects under the abstract path name, add file name to filter File[] files3 = file.listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { // Determine whether it is a file ending in png (new File(dir, name).isFile())&&name.endsWith(".png"); } }); for (File f : files) { System.out.print(f.getName()+" ");//$RECYCLE.BIN android4.0 dark horse android video...etc.System.out.println(); for (File f : files2) { System.out.print(f.getName()+" ");//$RECYCLE.BIN ghos } System.out.println(); for (File f : files3) { System.out.print(f.getName());//ic_ptr_loading.pngic_ptr_pull.pngic_ptr_release.png }(5).Rename function
// Rename the file represented by this abstract pathname
boolean renameTo(File dest)
// Rename the file represented by this abstract path name// boolean renameTo(File dest) File file = new File("f://"); File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { File f = files[i]; // Determine whether it is a file ending in .png if (f.isFile() && f.getName().endsWith(".png")) { // Change the file name, renameTo() receives the File object, where the object does not specify the drive letter boolean b = f.renameTo(new File("pic" + i + ".png")); System.out.println(b); // true // true // true } } Illustration:
Note: When changing the file or directory name, if the renameTo() method parameter object does not specify the disk, the file will be cut to the project directory by default (as can be seen from the screenshot above); if the drive letter is specified, it will be cut to the location according to the specified location. The renameTo() method is equivalent to cutting and renaming.
Note: More methods to view the API
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.