当前位置:网站首页>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
边栏推荐
猜你喜欢

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

Sourcetree reported an error SSH failure

Implementation of large file fragment uploading based on webuploader

A piece of code to solve the problem of automatic disconnection of Google colab

Analysis of 43 cases of MATLAB neural network: Chapter 29 research on the application of limit learning machine in regression fitting and classification -- Comparative Experiment

Analysis of 43 cases of MATLAB neural network: Chapter 28 Application Research of decision tree classifier - breast cancer diagnosis

Cookie setting and reading in C #
![[graduation season · advanced technology Er] a graduate student's chatter](/img/c2/dc77b8ffecfcb0a495e1dc8b6e7922.png)
[graduation season · advanced technology Er] a graduate student's chatter

毕业回馈!Apache Doris 社区所有贡献者来领礼品啦!

2022 Shanxi secondary vocational group "Cyberspace Security" event module b- web page penetration
随机推荐
innosetup判断程序已经运行方法
删除弹窗组件的封装使用
Service migration when deploying SuperMap iserver war package
Rétroaction sur la remise des diplômes! Tous les contributeurs de la communauté Apache Doris sont ici pour recevoir des cadeaux!
The prediction made ten years ago by the glacier has now been realized by Ali, which is very shocking
通达OA漏洞分析合集
count registers in C code -- registers has one pattern
亚马逊和独立站,不是简单的二选一
Go语言使用zap日志库
【毕业季·进击的技术er】一个读研学生的唠唠嗑
Amazon and independent station are not simply two choices
sourcetree报错ssh失败
强制删除 Terminating 状态的 namespace
Independent station optimization list - how to effectively improve the conversion rate in the station?
参数序列化
关于背包问题的总结
C#中Cookie设置与读取
Why is "CPS alliance marketing" the most cost-effective promotion method?
Graduation feedback! All contributors of Apache Doris community come to receive gifts!
QEMU ARM interrupt system architecture