Advanced features of java learning - regular expressions, File classes, observer patterns, input and output streams (parts)

Observer model:

A one-to-many dependency is defined, which allows multiple objects to listen to a subject object at the same time. When the subject object changes, all observer objects are notified and updated by themselves.

import java.util.Observable;

public class House extends Observable {

    private float price;

    public House() {
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        //Call the parent class to set the change point
        super.setChanged();
        //Price changes, presented to all observers
        super.notifyObservers(price);
        this.price = price;
    }

    @Override
    public String toString() {
        return "House{" +
                "price=" + price +
                '}';
    }

    public House(float price) {
        this.price = price;
    }
}

import java.util.Observable;
import java.util.Observer;

public class PersonObservable implements Observer {


   private String name;

    public PersonObservable() {
    }

    public PersonObservable(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void update(Observable o, Object arg) {
        //Parameter o: the content of the arg modification of the observer object
        if(arg instanceof Float){
         //   System.out.println(this.name + "observed price is:"+ (Float) arg. floatValue ());
            System.out.println(this.name+"The observed prices are:"+arg);

        }
    }
}


public class test {

    public static void main(String[] args) {
        House house = new House(12000);
        PersonObservable po1 = new PersonObservable("1");
        PersonObservable po2 = new PersonObservable("2");
        PersonObservable po3 = new PersonObservable("3");

        house.addObserver(po1);
        house.addObserver(po2);
        house.addObserver(po3);
        System.out.println(house);
        house.setPrice(13500);

        System.out.println(house);



    }

}

Regular expressions:

Matching data, string validation and substitution

 

^ Match the beginning of a string

$: Matched Character End

* Match a character 0 or more times

+ One or more times

? Match 0 times or once

{n}: Negative integer, matching n times

{n,}: Negative integer, matching greater than or equal to N times

{n,m} Matches at least N times and at most m times

\ d: Represents numbers

\ w: Representation character

package c0801zhengze;

import java.util.Calendar;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {


    public static void main(String[] args) {
        boolean flag = true;
        String str = "123456789";
        char c[] = str.toCharArray();
        for (int i =0;i<c.length;i++){
            if (!Character.isDigit(c[i])){
                flag = false;
                break;
            }
        }
        System.out.println(flag);


        /**
         * regular expression
         * Using regular expressions requires dependencies on pattern classes and matcher classes
         * pattern: Mainly for the preparation of regular specifications
         * matcher: Mainly to implement the norms
         */
        String Str1="1234567890";
        if(Pattern.compile("[0-9]+").matcher(Str1).matches()){
            System.out.println("Digital composition");
        }
        else System.out.println("no");


        /**
         *
         * Verification date format 2019-08-01
         * \d{4}-\d{2}-\d{2}
         */
        String  a = "2019-08-01";

        String pat = "\\d{4}-\\d{2}-\\d{2}";
        //20[0,1]\d{1}-
        Pattern p = Pattern.compile(pat);
        Matcher m = p.matcher(a);

        if(m.matches()){
            System.out.println("Correct");
        }
        else{
            System.out.println("Incorrect");
        }


        /**
         *
         * Split Strings (Regular Expressions)
         */
//        String u= "a1b2c33333d4444g5556";
//        String  patt = "\\d+";
//        p = Pattern.compile(patt);
//        String []s = p.split(u);
//        System.out.println(s.length);
//        for (String t:s
//             ) {
//            System.out.println(t);
//
//        }

        /*
         * Split Strings (Regular Expressions)
         */
//        String u= "a1b2c33333d4444g5556";
//        String  patt = "\\d";
//        p = Pattern.compile(patt);
//        Matcher matcher = p.matcher(u);
//        String newString = matcher.replaceAll("_");
//        System.out.println(newString);

        /**
         * Enter a set of lowercase letters from the keyboard and store them in str
         * Convert lower-case letters with odd subscripts to upper-case letters and output results
         */
        Scanner input  = new Scanner(System.in);
        System.out.println("Input string");
        String f = new String();
        f = input.next();
        char []chars = f.toCharArray();
        for(int i = 1;i<chars.length;i+=2){
            if(Character.isLowerCase(chars[i])){
                chars[i] = Character.toUpperCase(chars[i]);
            }
        }
           f = new String(chars);
        System.out.println(f);
        System.out.println(chars);

//        String  patt = "[a-z]  ";
//        p = Pattern.compile(patt);
//        Matcher matcher = p.matcher(f);
//        String newString = matcher.replaceAll("_");
//        System.out.println(newString);

    }
}

Input and output streams:

Why do I need to input and output:

Real-world interactions occur between programs and hardware, or between programs and programs, where information transfer is required and flow is required.

Common input and output devices: keyboard, printer, screen

 

Class File:

Java.io.File: Represents a file or directory on a hard disk

 

Create File Objects with Parametric Constructions (Addresses)

Relative path and absolute path

package File_test;

import java.io.*;

public class Test_File {
    public static void main(String[] args) throws IOException {
        File file = new File("d:/text.txt");
        byte a[] = new byte[1024];
        InputStream in = new FileInputStream(file);

        a = in.readAllBytes();
      //  System.out.println('\n'+file.getAbsolutePath());
       // System.out.println(file.getPath());
        String s = new String(a);
        System.out.println(s);
        s = s + "00000000000000000000000000000000\n\n\n";

        byte f[] = s.getBytes();
        in.close();
        OutputStream out = new FileOutputStream(file);
        out.write(f);
        out.close();
    }


    }

 

Keywords: Java

Added by platinum on Sat, 12 Oct 2019 19:30:09 +0300