20165330 2017-2018-2 "Java Programming" 5th Week study Summary

Source: Internet
Author: User
Tags assert create directory

Textbook Knowledge Summary seventh Chapter inner class and Exception class
    • Inner class: A member of a class
    1. Outer-Nested classes: Classes that contain inner classes are called outer-nested classes of inner classes
    2. The relationship between the two:
    • The member variables of the inner class are still valid in the inner class, and the methods in the inner class can also call methods in the outer class.
    • class variables and class methods cannot be declared in the class body of an inner class. An outer class can declare an object as a member of an outer class in the class body of a nested class.
    • The inner class is used only by its outer-nested classes, and other classes cannot declare objects with the inner class of a class.
    1. Inner classes can be decorated as static inner classes
    2. The static inner class cannot manipulate instance member variables in an outer-nested class
    3. Internal class corresponding bytecode file name format: Outer nested class name $ internal class name
    • Anonymous class: One subclass removes the post-declaration class body
      • If the parameter of a method is an interface type, you can use the interface name and class body combination to create an anonymous object to pass to the method's parameters, and the class body must override all methods in the interface.
    • Exception class: Some errors that may occur while the program is running
    1. Try-catch statement (handles exceptions) in the following format:
 try {     包含可能发生异常的语句 } catch(ExceptionSubClass e) {     ... } catch(ExceptionSubClass e) {     ... }
    1. Custom exception Classes

A method creates an object with the corresponding exception class at the time of declaration and throws the exception object with the Throw keyword, causing the method to end execution.

The program must invoke a method in the Try-catch block statement that could have an exception, where the catch is capturing the exception object thrown by the Throw keyword.

    • Assertion: Generally used for errors that the program is not prepared to handle by catching exceptions
    1. Syntax format:
assert booleanExpression;assert booleanExpression:messageException;
    1. To enable assertion statements:
java -ea mainClass
    1. Close Assertion statement: When the application is run directly using the Java interpreter, the assertion statement is closed by default

      Tenth. Input, output stream
    • File class
    1. Constructing a File object
    • File (String filename);
    • File (String directorypath,string filename);
    • File (file dir,string filename);
    1. File properties
    2. Create directory: Public boolean mkdir ();
    3. Create file: Public boolean createnewfile ();
    4. Delete current file: File.delete ();
    5. Executing executable files using the runtime class
    • File byte input stream
    1. Steps to use:
      • Set the source of the input stream
      • To create an input stream to a source
      • Let the input stream read the data from the source
      • Close the input stream
    2. Construction Method:

      FileInputStream(String name);FileInputStream(File file);
    3. Close stream: Close ()
    • File byte output stream
    1. Steps to use:
      • Give the destination of the output stream
      • Create an output stream to a destination
      • Let the output stream write data to the destination
      • Turn off the output stream
    2. Construction Method:
FileOutputStream(String name);FileOutputStream(File file);
    1. Close stream: By calling the Close () method
    • Buffered streams: objects created by the BufferedReader and BufferedWriter classes
    1. The construction methods were:
BufferedReader(Reader in);BufferedWriter (Writer out);
    1. BufferedReader Stream is able to read lines of text by ReadLine ()
    • Random stream: Stream created by the Randomaccessfile class
      • Construction Method:
RandomAccessFile(String name,String mode)

The parameter mode takes R (read-only) or RW (read-write), which determines the access rights of the created stream to the file.

RandomAccessFile(File file,String mode)
    1. Stream can call the Getfilepointer () method to get the current read and write location of the stream
    • Array flow
      • byte array stream:
        • Input stream: Bytearrayinputstream (byte[] buf); Bytearrayinputstream (byte[] buf,int offset,int length);
        • Output stream: Bytearrayoutputstream (); Bytearrayoutputstream (int size);
      • Character array flow: CharArrayReader and Chararraywriter classes
    • Data flow
      • Data input stream: DataInputStream
      • Data output stream: DataOutputStream
    • Object Flow
      • Object input stream: ObjectInputStream (InputStream in)
      • Object output stream: ObjectOutputStream (OutputStream out)
    • Parsing files using scanner
    1. Parse files with default delimited tags: Create a Scanner object and point to the file to parse
File file = new File("hello,java");Scanner sc = new Scanner(file);

2. Parse the file with a regular expression as a delimited tag: Create a Scanner object, point to the file to be parsed, and use the Usedelimiter method to specify the regular expression as the delimited tag

File file = new File("hello.java");Scanner sc = new Scanner(file);sc.useDelimiter(正则表达式);
    • Input stream with progress bar: You can use the input class flow Progressmonitorinputstream provided by the javax.swing package.
ProgressMonitorInputStream(Component c,String s,InputStream);
    • File lock
    1. Use the Randomaccessfile stream to establish a stream object to the file, and the read/write property of the object must be RW
RandomAccessFile input=new RandomAccessFile("Example.java","rw");
    1. The input stream call Method Getchannel () obtains a FileChannel object (channel) connected to the underlying file
FileChannel channel=input.getChannel();
    1. The channel calls the Trylock () or lock () method to obtain a Filelock (file Lock) object, which is also known as locking the file
FileLock lock=channel.tryLock();
    1. After a file is locked, if you want to read and write, the write file must have the Filelock object call Release () to free the file lock
lock.release();
Code Hosting

Last week's summary of wrong questions
    1. Parse: Item B: Use + to represent public, use-to represent private. D: The child class points to the parent class.

    2. Parse: A variable declared with a class is called an object.

    3. Parse: C: NullPointerException exception. E item: If the reference is the same, the entity is identical.

    4. Resolution: A: The Cone object Delegate Circle object calls the Getarea () method to calculate the bottom area.

    5. Resolution: Item B: The order of the package, import, class. C: Not too easy, at least to label the package name. D: see the API documentation.

    6. Parsing: An object of type integer contains a field of type int, and the integer is automatically unboxing to int, so the values are equal.

20165330 2017-2018-2 "Java Programming" 5th Week study Summary

Related Article

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.