Day24
一,对象流
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class Test03 {
public static void main(String[] args) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("io.txt"));
oos.writeObject(new User("1810466136", "123123", "金刚芭比", "寒冰射手", 10000, 8000));
oos.writeObject(new User("1444911642", "123456", "死亡如风", "亚索", 1000, 50));
oos.writeObject(new User("1048312408", "111222", "撕裂的忧伤", "塞拉斯", 8000, 6000));
oos.writeObject(new User("2929954193", "123321", "leoy", "盲僧", 5000, 2000));
oos.writeObject(null);
oos.close();
}
}
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class Test04 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("io.txt"));
User user;
while((user = (User) ois.readObject()) != null){
System.out.println(user);
}
ois.close();
}
}
import java.io.Serializable;
public class User implements Serializable{
private static final long serialVersionUID = -7113110317387504182L;
private String username;
private transient String password;
private String nickName;
private String role;
private double hp;
private double mp;
public User() {
}
public User(String username, String password, String nickName, String role, double hp, double mp) {
this.username = username;
this.password = password;
this.nickName = nickName;
this.role = role;
this.hp = hp;
this.mp = mp;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public double getHp() {
return hp;
}
public void setHp(double hp) {
this.hp = hp;
}
public double getMp() {
return mp;
}
public void setMp(double mp) {
this.mp = mp;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + ", nickName=" + nickName + ", role=" + role
+ ", hp=" + hp + ", mp=" + mp + "]";
}
}
二,内存流
1.内存输出流
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class Test01 {
public static void main(String[] args) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write("用良心做教育".getBytes());
byte[] byteArray = baos.toByteArray();
System.out.println(new String(byteArray));
String str = baos.toString();
System.out.println(str);
}
}
2,内存输入流
import java.io.ByteArrayInputStream;
import java.io.IOException;
public class Test02 {
public static void main(String[] args) throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream("用良心做教育".getBytes());
byte[] bs = new byte[1024];
int len;
while((len = (bais.read(bs))) != -1){
System.out.println(new String(bs, 0, len));
}
}
}
三,打印流
1,字节打印流
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class Test01 {
public static void main(String[] args) throws IOException {
PrintStream ps = new PrintStream(new FileOutputStream("io.txt",true));
ps.println("用良心做教育");
ps.close();
}
}
2,字符打印流
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class Test02 {
public static void main(String[] args) throws IOException {
PrintWriter pw = new PrintWriter(new FileWriter("io.txt",true));
pw.println("用良心做教育");
pw.close();
}
}
3,重定向
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Scanner;
public class Test03 {
public static void main(String[] args) throws FileNotFoundException {
System.setIn(new FileInputStream("io.txt"));
InputStream in = System.in;
Scanner scan = new Scanner(in);
String str = scan.next();
System.out.println(str);
scan.close();
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
public class Test04 {
public static void main(String[] args) throws FileNotFoundException {
System.setOut(new PrintStream(new FileOutputStream("io.txt",true)));
PrintStream out = System.out;
out.println("彩虹小可爱,皇冠给你带");
}
}
四,随机访问流
1,写入数据
import java.io.IOException;
import java.io.RandomAccessFile;
public class Test01 {
public static void main(String[] args) throws IOException {
RandomAccessFile w = new RandomAccessFile("io.txt", "rw");
w.write("123abc木头人".getBytes());
w.close();
}
}
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
public class Test02 {
public static void main(String[] args) throws IOException {
File file = new File("io.txt");
RandomAccessFile w = new RandomAccessFile(file, "rw");
w.seek(file.length());
w.write("123abc木头人".getBytes());
w.close();
}
}
2,读取数据
import java.io.IOException;
import java.io.RandomAccessFile;
public class Test03 {
public static void main(String[] args) throws IOException {
RandomAccessFile r = new RandomAccessFile("io.txt", "r");
byte[] bs = new byte[1024];
int len;
while((len = r.read(bs)) != -1){
System.out.println(new String(bs,0,len));
}
r.close();
}
}
3,拷贝数据
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
public class Copy {
public static void main(String[] args) throws IOException {
RandomAccessFile r = new RandomAccessFile("水野朝阳.mp4", "r");
File targetFile = new File("copy.mp4");
RandomAccessFile w = new RandomAccessFile(targetFile, "rw");
long length = targetFile.length();
r.seek(length);
w.seek(length);
byte[] bs = new byte[1024];
int len;
while((len = r.read(bs)) != -1){
w.write(bs, 0, len);
}
r.close();
w.close();
}
}
五,网络编程–TCP协议
1,一对一(发一句收一句)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket socket = new Socket("127.0.0.1", 8080);
PrintStream ps = new PrintStream(socket.getOutputStream());
ps.println("王晨曦:妹妹,你多大了?");
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "GBK"));
String readLine = br.readLine();
System.out.println(readLine);
ps.close();
br.close();
socket.close();
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(8080);
Socket socket = server.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "GBK"));
String readLine = br.readLine();
System.out.println(readLine);
PrintStream ps = new PrintStream(socket.getOutputStream());
ps.println("18号技师:刚满18岁");
br.close();
ps.close();
socket.close();
server.close();
}
}
2,一对一(一直发一直收)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(8080);
Scanner scan = new Scanner(System.in);
Socket socket = server.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "GBK"));
PrintStream ps = new PrintStream(socket.getOutputStream());
while(true){
String readLine = br.readLine();
System.out.println(readLine);
ps.println("18号技师:" + scan.next());
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket socket = new Socket("127.0.0.1", 8080);
Scanner scan = new Scanner(System.in);
PrintStream ps = new PrintStream(socket.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "GBK"));
while(true){
ps.println("王晨曦:" + scan.next());
String readLine = br.readLine();
System.out.println(readLine);
}
}
}
3,一对一(发,收多句)–利用线程
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(8080);
Socket socket = server.accept();
new ReceiveThread(socket).start();
Scanner scan = new Scanner(System.in);
PrintStream ps = new PrintStream(socket.getOutputStream());
while(true){
ps.println("18号技师:" + scan.next());
}
}
}
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket socket = new Socket("127.0.0.1", 8080);
new ReceiveThread(socket).start();
Scanner scan = new Scanner(System.in);
PrintStream ps = new PrintStream(socket.getOutputStream());
while(true){
ps.println("王晨曦:" + scan.next());
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class ReceiveThread extends Thread{
private Socket socket;
public ReceiveThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
while(true){
try {
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "GBK"));
String readLine = br.readLine();
System.out.println(readLine);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4,群聊
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ConcurrentHashMap;
public class Server {
public static final ConcurrentHashMap<String, PrintStream> map = new ConcurrentHashMap<>();
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(8080);
while(true){
Socket socket = server.accept();
String ip = socket.getInetAddress().getHostName();
System.out.println(ip);
PrintStream ps = new PrintStream(socket.getOutputStream());
map.put(ip, ps);
new ServerThread(socket).start();
}
}
}
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket socket = new Socket("127.0.0.1", 8080);
new ReceiveThread(socket).start();
Scanner scan = new Scanner(System.in);
PrintStream ps = new PrintStream(socket.getOutputStream());
while(true){
ps.println("王晨曦:" + scan.next());
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public class ServerThread extends Thread{
private Socket socket;
public ServerThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
ConcurrentHashMap<String,PrintStream> map = Server.map;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "GBK"));
while(true){
String readLine = br.readLine();
System.out.println(readLine);
Set<Entry<String,PrintStream>> entrySet = map.entrySet();
for (Entry<String, PrintStream> entry : entrySet) {
String ip = entry.getKey();
if(!socket.getInetAddress().getHostName().equals(ip)){
PrintStream ps = entry.getValue();
ps.println(readLine);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class ReceiveThread extends Thread{
private Socket socket;
public ReceiveThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
while(true){
try {
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "GBK"));
String readLine = br.readLine();
System.out.println(readLine);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
六,网络编程–UDP协议
1,一对一
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class Client01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(7070);
byte[] buf = "电动跳跳恐龙蛋".getBytes();
DatagramPacket p = new DatagramPacket(buf , 0, buf.length, InetAddress.getByName("127.0.0.1"), 8080);
socket.send(p);
buf = new byte[1024];
p = new DatagramPacket(buf, 0, buf.length);
socket.receive(p);
System.out.println(new String(buf).trim());
}
}
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class Client02 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(8080);
byte[] buf = new byte[1024];
DatagramPacket p = new DatagramPacket(buf, 0, buf.length);
socket.receive(p);
System.out.println(new String(buf).trim());
buf = "一比一高达模型".getBytes();
p = new DatagramPacket(buf , 0, buf.length, InetAddress.getByName("127.0.0.1"), 7070);
socket.send(p);
}
}
2,一对一(连说)
import java.net.DatagramSocket;
public class Client01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(7070);
new ReceiveThread(socket).start();
new SendThread("127.0.0.1", 8080, "王波", socket).start();
}
}
import java.net.DatagramSocket;
public class Client02 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(8080);
new ReceiveThread(socket).start();
new SendThread("127.0.0.1", 7070, "袁黄彬", socket).start();
}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class ReceiveThread extends Thread{
private DatagramSocket socket;
public ReceiveThread(DatagramSocket socket) {
this.socket = socket;
}
@Override
public void run() {
while(true){
byte[] buf = new byte[1024];
DatagramPacket p = new DatagramPacket(buf, 0, buf.length);
try {
socket.receive(p);
System.out.println(new String(buf).trim());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
public class SendThread extends Thread{
private String ip;
private int port;
private String nickName;
private DatagramSocket socket;
public SendThread(String ip, int port, String nickName, DatagramSocket socket) {
this.ip = ip;
this.port = port;
this.nickName = nickName;
this.socket = socket;
}
@Override
public void run() {
Scanner scan = new Scanner(System.in);
while(true){
byte[] buf = (nickName + ":" + scan.next()).getBytes();
try {
DatagramPacket p = new DatagramPacket(buf , 0, buf.length, InetAddress.getByName(ip), port);
socket.send(p);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
七,网络编程–http
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Test01 {
public static void main(String[] args) throws Exception {
URL url = new URL("https://suggest.taobao.com/sug?code=gbk&q=始祖鸟&callback=cb");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int code = connection.getResponseCode();
if(code == HttpURLConnection.HTTP_OK){
InputStream in = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
char[] cs = new char[1024];
int len;
while((len = br.read(cs)) != -1){
System.out.println(new String(cs,0,len));
}
in.close();
}else if(code == HttpURLConnection.HTTP_NOT_FOUND){
System.out.println("抱歉,网页未找到");
}
}
}
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Test02 {
public static void main(String[] args) throws Exception {
URL url = new URL("https://p9.itc.cn/q_70/images03/20201010/879e2dfb54ac4118b22a412bdcc79cca.jpeg");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int code = connection.getResponseCode();
if(code == HttpURLConnection.HTTP_OK){
InputStream in = connection.getInputStream();
FileOutputStream out = new FileOutputStream("相泽南.jpg");
byte[] bs = new byte[1024];
int len;
while((len = in.read(bs)) != -1){
out.write(bs, 0, len);
}
in.close();
out.close();
}else if(code == HttpURLConnection.HTTP_NOT_FOUND){
System.out.println("抱歉,网页未找到");
}
}
}