===>Java反射
==獲取Class實例的的三種方法
1調用運行時類本身的.class屬性
語法:
Class Class對象名=類名.class;
舉例:
Class clazz=Person.class;//獲取Person類的屬性
System.out.println(clazz1.getName());
Class clazz2=String.class;
System.out.println(clazz2.getName());
2.通過運行時類的對象獲取
語法:
Class Class對象名=對象.getClass();
舉例:
Person p=new Person();//創建Person類對象p
Class clazz3=p.getClass();//獲取Person類的對象p的屬性
3.通過Class的靜態方法獲取
語法:
Class Class類對象名=Class.forName(包下類地址);
舉例:
String className="com.cfl.Person";//com.cfl爲包名,Peson爲類名
Class clazz4=Class.forName(className);//通過Class的靜態方法獲取
通過反射調用類的完整結構
只能獲取到運行時類及其父類中聲明爲public的屬性:getFields()
獲取到運行時類本身的=所有屬性:getDeclaredFiled()
====>網絡編程
1.傳輸層協議
-------傳輸控制協議TCP
-------用戶數據報協議UDP
傳輸控制方法
創建InetAddress對象:getByName(String host)
獲取IP地址對應域名:getHostName()
獲取IP地址:getHostAdress()
舉例:
import java.net.InetAddress;
public class TestInetAddress {
public static void main(String[] args) throws Exception{
InetAddress inet= InetAddress.getByName("www.baidu.com");
System.out.println(inet);
System.out.println("域名爲:"+inet.getHostName());
System.out.println("ip地址爲:"+inet.getHostAddress());
}
}
運行結果:
URL編程
1.創建一個URL的對象
語法:
URL 對象名=new URL(“地址”);
舉例:
URL url = new URL("http://127.0.0.1:8080//Helloworld.txt");
常用方法
獲取該URL的協議名:public String getProtocol()
獲取該URL的主機名:public String getHost()
獲取該URL的端口號:public String getPort()
獲取讀URL的文件路徑:public String getPath()
獲取讀URL的文件名:public String getFile()
獲取該URL在文件中的相對位置;public String getRef()
獲取讀URL的查詢名:public String getQuery()
舉例:
import java.net.MalformedURLException;
import java.net.URL;
public class TestUrl {
public static void main(String[] args) throws MalformedURLException {
//1.創建一個URL的對象
URL url = new URL("http://127.0.0.1:8080//examples//Helloworld.txt");
System.out.println(url.getProtocol());
System.out.println(url.getHost());
System.out.println(url.getPort());
System.out.println(url.getFile());
System.out.println(url.getRef());
System.out.println(url.getQuery());
}
}
運行結果:
將服務器的資源讀進來:openStream()
語法:
InputStream 對象名=new openStream();
舉例:
InputStream is=url.openStream();
輸入和輸出數據: URLConnection()
語法:
URLConnection =URL對象名.openConnection();
InputStream 輸入流對象名=URL對象名.getInputStream();
FileOutputStream 輸出流對象名=new FileOutputStream(new File(“輸出文件名稱及格式”));
舉例:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class PutStream {
public static void main() throws IOException {
URL url = new URL("http://127.0.0.1:8080//examples//Helloworld.txt");
InputStream is=url.openStream();
byte[] b=new byte[20];
int len;
while((len=is.read(b))!=-1) {
String str=new String(b,0,len);
System.out.print(str);
}
is.close();
URLConnection urlConn=url.openConnection();
InputStream is1=urlConn.getInputStream();
FileOutputStream fos=new FileOutputStream(new File("abc.txt"));
byte[] b1=new byte[20];
int len1;
while((len1=is1.read(b1))!= -1) {
fos.write(b1,0,len1);
}
fos.close();
is1.close();
}
}
網絡通訊步驟
1.創建Socket對象,藉助構造器指明服務器ip地址和接收程序的端口號。
2.使用getOutputStream()方法發送數據
3.傳輸內容
簡單網絡通信:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import org.junit.jupiter.api.Test;
public class TestTCP {
//客戶端
@Test
public void cilent(){
Socket socket=null;
OutputStream os=null;
try {
socket=new Socket(InetAddress.getByName("127.0.0.1"),9090);
os=socket.getOutputStream();
os.write("我是客戶端,向你發來問候".getBytes());
}catch(IOException e){
e.printStackTrace();
}finally {
if(os!=null) {
try {
os.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
//服務端
@Test
public void server() {
// ServerSocket ss=new ServerSocket();
ServerSocket ss=null;
Socket s=null;
InputStream is=null;
try {
ss=new ServerSocket(9090);
} catch (IOException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
try {
s=ss.accept();
} catch (IOException e1) {
// TODO 自動生成的 catch 塊
e1.printStackTrace();
}
try {
is=s.getInputStream();
} catch (IOException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
byte[] b=new byte[20];
int len;
try {
while((len=is.read(b))!=-1) {
String str=new String(b,0,len);
System.out.print(str);
}
} catch (IOException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
if(is!=null)
try {
is.close();
} catch (IOException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
if(s!=null)
try {
s.close();
} catch (IOException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
if(ss!=null)
try {
ss.close();
} catch (IOException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
}
}
===》初學Spring框架
1.ecplise安裝spring框架
安裝ecplise框架,教程地址:https://blog.csdn.net/qq_40587575/article/details/79915669
本來想繼續寫下去的,發現還有個英語的測試沒寫,下週吧,抱歉哈,