In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will introduce in detail for you "what is the File class in Java IO", the content steps are clear and detailed, the details are handled properly, and the editor updates different knowledge points every day, I hope this article "what is the File class in Java IO" can give you unexpected gains, please follow the editor's ideas slowly in depth, the specific contents are as follows, to harvest new knowledge together.
File class: an abstract representation of file and directory pathnames.
Note: the File class can only manipulate the properties of the file, not the contents of the file.
1. Fields of File class
We know that the path separator is different from platform to platform.
①, for UNIX platforms, the absolute pathname is always prefixed with "/". The relative pathname does not have a prefix. The abstract pathname that represents the root directory has a prefix "/" and an empty name sequence.
②, for Microsoft Windows platforms, the prefix of the pathname containing the drive specifier consists of a drive letter followed by ":" or, if the pathname is absolute, followed by "\". The UNC pathname is prefixed with "\"; the hostname and share name are the first two names in the name sequence without the relative pathname of the specified drive without prefix.
So to shield the separator differences between platforms, when we construct the File class (see the second point below), we can use the fields provided for us by the above Java.
Then we can see:
File.pathSeparator refers to a delimiter that separates consecutive path strings.
File.separator is used to separate directories in the same path string
2. The construction method of File class.
See the following example for how to use the above construction method:
/ / do not use the delimiter field provided by Java. Note: this is only valid on Windows platforms: File F1 = new File ("D:\ IO\\ a.txt"); / / use the delimiter File f2 provided by Java ("D:" + File.separator+ "IO" + File.separator+ "a.txt"); System.out.println (F1) / / output D:\ IO\ a.txt System.out.println (f2); / / output D:\ IO\ a.txt / / File (File parent, String child) / / create a new File instance from the parent abstract path name and child path name strings. File f3 = new File ("D:"); File f4 = new File (f3, "IO"); System.out.println (f4); / / D:\ IO / / File (String pathname) / / creates a new File instance by converting the given pathname string to an abstract pathname. File f5 = new File ("D:" + File.separator+ "IO" + File.separator+ "a.txt"); System.out.println (f5); / / D:\ IO\ a.txt / / File (String parent, String child) / / create a new File instance from the parent path name string and the child path name string. File f6 = new File ("D:", "IO\\ a.txt"); System.out.println (f6); / / D:\ IO\ a.txt3, common methods of File class ①, creation method
1.boolean createNewFile () does not exist return true exists return false
2.boolean mkdir () creates a directory, but fails if the previous directory does not exist
3.boolean mkdirs () creates a multi-level directory, which will be created automatically if the previous directory does not exist.
②, deletion method
1.boolean delete () deletes a file or directory. If it represents a directory, it must be empty before it can be deleted.
Delete the 2.boolean deleteOnExit () file after using it.
③, judgment method
1.boolean canExecute () determines whether the file is executable or not
2.boolean canRead () determines whether the file is readable
3.boolean canWrite () determines whether the file is writable or not
4.boolean exists () determines whether a file or directory exists
5.boolean isDirectory () determines whether the path is a directory
6.boolean isFile () determines whether it is a file or not
7.boolean isHidden () determines whether it is a hidden file.
8.boolean isAbsolute () can also determine whether the absolute path file does not exist.
④, acquisition method
1.String getName () gets the name of the file or directory represented by this path
2.String getPath () converts this pathname to a pathname string
3.String getAbsolutePath () returns the absolute form of this abstract pathname
4.String getParent () / / return null if there is no parent directory
5.long lastModified () / / get the time of the last modification
6.long length () returns the length of the file represented by this abstract pathname.
7.boolean renameTo (File f) renames the file represented by this abstract pathname.
8.File [] liseRoots () / / get the machine drive letter
9.String [] list () returns an array of strings naming the files and directories in the directory represented by this abstract pathname.
10.String [] list (FilenameFilter filter) returns an array of strings naming the files and directories in the directory represented by this abstract pathname that satisfy the specified filter.
/ / File (File parent, String child) / / create a new File instance from the parent abstract pathname and child pathname strings. File dir = new File ("D:" + File.separator+ "IO"); File file = new File (dir, "a.txt"); / / determine whether dir exists and represent a directory if (! (dir.exists () | | dir.isDirectory () {/ / if dir does not exist, create this directory dir.mkdirs () / / create the a.txt file file.createNewFile () based on the directory and file name;} / / returns the name of the file or directory represented by this abstract pathname. This is just the last name in the path name sequence. If the sequence of pathnames is empty, an empty string is returned. System.out.println (file.getName ()); / / a.txt / / returns the pathname string of the parent null of this abstract pathname, or null if the pathname is not named as the parent directory. System.out.println (file.getParent ()); / / D:\ IO / / converts this abstract pathname to a pathname string. The result string uses default name-separator character to separate the names in order of name. Some skills of System.out.println (file.getPath ()); / / D:\ IO\ a.txt4, File
①, print the contents of all folders and folders under a given directory
Public static void getFileList (File file) {/ / first-level subdirectory File [] files = file.listFiles (); for (File f:files) {/ / print directories and files System.out.println (f); if (f.isDirectory ()) {getFileList (f);}
Test:
Public static void main (String [] args) throws Exception {File f = new File ("D:" + File.separator+ "WebStormFile"); getFileList (f) } Thank you for reading this. I hope you have the most profound understanding of the key question "what is the File class in Java IO" from the practical level, and you still need to practice and use it on your own. If you want to read more related articles, welcome to follow the industry information channel!
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.