当前位置:网站首页>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】
List of articles
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());
}
}

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.OutputStreamCharacter 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.WriterRead 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();
}
}

- 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();
}
}

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);
}
}


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());
}
}
}

- Start the inheritance Thread

- Mode two Runnable Interface to implement

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

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
边栏推荐
- 随鼠标移动的提示框
- Running yolov5 reports an error attributeerror: cant get attribute sppf on module models common
- Optimization direction of code walk through (convenient interface requests, long dynamic class judgment conditions, removal of useless consoles, separation of public methods)
- Get jedis from jedissentinelpool
- Throw away electron and embrace Tauri based on Rust
- count registers in C code -- registers has one pattern
- 數據的存儲(進階)
- 跨境政策频调整 “ 独立站&平台 ” 哪个才是趋势所在?
- Online text code comparison tool
- 毕业回馈!Apache Doris 社区所有贡献者来领礼品啦!
猜你喜欢

Talk about MySQL's locking rule "hard hitting MySQL series 15"

Independent station optimization list - how to effectively improve the conversion rate in the station?

Someone always asks me: how to play independent station? Three cases, you will understand after reading

Kubernetes -- setting up an environment using minicube

想投放Facebook广告却不知从何入手?此文带你深入了解

Facebook账户 “ 解封、防封、养号 ” 知识要点,已收藏!
![Force buckle 33 [search rotation sort array]](/img/0f/d7e2f2fdf48bcd70e6c69bca7d4002.png)
Force buckle 33 [search rotation sort array]

nacos server 源码运行实现

P1318 积水面积

移动端 realm数据库使用及解耦,跨线程安全使用 OC realm
随机推荐
致远OA漏洞分析、利用与防护合集
Opencv function usage details 1~10, including code examples
sourcetree报错ssh失败
关于背包问题的总结
Go语言使用JWT
从“ 平台转型 ”到“ DTC品牌出海 ”,2021趋势何在?
做事方法:3C 方案设计法
毕业季 | 新的开始,不说再见
printf becomes puts
Facebook账户 “ 解封、防封、养号 ” 知识要点,已收藏!
Zhiyuan OA vulnerability analysis, utilization and protection collection
2022 Shanxi secondary vocational group "Cyberspace Security" event module b- web page penetration
SCM future employment development direction, learn SCM must know some entry-level knowledge and industry prospects, read the benefit
Some templates about bisection
A piece of code to solve the problem of automatic disconnection of Google colab
Sourcetree reported an error SSH failure
我不建议你工作太拼命
Independent station optimization list - how to effectively improve the conversion rate in the station?
Rambbmitmq Push Party
CLion安装下载