java019 exception, File class

Source: Internet
Author: User
Tags throwable try catch

Classification of exceptions:
* ERROR: called the wrong class. Represents a Java runtime system internal error or resource exhaustion error that cannot be resumed by the modification program itself. For example: Server outage, database crashes, etc.
* Exception: Called an exception class that represents an error that the program itself can handle.

Inheritance system
* Throwable
* Error
* Exception
* RuntimeException//except the runtime exception is a compile-time exception, which is usually the programmer's own error

How the JVM handles exceptions by default:
* A: Handle the problem yourself, then continue running, corresponding to the a code below
* B: There is no treatment for itself, only the JVM that calls main is processed, and if the JVM has a default exception handling mechanism, the exception is processed. And the exception's name, the exception information. The location of the exception is printed on the console, and the program is stopped running corresponding to the B code below
Two ways to handle exception
* a:try...catch...finally
* Try Catch
* Try Catch finally
* Try Finally
* B:throws

Differences between compile-time exceptions and run-time exceptions:
* All instances of the RuntimeException class and its subclasses are called run-time exceptions, other exceptions are compile-time exceptions
* Compile-time exception, Java program must display processing, or the program will be an error, can not be compiled
* Run-time exception, no display processing, and can be treated as a compile-time exception

Several common methods of throwable
* A:getmessage ()
* Get exception information, return string.
* B:tostring ()
* Gets the exception class name and exception information, and returns a string.
* C:printstacktrace ()
* Gets the exception class name and exception information, and where the exception appears in the program. return value void.

Throws way to handle exceptions: when defining a functional method, you need to expose the problem to the caller to handle it, and then identify the method by throws.

Throw: In the function method inside a certain situation, the program can not continue to run, need to jump, with throw to throw the exception object.

The difference between throws and throw
* A:throws
* Used after the method declaration, followed by the exception class name
* Can be separated by commas with multiple exception class names
* Indicates a thrown exception, handled by the caller of the method
* B:throw
* Used in the body of the method, followed by the name of the exception object
* Only one exception object name can be thrown
* Indicates thrown exception, handled by the statement in the method body

Finally features: Unless the JVM exits (such as system.exit (0)) before the program executes to finally, the statement body that is finally controlled is bound to execute
* Role: Freeing resources

Exception considerations
* A: When a subclass overrides a parent class method, the child class's method must throw the same exception or subclass of the parent exception. (father is broken, son can not be worse than father)
* B: If the parent class throws more than one exception, when the subclass overrides the parent class, it can only throw the same exception or a subset of his, the subclass cannot throw exceptions that the parent class does not have
* C: If the overridden method does not throw an exception, then the method of the subclass must not throw an exception, if there is an exception in the subclass method, then the subclass can only try, not throws

How to use exception handling
* Principle: If the function can be handled within the problem, with a try, if not handled, referred to the caller, which is used throws
Difference
* Subsequent programs need to be run on try
* Subsequent programs do not need to continue to run throws
* If the JDK does not provide a corresponding exception, a custom exception is required.

File class:
* file should be called a path
* File path or folder path
* Path is divided into absolute path and relative path
* Absolute path is a fixed path, starting with the drive letter
* Relative path relative to a location, under Eclipse refers to the current project under the DOS View API refers to the current path!
* Abstract representation of file and directory path names

Construction method
* File (String pathname): Gets the file object based on a path
* File (string parent, String child): Gets the file object based on a directory and a sub-file/directory
* File (file parent, String child): Gets the file object based on a parent file object and a sub-file/directory
Creating features
* Public boolean CreateNewFile (): Create file If such a file exists, you do not create a
* Public boolean mkdir (): Create folder if such a folder exists, you do not create a
* Public boolean mkdirs (): Create a folder, if the parent folder does not exist, will help you create it
Precautions:
* If you create a file or folder where you forgot to write the drive path, the default is under Project path.

Renaming and deleting features
* Public boolean Renameto (file dest): Renaming files to the specified file path
* Public Boolean Delete (): Delete file or folder
Renaming considerations
* If the path name is the same, it is renamed.
* If the path name is different, it is renamed and cut.
Removal considerations:
* Delete in Java does not go to recycle Bin.
* To delete a folder, please note that the folder cannot contain files or folders

Judging function
* Public boolean isdirectory (): Determine if it is a directory
* Public boolean isfile (): Determines whether the file is
* Public Boolean exists (): Determine if there is
* Public boolean CanRead (): Determines whether it is readable
* Public boolean CanWrite (): Determines if writable
* Public boolean Ishidden (): Determines whether to hide

Get features
* Public String GetAbsolutePath (): Gets the absolute path
* Public String GetPath (): Get Path
* Public String GetName (): Get Name
* Public long Length (): Gets the length. Bytes//can only get the length of the file, cannot get the length of the folder
* Public Long LastModified (): Gets the last modified time, millisecond value
* Public string[] List (): Gets an array of names for all files or folders under the specified directory
* Public file[] Listfiles (): Gets the file array of all files or folders under the specified directory

File name filter:
* Public string[] List (filenamefilter filter)
* Public file[] Listfiles (filefilter filter)
Use of the file name filter
* Requirements: Determine if the e-disk directory has a suffix. jpg file, and if so, output the file name
SOURCE Analysis
* Source of List () method with file name filter

java019 exception, File class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.