CGBTN2108-DAY12 summary review

Keywords: Java regex File BigDecimal

DAY12 review

1. Regular expression

Function: take the rules we specify to judge whether the data conforms to this rule

  1. Specify rule: String regex = "[0-9]{17}[0-9X]";
  2. Compare the data with the rule: input.matches(regex) – > if it matches, the matches method returns true

2. Packaging

  1. There are only two types of data types in Java: 8 basic types and reference types
  2. Wrapper class is one of the reference types. There are 8 kinds of wrapper classes that correspond to the basic type one by one
  3. Basic types can only store values of their own types and have no other additional functions
  4. Packaging type is the packaging of basic types and provides rich functions. Packaging type is the expansion of basic types
  5. How the wrapper type Integer is created:
    1)Integer i1 = new Integer(5); There is no efficient effect. New once creates a wrapper class object
    2)Integer i2 = Integer.valueOf(5); It has an efficient effect. Only when the data is in the range of - 128 ~ 127 can it have an efficient effect
    3)Integer i3 = 5; Automatic boxing: the compiler will box int type 5 into integer type, and the underlying method: valueOf(5)
  6. How to create the wrapper type Double:
    1)Double d1 = new Double(3.4); There is no efficient effect. New once creates a wrapper class object
    1. Double d2 = Double.valueOf(3.4); This has no efficient effect, only Integer has
  7. Automatic packing and unpacking:
    1)Integer i3 = 5; Auto packing [basic type to packing type]:
    The compiler will box int type 5 into Integer type, and the underlying method: valueOf(5)
    2)int n = i3; Automatic unpacking [package type to basic type]:
    The compiler will take out the data in i3 of the wrapper type and assign this value to the basic type int
    Method called by the bottom layer: i3.intVlaue();

3. Solution to imprecise floating point operation: BigDecimal

  1. If you use a tool, you must first create an object of this class. If you want to create an object, you must first understand what construction methods it provides
  2. How to create BigDecimal objects:
    1) BigDecimal(double val): take the data of double type as a parameter and give it to the BigDecimal object [no, because double itself is imprecise]
    2) BigDecimal(String val): give the data of String type as a parameter to the BigDecimal object [use this]
    Note: Double - > string can directly splice an empty string ""
  3. Object is used for addition, subtraction, multiplication and division. Note: exceptions will be thrown when the division is not complete, so it is necessary to specify how many decimal places to keep and the rounding method when the division is not complete

4. File class

  1. Create the object of File class: new File(String pathname): this parameter is a path name of String type. This path may be:
    1) File path "E://ready//1.txt"
    2) Path to folder "E://ready//a"
    3) Path of file / folder that does not exist before: this path has not been created in windows yet
    Note: the object of the new File class is only one Java object in memory, and will not really help us create a new File / folder externally
  2. Review code:
package cn.tedu.review;

import java.io.File;
import java.io.IOException;

/*This class is used to replicate the API of the File class*/
public class TestFile {
    public static void main(String[] args) throws IOException {
        //1. Create an object of File class. Note that File class needs to be imported
        File file = new File("E:\\ready\\1.txt");

        //2. Common methods of testing documents
        System.out.println(file.length());//Gets the number of bytes of the file
        System.out.println(file.exists());//Determine whether the file exists
        System.out.println(file.isFile());//Judge whether the path represented by the current file object is a file
        System.out.println(file.isDirectory());//Judge whether the path represented by the current file object is a folder
        System.out.println(file.getName());//1.txt to get the file name
        System.out.println(file.getParent());//E:\ready, get the parent path, excluding the file name
        System.out.println(file.getAbsolutePath());//E:\ready.txt to obtain the absolute path with drive letter and file name

        //3. Test the creation and deletion of file objects in external windows
        file = new File("E:\\ready\\5.txt");
        System.out.println(file.createNewFile());//Create a file that does not exist before. Note that an exception is thrown

        file = new File("E:\\ready\\aaa");
        System.out.println(file.mkdir());//Create a single tier folder that did not exist before

        file = new File("E:\\ready\\j\\q\\k");
        System.out.println(file.mkdirs());//Create a multi tier folder that did not exist before

        System.out.println(file.delete());//Only empty folders and files can be deleted
        file = new File("E:\\ready\\5.txt");
        System.out.println(file.delete());

        //4. Document list test
        file = new File("E:\\ready");
        String[] list = file.list();//The return value type is a String [], and the array is full of objects of type String
        File[] fs = file.listFiles();//The return value type is a File [], and the array is full of objects of File type
        //If you want to get the File object for further operations, you need to use listFiles()

    }
}

Posted by lancet2003 on Sat, 18 Sep 2021 05:25:20 -0700