In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "Java File class common methods and file filter instance analysis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
File class
The File class is used to encapsulate a path, which can be an absolute path starting from the system drive letter or relative to the current directory. The path encapsulated inside the File class can point to a file or a directory, and some general operations for these directories or files are provided in the File class.
Common construction methods of File class
File (String pathname) / / creates a new File object by specifying a file path of type string
File (String parent,String child) / / creates a File object based on a specified parent path of type string and a subpath of type string
File (File parent,String child) / / creates a File object based on the parent path of the specified File class and the subpath of the string type
View the corresponding information of the file package JS; import java.io.File; public class XX {public static void main (String [] args) {File file=new File ("example.txt"); / / get the file name System.out.println ("file name:" + file.getName ()); / / get the relative path of the file System.out.println ("relative path of the file:" + file.getPath ()) / / get the absolute path of the file System.out.println ("absolute path of the file:" + file.getAbsolutePath ()); / / get the parent path of the file System.out.println ("parent path of the file:" + file.getParent ()); / / determine whether the file is readable System.out.println (file.canRead ()? " File readable ":" File unreadable "); / / determine whether the file is writable System.out.println (file.canWrite ()?" File writable ":" File not writable "); / / determine whether it is the same file System.out.println (file.isFile ()?" Is a file ":" not a file "); / / determine whether it is the same directory System.out.println (file.isDirectory ()?" The file is a directory ":" the file is not a directory "); / / get the last modification time System.out.println of the file (" last modification time is: "+ file.lastModified ()); / / get the file size System.out.println (" file size is: "+ file.length () +" bytes ") / / whether the file System.out.println was deleted successfully ("whether the file was deleted successfully" + file.delete ()) }}
Traverse the files in the directory
The list () method allows you to traverse all file names in a specified directory
Package JhiShi; import java.io.File; public class Example01 {public static void main (String [] args) throws Exception {File file=new File ("C:\\ Users\\ lenovo\\ IdeaProjects\\ java se"); if (file.isDirectory ()) {String [] names=file.list (); for (String name:names) {System.out.println (name) }
First, through the isDirectory () method in the File class to determine whether the path points to an existing directory, call the list () method if it exists, and get the String type array names, which contains the file names of all the files in this directory, and then loop through the names of the array, printing out the name of each file in turn.
File filter package JhiShi; import java.io.File; import java.io.FilenameFilter; public class Example02 {public static void main (String [] args) throws Exception {File file=new File ("C:\\ Users\\ lenovo\\ IdeaProjects\\ java se"); FilenameFilter filter=new FilenameFilter () {@ Override public boolean accept (File dir, String name) {File currFile=new File (dir,name) If (currFile.isFile () & & name.endsWith (".txt")) {return true;} else {return false;}; if (file.exists ()) {String [] lists=file.list (filter) For (String name:lists) {System.out.println (name);}
Traverse the subdirectory package JhiShi; import java.io.File; public class Example03 {public static void main (String [] args) throws Exception {File file=new File ("C:\\ Users\\ lenovo\\ IdeaProjects\\ java se"); fileDir (file);} public static void fileDir (String [] args) {File [] files=dir.listFiles () For (File file:files) {if (file.isDirectory ()) {fileDir (file);} System.out.println (file.getAbsoluteFile ());}
Through a static method fileDir (), used to receive a File object that represents a directory, first call the listFile () method to store all the subdirectories and files under the directory into an array files of type File, then traverse the array files, and judge the traversal object, if it is a directory, call the fileDir () method again for recursion, and if it is a file, output the path of the file.
Delete files and directories package JhiShi; import java.io.File; public class Example03 {public static void main (String [] args) {File file=new File ("C:\\ ABC"); deleteDir (file);} public static void deleteDir (String [] args) {if (dir.exists) {File [] files=dir.listFiles () For (File file:files) {if (files.isDirectory ()) {deleteDir (file);} else {file.delete ();}} dir.delete ();}
Define a static method deleteDir () to delete a directory to receive a parameter of type File, call the listFiles () method to save all subdirectories and files under this directory into an array files of type File, and then traverse files. If the directory is recursive from a new call to the deleteDir () method, if it is a file, directly call the delete () method of File to delete, when all the files in this directory are deleted. Then delete this directory.
Note: the Java delete directory is deleted directly from the virtual machine rather than the Recycle Bin. Once deleted, it cannot be recovered.
This is the end of the content of "Java File class common methods and file filter instance analysis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.