当前位置:网站首页>Network, IO flow, reflection, multithreading, exception

Network, IO flow, reflection, multithreading, exception

2022-06-22 05:39:00 weixin_ forty-two million four hundred and thirty-three thousan



One 、 Establish client and host connections ( The Internet )

  • host
package text;

import java.io.IOException;  // Import   This package is used to catch input and output exceptions  
import java.io.InputStream; // yes java Standard library provides the most basic input stream , It's an abstract class , Is a superclass of all input streams 
import java.net.ServerSocket;  // Server socket   Server program and IP Address binding 
import java.net.Socket;  //  Client initialized a socket Connect , To connect to the server , Two programs on the network exchange data through a two-way communication connection , One end of this two-way link is called a Socket. Any one of them Socket It's all by IP The address and port number are uniquely determined .



public class Java03 {
    
	public static void main(String []args) throws IOException {
    
		//TODO  The server-side code is as follows :
		//ip: Equivalent to host number 
		//port: Extension number 
		
		// Create a server-side socket 、 Specify port number 
		ServerSocket ss=new ServerSocket(1009);
		// Receiving request 、 And returns the socket 
		Socket s=ss.accept();
		
		InputStream is=s.getInputStream();
		byte [] b=new byte[1024];
		int i=is.read(b);
		String str=new String(b,0,i);
		System.out.println(str);
		
		while(true){
       // while The loop wraps the code   can   Receive messages from multiple clients 
			
		}
	}
}
  • The client
package text;

import java.io.IOException;  // Import   This package is used to catch input and output exceptions 
import java.io.OutputStream;  // Use OutputStream Stream output file 
import java.net.Socket;      // Client initialized a socket Connect , To connect to the server , Two programs on the network exchange data through a two-way communication connection , One end of this two-way link is called a Socket. Any one of them Socket It's all by IP The address and port number are uniquely determined .

import java.net.UnknownHostException;

public class Java04 {
    
	public static void main(String[] args) throws UnknownHostException, IOException {
    
		//TODO  The client code is as follows :
		//  Create socket object , Must specify ip and port
		Socket s=new Socket("192.168.10.37",1009);
		OutputStream o=s.getOutputStream();
		String str="hallo";
		o.write(str.getBytes());
	}
}

 Insert picture description here


Two 、I/O system

  • I / O : Input/Output      read / Write      Input / Output
    Byte stream : Read and write in bytes :byte[ ]
    Byte input stream :java.io.InputStream
    Byte output stream :java.io.OutputStream

    Character stream : Read and write in character mode :char[ ]
    The input stream of the child character :java.io.Reader
    The output stream of characters :java.io.Writer

  • Read and write local files

package page;
import java.io.FileReader;// File input character stream 
import java.io.IOException;
import java.io.BufferedReader; // Buffer flow 
import java.io.FileNotFoundException;  // Throw an exception 

public class Java05 {
    
	public static void main(String[] args) throws IOException {
    
		// TODO Auto-generated method stub
				// Read native files 
				/ The first step is to create a file input stream 
					FileReader f=new FileReader("d:/1.txt");
					// The second step   Create a buffered stream object 
					BufferedReader b=new BufferedReader(f);
					String s="";
					while((s=b.readLine())!=null) {
    
						System.out.println(s);
					}
					// The third step   close IO flow 
					b.close();
					f.close();
	}
}

 Insert picture description here

  • Native write file
package page;
import java.io.FileWriter;  // Write 
import java.io.IOException;
import java.io.BufferedWriter;	// Write buffer 
public class Java06 {
    
	public static void main(String[] args) throws IOException {
    
		FileWriter f=new FileWriter("D:/2.txt",true); //true  Adding after does not overwrite , conversely 
		BufferedWriter b=new BufferedWriter(f);
		b.write("Hallo123"+"\r"); // \r  or \n  Line break 
		b.close();
		f.close();
	}
}

 Insert picture description here


3、 ... and 、 Reflection

(1) java Reflection API

  • java.lang.Class                   class
  • java.lang.reflect.Field         Member variables
  • java.lang.reflect.Method     Member method
  • java.lang.reflect.Constructor Construction method
package text;
import java.lang.Class;
import java.lang.reflect.Field; // member object 
import java.lang.reflect.Method;// Member method 
import java.lang.reflect.Constructor; // Construction method 



public class Java01 {
    
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    
		// Create objects with reflections  iop Inversion of control  @  The underlying implementation functions   Create objects with reflections  instance  obtain  
		Class J=Class.forName("text.Java02");
		Java02 j2=(Java02) J.newInstance();
		j2.setAge(2);
		System.out.println(j2);
	}
}

 Insert picture description here
 Insert picture description here


Four 、 Multithreading

  • Mode one : By inheritance Thread Class to implement multithreading

  • Mode two : By implementing Runnable Interface to implement multithreading

  • Mode one Rhread Realization

package page;

public class T1 extends Thread {
    

	// This is the core business method of the thread 
	public void run() {
    
		
		while(true) {
    
			System.out.println(Thread.currentThread().getName());
		}
		
	}
		
	
}

 Insert picture description here

  • Start the inheritance Thread
     Insert picture description here

  • Mode two Runnable Interface to implement

 Insert picture description here

package page;

public class T2 implements Runnable {
    

	
	public void run() {
    
		// This is the core business method of the thread 
		
			
			while(true) {
    
				System.out.println(Thread.currentThread().getName());
			}
			
		}
			

	

}
  • Start implementation Runnable Interface
     Insert picture description here

5、 ... and 、 abnormal

package page;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Random;

public class Java07 {
    
	public static void main(String []args) throws FileNotFoundException {
    
		//(1)  Arithmetic abnormality : java.lang.ArithmeticException 
		int a=3/0;
		
		//(2)  Null pointer exception : java.lang.NullpointerException
		String s=null;
		System.out.println(s.equals("123"));
		
		//(3) Array subscript out of bounds : java.lang.ArrayIndexOutOfBoundsException
		int [] arr= {
    1,2,3};
		System.out.println(arr[3]);
		
		//(4) Type conversion exception : java.lang.ClassCastException
		Object o = new Random();
		String r=(String) o;
		
		//(5) File cannot find exception : java.io.FileNotFoundException
		FileReader f=new FileReader("d:/123.txt");
		
		
	}
}

summary

原网站

版权声明
本文为[weixin_ forty-two million four hundred and thirty-three thousan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220526390721.html