one-12 regular expressions wrap class IO streams

1. Regular expression Regex

String provides methods to support regular expressions

  1. Matches: Can the current string match a regular expression
  2. ReplceAll: Replace Substring
  3. Split (regular): split a string

k must match the specified character k
abc must match the specified character abc
[abc] matches only a single character
[abc] [123] Combine one of the two
[a-z] matches 26 lower case letters
[a-zA-Z0-9] All three conformances can exist singly
[^a-zA-Z] Exclude English letters
\d number, equivalent to [0-9]
\D Excludes numbers, equivalent to [^0-9]
\s White Space Character
\S Excludes white space characters
\w Alphanumeric underscore, equivalent to [a-zA-Z0-9]
\W Excludes alphanumeric underscores
.Match any character individually
[abc]??Means that there are 0 or 1 occurrences
[abc]?[123] combinations, such as 1,2,a1,a2
[abc]* means that it can occur 0 or more times
[abc]++ means that the number of occurrences is >=1
[abc]{3} elements can occur three times, aaa,bbb,abc.

private static void method1() {
        System.out.println("Please enter your ID number");
        String s = new Scanner(System.in).nextLine();
        /*A single \ has a special meaning in java for an escape symbol and is not considered a slash
         * So if you want to represent a slash, you need to precede it with a \to escape it
         * That is to say, \ is expressed as a simple \ */
        String Regex = "[\\d]{17}[\\dX]";
        if(s.matches(Regex)){
            System.out.println("Correct input");
        }else{
            System.out.println("Please re-enter");
        }
    }

2. Packaging Classes

  1. Packaging class packs the basic types to provide better functions.
    Basic types have no function, they are just a variable, they record values, and wrapper classes can have a richer function
  2. Number abstract class
    An Abstract parent of a numeric wrapper class.
    Provides various ways to get values.
/**This class is used to test basic types of packaging classes*/
public class TestNumber {
    public static void main(String[] args) {
        Integer i1 = new Integer(123);
        Integer i11 = new Integer(123);
        System.out.println(i1 == i11); //false
        System.out.println(i1.equals(i11));//true

        /*Integer Efficient results, but three conditions must be met:
        * 1.Is Integer Type
        * 2.Creation conditions using valuesOf()
        * 3.Data in the range -128 to 127
        * If the above conditions are met, the same data will only be saved once, and subsequent reuse will be the same data that was previously stored.*/
        Integer i2 = Integer.valueOf(100);
        Integer i22 = Integer.valueOf(100);
        System.out.println(i2 == i22); //true

        Integer i3 = Integer.valueOf(300);
        Integer i33 = Integer.valueOf(300);
        System.out.println(i3 == i33); // false, beyond the efficient data range-128-1279

        Double d1 = new Double(3.14);
        Double d11 = new Double(3.14);
        System.out.println(d1 == d11); //false
        /*Only Integer works, Double doesn't*/
        Double d2 = Double.valueOf(3.14);
        Double d22 = Double.valueOf(3.14);
        System.out.println(d2==d22);//false
        //The purpose of this method is to convert the incoming data of type String to int

        /*What type of object is it, you can use all the resources of this class
        * i1 Is an object of type Integer, so you can use parseInt() to convert data of type String to type int
        * d1 Is a Double-type object, so you can use parseDouble() to convert String-type data to double-type*/
        System.out.println(i1.parseInt("800")+8); //808
        System.out.println(d1.parseDouble("2.2")+3.4); //5.6
    }
}

Type int Packing and Unpacking

/**This class is used to test automatic packing and unloading*/
public class TestBox {
    public static void main(String[] args) {
        Integer i1 = new Integer(100);
        Integer i11 = new Integer(100);
        Integer i2 = Integer.valueOf(100);
        Integer i22 = Integer.valueOf(100);
        System.out.println(i11==i22); //false
        System.out.println(i1==i11); //false
        System.out.println(i2==i22);//true

        /*1.Auto-boxing, the compiler will automatically wrap the basic type int 5 into the corresponding wrapper type Integer
        * Then give it to i3 to save, automatically packing the code that happens at the bottom of the box: Integer.valueOf(5);
        * valueOf()Direction: int ---> Integer*/
        Integer i3 = 5;//No error, this phenomenon is automatic packing
        /*2.Automatically unpacks, the compiler will automatically unpack the package type i1 into the basic type data 5
        * Then give it to the basic type int i4 to save the code that happens at the bottom: i1.intValue()
        * intValue()Direction: Integer ---> int*/
        int i4 = i1; //No error, this phenomenon is automatic Unboxing
    }
}

3.io Flow

  1. Flow can only flow in one direction
  2. Input stream for reading_in
  3. Output stream is used to write out_out
  4. Data can only be read and written once from beginning to end

3.1 Inheritance structure of IO streams

File
Byte stream: for binary files
InputStream
FileInputStream
BufferedInputStream
ObjectInputStream
OutputStream
FileOutputStream
BufferedOutputStream
ObjectOutputStream
Character stream: for text files
Reader
FileReader
BufferedReader
InputStreamReader
Writer
FileWriter
BufferedWriter
OutputStreamWriter
PrintWriter Writes out on one line

3.2 File File Class

Summary
Encapsulates a disk path string for which an operation can be performed once
Can encapsulate file paths, folder paths, non-existent paths

/**This class is used to test file class File*/
public class TestFile {
    public static void main(String[] args) throws IOException {
        /*1.ready Directories and 1.txt need to be created by yourself
        * 2.Need to import: import java.io.File;
        * 3.The path is of type String and must be written correctly or the file cannot be found
        * 4.The complete file name consists of two parts: file name + suffix name*/
        File file = new File("D:\\ready\\1.txt");
        //Common methods in 2.1 files
        System.out.println(file.length()); //3, get File Bytes
        System.out.println(file.exists());//true, determine whether the file exists
        System.out.println(file.isFile());//true, determine whether it is a file
        System.out.println(file.isDirectory());//false, determine if it is a folder
        System.out.println(file.getName());//1.txt, get the file name
        System.out.println(file.getParent());//D:\ready, get the parent directory
        System.out.println(file.getAbsolutePath());//D:\ready\1.txt, get the full path with the drive letter: absolute path
        //2.2 Creation and Deletion
        /*new Only one File-type object will be created in memory
        * It does not create a real 2.txt file on disk*/
        file = new File("D:\\ready\\2.txt");
        System.out.println("******************");
        /*If the path specified to create the file is incorrect, an exception is thrown: java.io.Exception
        * So you need to deal with this ahead of time, and temporarily choose to throw it on main()
        * This IO exception is an exception we are currently encountering that must be handled beforehand
        * If not handled, method calls will fail and cannot be compiled*/
        System.out.println(file.createNewFile());//Create a file that did not exist before and return true if created successfully

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

        file = new File("D:\\ready\\a\\b");
        System.out.println(file.mkdirs());//Create a multi-layer folder that did not exist before

        System.out.println(file.delete());//true, delete empty folders/files

        file = new File("D:\\ready\\a");
        System.out.println(file.delete());// false, only empty folders can be deleted

        file = new File("D:\\ready\\2.txt");
        System.out.println(file.delete());// true,2.txt deleted, you can delete files

        //2.3 Test Show File List
        file = new File("D:\\ready");
        String[] list = file.list();/*Not Common*/
        System.out.println(Arrays.toString(list));
        //String[] cannot use File[] method
//        System.out.println(list[0].isDirectory());

        File[] fs = file.listFiles();/*Common*/
        System.out.println(Arrays.toString(fs));
        System.out.println(fs[0].isDirectory());

    }
}

3.3 Byte Stream Read

Byte streams consist of bytes and character streams.
Characters in Java consist of two bytes. Byte streams are basic streams used primarily for processing binary data.
So byte streams are common and can handle many different kinds of files, such as text documents/audio/video, etc.

3.3.1 InputStream abstract class

This abstract class is a superclass/abstract class representing all classes of the byte input stream and cannot create objects
Common methods:

abstract int read() Read the next byte of data from the input stream
int read(byte[] b) Reads a number of bytes from the input stream and stores them in a buffer array b in
int read(byte[] b, int off, int len) Maximum number of input streams len Read in data bytes byte array,off Represents an offset in memory
void close() Close this input stream and release all system resources associated with it

3.3.2 FileInputStream subclass

create object
FileInputStream(File file)—Direct File Transfer Object
 Create one by opening a connection to the actual file FileInputStream,This file passes through the File object file Appoint FileInputStream(String pathname)—Transmission Path
 Create one by opening a connection to the actual file FileInputStream,The file passes through the path name in the file system name Appoint
/**This class is used to practice byte input streams*/
public class TestIn {
    public static void main(String[] args) {
        try {
//            InputStream in = new FileInputStream(new File("D:\\ready\\1.txt"));
            InputStream in = new FileInputStream("D:\\ready\\1.txt");
            System.out.println(in.read());
            System.out.println(in.read());
            System.out.println(in.read());
        } catch (Exception e) {
            e.printStackTrace();//Error messages are printed to the programmer in the console to debug after an error has occurred
        }
    }
}

Tags: Java regex

Posted on Wed, 15 Sep 2021 13:39:29 -0400 by bakigkgz