会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132359个问题

老师,该如何理解:序列化不能保存任何成员方法和静态的成员变量 

对此我还特意去写了一下

源码:

    自定义一个可以序列化的类除了get set 方法 还特意加了两个测试方法:

package com.sxt.obj;


import java.io.Serializable;

public class Student implements Serializable{
	private int id;
	private String name;
	public Student(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public Student() {
		super();
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String returntest() {//增加测试方法
		return "return ...";
	}
	public void voidTest() {//增加测试方法
		System.out.println("void ....");
	}
}

    2、写入

package com.sxt.obj;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class StudentWrite {
	public static void main(String[] args) {
		Student stu = new Student(1001, "张三");
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(
					new BufferedOutputStream(
							new FileOutputStream("student.txt")));
			oos.writeObject(stu);
			oos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(oos != null) {
				try {
					oos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

    3、读取

package com.sxt.obj;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class StudnentRead {
	public static void main(String[] args) throws ClassNotFoundException {
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(
					new BufferedInputStream(
							new FileInputStream("student.txt")));
			Student stu = (Student) ois.readObject();
			String str = stu.returntest();
			System.out.println(str);
			stu.voidTest();
			System.out.println(stu.getId() + ", " + stu.getName());
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(ois != null) {
				try {
					ois.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
}


读取后还是可以使用对象的方法

image.png

源码:

ObjStreamPro.7z


最后,感觉方法还是保存了???

JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 3769楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 3770楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 3771楼

老师,在生产者消费者模式里面,wait和notify是不是都要在Good里面呢,我写在Producer和Consumer里好像就不起作用了,为什么?代码如下:

package com.produceandcomsume;

public class Goods {
	boolean isEmpty;
	private String name;
	private String brand;
	public Goods(String name, String brand) {
		super();
		this.name = name;
		this.brand = brand;
	}
	public Goods() {
		super();
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}

}
package com.produceandcomsume;

class Producer implements Runnable {
	Goods good;
	public Producer(Goods good) {
		this.good = good;
	}
	@Override
	public void run() {
		producer();
	}
	public synchronized void producer() {
		if(!good.isEmpty) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		for (int i = 0; i < 10; i++) {
			if (i % 2 == 0) {
				good.setBrand("娃哈哈");
				try {
					Thread.sleep(300);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				good.setName("矿泉水");
				System.out.println(Thread.currentThread().getName() + "生产了-->" + good.getBrand() + good.getName());
			} else {
				good.setBrand("旺仔");
				good.setName("小馒头");
				System.out.println(Thread.currentThread().getName() + "生产了-->" + good.getBrand() + good.getName());
			}
			super.notifyAll();
			good.isEmpty = false;
		}
	}
}
package com.produceandcomsume;

public class Consumer implements Runnable {
	Goods good;
	public Consumer(Goods good) {
		this.good = good;
	}
	@Override
	public void run() {
		consume();
	}
	public synchronized void consume() {
		if(good.isEmpty) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		for (int i = 0; i < 10; i++) {
			good.getBrand();
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			good.getName();
			System.out.println(Thread.currentThread().getName() + "消费了-->" + good.getBrand() + good.getName());
		}
		super.notifyAll();
		good.isEmpty = true;
	}
}
package com.produceandcomsume;

public class Demo {
	public static void main(String[] args) {
		Goods goods = new Goods();
		goods.isEmpty = true;
		Thread producer = new Thread(new Producer(goods), "生产者");
		Thread consumer = new Thread(new Consumer(goods), "消费者");
		producer.start();
		consumer.start();
	}
}

运行结果:

image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 3773楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 3774楼

问题一:Server运行,游览器无响应啊?

问题二:StringBuilder sb,添加了那么多字符串的意义在哪里,响应只显示“成功”,其余的是被游览器解析的吗?

package cn.bjsxt.server;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {//服务器,启动和停止服务
	private ServerSocket server;
	public static void main(String[] args){
		Server server = new Server();//创建服务器对象
		server.start();
	}
	public void start(int port){
		try {
			server = new ServerSocket(port);
			this.receive();//调用接收请求信息的方法
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void start(){
		this.start(8888);
	}
	private void receive() {
		
		try {
			//(1)监听
			Socket client = server.accept();
			//获取用户的请求
			/*InputStream is = client.getInputStream();
			byte [] buf = new byte[20480];
			int len = is.read(buf);
			System.out.println(new String(buf,0,len));*/
			Request req = new Request(client.getInputStream());
			//req.show();
			/**做出响应*/
			StringBuilder sb = new StringBuilder();
			sb.append("HTTP/1.1").append(" ").append(200).append(" ").append("OK").append("\r\n");
			sb.append("Content-Type:text/html;charset=utf-8").append("\r\n");
			//内容
			String str="<html><head><title>响应结果</ritle></head><body>成功</body></html>";
			sb.append("Content-Length:"+str.getBytes("utf-8").length).append("/r/n");
			sb.append("\r\n");
			sb.append(str);
			//通过输出流发送出去
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream(),"utf-8"));
			bw.write(sb.toString());
			bw.flush();
			bw.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void stop(){
		
	}

}

image.png

http_server4.rar


JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 3776楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/XML 技术(旧) 3777楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 3779楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637