In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What are the methods of exceptions and file classes? in view of this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
* there are two pieces of content to learn today: exceptions and several methods of the File class.
*
* exception:
* concept:
* there are many classes in Java, some are collection classes, and some are exception classes. When there is an exception in the operation of the program
* jdk will encapsulate abnormal information, such as on which line there is a problem, what is the reason, and so on.
* for example, these objects are objects of exception classes. In other words, these objects encapsulate abnormal information!
*
* abnormal architecture:
* like collections, there are many kinds of exception classes. Together, these exception classes are called exception architecture. Where Throwable is the root class of the exception architecture.
* Throwable class, which has two common subclasses, one is Error and the other is Exception. Among them, we don't need to pay attention to Error, so we don't have to learn this, we just need to learn Exception!
*
* there are many kinds of exceptions, which can be divided into two categories: compile-time exceptions and run-time exceptions.
* what is a compile-time exception?
* the exception that occurs during compilation is the compile-time exception. To be exact, if a class itself or one of its ancestors is an Exception class, and no ancestor is RuntimeException, then the class is a compile-time exception.
* what is a runtime exception?
* the exception that occurs at run time is the runtime exception. To be exact, if a class, it or one of its ancestors, is RuntimeException, then the class is the runtime exception.
*
* exception handling: complete mastery is required!
*
* step 1, remember the two ways to handle exceptions!
* the first one: try {
* what is placed here is the code that may have problems
*} catch (exception class name variable name) {
* exception handling code
*}
*
* or
* try {
* what is placed here is the code that may have problems
*} catch (exception class name variable name) {
* exception handling code
*} finally {
* / / the code here, under normal circumstances, must be executed!
*}
*
* if you are required to use this format, just apply it directly. You don't have to watch the video. Try it yourself!
*
* the second category:
* use throws keyword
* format:
* throws exception class name
*
*
* step 2, when you are sure that step 1 is all right, go to step 2.
* Custom exception:
* if you write any class and inherit the Exception class, then this class is a compile-time exception.
* write any class that inherits RuntimeException, and that class is a run-time exception.
*
* if you really don't know how to write a custom exception, apply the following code directly!
*
* class XxxException extends Exception {
* public XxxException (String msg)
* {
* super (msg)
*}
*}
*
* in this way, the above code is only 3 lines. Just change the class name to your favorite class name, and you can create a compile-time exception, which can meet the requirements. You don't have to learn anything anymore, just these 3 lines of code!
*
* then customize a runtime exception class. If you really don't know how to write it, apply the following code directly!
* class XxxException extends RuntimeException {
* public XxxException (String msg)
* {
* super (msg)
*}
*}
* similarly, change the class name of the above code to your favorite class name, and write a runtime exception class, which is done with these three lines of code.
*
* well, friends can see here. Congratulations, you have mastered the custom exception.
*
*
*
* step 3, code that handles multiple exceptions, one try, multiple catch. If you don't know what this means, ask your classmate and your teacher.
* step 4, exception handling of the method of the child parent class with the same name. When a child class overrides the method of the parent class, the step can throw more exceptions than the parent class!
*
*
*
*
* step 5, learn the class File
*
* first of all, File is an ordinary Java class. Learning this class is nothing more than learning what this class can do for us. This class can help us do a lot of things by providing public methods to be called by us.
* so, learn File, that is, to learn the common methods and variables in it.
* so when it comes to calling the methods and variables of this class, you must learn how to call it!
* 1. If the method of this class is static, direct the class name. The method name (parameter list) is called like this
* 2. If the method of this class is non-static, then create the object of this class first, through the object. The method name (parameter list) is called like this
* 3. If the method of this class is a constructor, it should be called by the new keyword in the format of: new File (parameter list)
*
* 4. To know how to call the constructor of File, what does each constructor mean?
* File (String pathname): encapsulate the path pathname into a File object
File (String parent,String child): combine parent and chid to form a new path, and encapsulate the new path into a File object
File (File parent,String child): creates a File object based on the specified parent folder object and child files or folders
*
* 5, create files, create folders
* there is a method in the File class, and the method name is createNewFile (). If you master this method, it is OK!
* there is another method in the File class, the method name is mkdir (). When this method is called, a directory is created. Note that only one-level directory can be created. To create a multi-level directory, use mkdirs ()
* there is another method in the File class, whose name is mkdirs (). It is recommended that you always use this method when creating a directory, because you can create either a first-level directory or a multi-level directory! Just eliminate mkdir () directly!
*
* 6, delete function
* there is a method in the File class. The method name is delete (). You need to know how to call it and what the call does (delete. Remember not to delete the contents of the entire drive letter
*
* 7, the judgment function, each method should be called at least twice to understand the function of the method.
* boolean exists (): judge whether the file object exists
* boolean isFile (): determines whether the file object is a file
* boolean isDirectory (): determines whether the file object is a folder
* boolean isAbsolute (): determines whether the file object is an absolute path
* boolean canRead (): determines whether the file object is readable
* boolean canWrite (): determines whether the file object is writable
* boolean isHidden (): determines whether the file object is hidden
*
*
* 8. Get the function. Each method should be called at least twice to understand the function of the method.
* String getAbsolutePath (): absolute path
* String getPath (): relative path
* String getName (): file name
* long length (): file size in bytes
* long lastModified (): millisecond value of the last modification time.
*
*
* 9. Get the function. Each method should be called at least twice to understand the function of the method.
* public static File [] listRoots (): list the available system file root directories, encapsulate them into File objects, then put these File objects into a File array, and finally return this array
* public String [] list (): put the names of the files in the folder or immediate subfolders into an array of strings, and then return the array
* public File [] listFiles (): encapsulate all files or folders in a specified directory into File objects, then put these File objects into a File array, and finally return this array
*
, /
The answers to the questions about exceptions and file methods are shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
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.