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

Source: Internet
Author: User
Tags assert readline

20165230 2017-2018-2 "Java program Design" 5th Week study summary Textbook study contents seventh chapter inner class and exception class inner class and outer nested class

You can define another class in the class, that is, the inner class

The class containing the inner class is an outer class of the inner class

    • A member variable of an outer-nested class is valid in an inner class that declares an object using an inner class in the body
    • class variables and class methods cannot be declared in the class body of an inline class, methods in an inner class can invoke methods of an outer-nested class
    • Inner classes are used only for outer-nested classes
    • An inner class can be used as an inner class to static 外嵌类.内部类 access an inner class, and an instance member variable in an outer nested class cannot be manipulated

      Anonymous class
    • Create anonymous class objects related to subclasses:

      new 类(){匿名类类体};
    • Creating anonymous class objects related to interfaces

new 接口名(){    实现接口的匿名类类体};
    • Anonymous class must be an internal class
    • Static member variables and static methods are not declared in the anonymous class body
    • A reference to an anonymous object is often passed to a parameter
Exception class
    • Using throw an exception object that throws a exception subclass to indicate that an exception occurred
    • try-catchStatement
    try {    包含可能发生的异常语句//行为发生异常,就是方法调用}catch(Exception e){···}

The exception class in the catch parameter is a exception subclass and cannot have a parent-child relationship between subclasses

    • Custom exception Classes

Declares a method with throws a keyword that declares several exceptions to be produced, and gives specific actions to produce an exception.

Assertion
assert boolean逻辑判断语句;assert  boolean逻辑判断语句:执行语句

When the Boolean is false, the program stops at the assertion

Tenth. Input, output stream
    • All input streams are classes InputStream or Reader subclasses of abstract class
    • All output streams are classes OutputStream or Writer subclasses of abstract class

      The file class creates three constructor methods for a File object
File(String filename);File(String directoryPath,String filename);File(File dir,String filename);
Directory

By File f = new File(name/path) Creating an object

    • By f.mkdir(); Creating a directory
    • To list files in a directory:

1.f.list()

String [] filelist = f.list();//用字符串形式File [] filelist = f.listFiles();//用File对象形式

2. List files of the specified type:f.list(FilenameFilter obj)

    • An object that passes a class to an interface FilenameFilter, and the parameter obj can be accept(File dir,String name called back to verify that the file meets the requirements
Create a delete and run executable file
f.creatNewFile();//创建f.delete();//删除Runtime ec = Runtime.getRuntime()//通过调用类方法创建对象ec.exec(String command)//ec调用方法打开机器上的可执行文件
File byte input/output stream 1. Give the source/destination of the input/output stream

File f = new File(name/path);

2. Create an input/output stream pointing to the source/destination
    • Point to Source

      FileInputStream(String name);FileInputStream(File file);
    • Point to Destination
      FileOutputStream(String name); FileOutputStream(File file);
      FileOutputStream(String name,boolean append); FileOutputStream(File file,boolean append);//当append的值为false时将刷新所指向的文件,否则将从文件末尾开始写入

Must be try-catch created in the Try block section of the statement

3. Reading bytes using the input/output stream
    • Input stream

      int read()//读取单个字节int read(byte b[])//读取b.length个字节int read(byte b [],int off,int len)//从第off个位置起读取len个字节
    • Output stream

      void write(int n)//写入单个字节void write(byte b[])//写入字节数组void write(byte b [],int off,int len)//从第off个位置起写入len个字节
      4. Close the stream

Use close() method

file character input/output stream

The data is processed in a character unit.

FileReader(String filename);FileReader(File filename);
FileWriter(String filename);FileWriter(File filename);FileWriter(String name,boolean append);FileWriter(File file,boolean)
Buffered streams

BufferedReaderStream and BufferedWriter , the source and destination of both must be character input stream and character output stream,
cannot be directly connected to the destination/source

Construction method
BufferedReader(Reader in);BufferedWriter(Writer out);

An object that passes a reader subclass to BufferedReader (connects two streams together) and then reads the file by line with the ReadLine () method

FileReader inOne = new FileReader("student.txt");BufferedReader inTwo = BufferedReader(inOne);String strLine = inTwo.readLine();
    • Bufferwriter a method to write a return line characternewLine()

      Random stream
    • A stream created by the Randomaccessfile class is called a random stream, and a stream created with that class can be either a source or a destination.
    • Construction Method:
      RandomAccessFile(String name,String mode)

Mode take R (read only), take RW (can read and write)

    • When you point to a file, the file is not refreshed

    • Method seek(long a) to determine the number of bytes written and read at the beginning of the file, can be replaced or added

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

Input stream

CharArrayReader(char[] buf);CharArrayReader(char[] buf,int offset,int length);

Output stream

CharArrayWriter();CharArrayWriter(int size);
Data flow
    • Allows the program to read Java raw data in a machine-independent style
    • Construction method
DataInputStream(InputStream in)//创建输入流指向由参数in指定的底层输入流DataOutputStream(OutputStream out)//创建输出流指向由参数in指定的底层输出流
Object Flow
    • Construction method

      ObjectOutputStream(OutputStream out)//该指向是一个输出流对象ObjectInputStream(InputStream in)//该指向是一个输入流对象
    • When using object flow to write to or read into an object, ensure that the object and the object's member variables are serialized: The class implements the serializable interface

      Serialization and object cloning
    • Writes an object to the destination that the object's output stream points to, and then the destination as the source of an object input stream, which can be cloned

      Parsing files using scanner
    • Parsing files with default split marks

File file = new File("hello.java");Scanner sc = new Scanner(file);
    • Parsing a file with a regular expression as a split tag

      File file = new File("hello.java");Scanner sc = new Scanner(file);sc.useDelimiter(正则表达式);
      File dialog box
    • JFileChooser()To create a modal file dialog box that is not initially visible
    • Call two methods to make a dialog visible

int ShowSaveDialog(Component a);int ShowSaveDialog(Component a);
    • OK, cancel or Close button, return constant
JFileChooser.APPROVE_OPTIONJFileChooser.CANCEL_OPTION
    • File type is the type required by the user
FileNameExtensionFilter filter = new FileNameExtensionFilter(7"图像文件","jpg","gif");//创建对象chooser.setFileFilter(filter);//对话框调用方法来设置对话框默认打开或显示的指定类型
Input stream with progress bar
    • javax.swingInput stream in Package: Progressmonitorinputstream

      ProgressMonitorInputStream(Conmponent c,String s,InputStream)//进度条在参数c指定的组件的正前方显示,若为null,在屏幕正前方显示
      File lock
RandomAccessFile input = new RandomAccessFile("Example.java","rw");//创建指向文件的流对象,读写属性必须是rwFileChannel channel=input.getChanner();//调用方法获得连接到底层文件的对象(信道)FileLock lock = channel.tryLock();//文件加锁,加锁后将禁止程序对文件进行操作lock.release();//释放文件锁
Problems in teaching materials learning and the solving process
  • Question 1: Why are class variables and class methods not declared in the class body of an inner class?
  • Problem 1 Solution: Learn the following knowledge through online inquiry

    Static variables are to occupy memory, at compile time as long as is defined as a static variable, the system will automatically allocate memory to him, and the internal class is compiled in the host class compile, that is, must have a host class exists before the internal class, which is at compile time for the static variable allocation of memory generated a conflict, Because the system executes: run the internal class, the static variable memory allocation, the host class, and the static variables of the inner class are generated before the inner class, which is obviously not possible, so you cannot define static variables!

  • Question 2:p289 What does the statement mean in the code of the page "新年快乐".getBytes() ?
  • Problem 2 Solution: Refer to the eighth chapter of the content, see p185 page Knowledge Point, String is a class, there are many methods, such as here is called the GetBytes () method

    public byte [] getBytes()Stores the character sequence of the current string object into a byte array using the platform's default character encoding, and returns a reference to the array.

  • Question 3:p292 What does the class of the code in the page StringTokenizer mean?
  • Question 3 Solution: Supplemental Learning Book on p191 page about StringTokenizer class knowledge.
  • Question 4: What is a regular expression
  • Problem 4 Solution: Find out through books

    A regular expression is a sequence of characters of a string object that contains characters that have special meanings, which are called metacharacters in regular expressions.
    In a regular expression, you can enclose several characters in square brackets to represent a meta character that represents any one of the character in square brackets

    Problems in code debugging and the resolution process
  • Issue 1: When you import a code file with idea, you are prompted to compile an error
  • Issue 1 Solution: can be forcibly compiled
  • Question 2: In the code in the p284 page, there is a method that ends with the specified name, is there a method that starts with the name of the specified change?
  • Problem 2 Solution: On-line query learned that there is a StartsWith method, name.startswith(extendName) can be set to return a file of the specified type
  • Issue 3: When debugging Example10_3, the executable file in the example cannot be run because it is not in this virtual machine
  • Issue 3 Solution: Try to open the Gedit editor that is already in the virtual machine by querying its destination /usr/bin , so the modified path is:

    File  file=new File("/usr/bin","gedit");
    can open gedit file successfully
  • The problem 4:example10_5 run out of the same results as the book, the book is annotated with 0,8,22. But running in a virtual machine gets to be 0,12,26

  • Problem 4 Solution: Online query to know that the Chinese characters are 3 bytes in Linux

    Ubuntu uses UTF-8 encoding by default, and this encoded character occupies three bytes

  • Issue 5: Running a virtual machine with a flower screen phenomenon
  • Problem 5 Solution: Read the online query to learn the resolution can be modified

Code Hosting

Other (sentiment, thinking, etc., optional)
    • This week the study task is heavier, in the study tenth chapter, because lacks the eighth chapter knowledge, learns to be difficult, the understanding of the code is not deep enough, encounters does not understand only then forwards the corresponding knowledge point to learn the side to supplement, the efficiency is not high. Should
      After encountering the knowledge points that do not understand, will be related to the knowledge of the system to complement, and then back to learn, or the more to the back the more strenuous
Learning progress Bar
lines of code (new/cumulative) Blog Volume (Add/accumulate) Learning Time (new/cumulative) Important Growth
Goal 5000 rows 30 Articles 400 hours
First week 13/13 1/30 19/19
Second week 426/426 3/30 12/12
Third week 562/562 4/30 15/15
Week Four 1552/1958 5/30 16/16
Week Five 1123/3086 6/30 14/14
Resources
    • "Java2 Practical Course (Fifth Edition)" Learning Guide

    • Why a static variable cannot be defined in a Java member inner class
    • Regular expressions
    • Detailed usr directory in Linux system
    • Intellj Idea Easy Tutorial
    • Debugging Java programs with JDB
    • The Chinese of the Linux system takes up a few bytes

20165230 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.