ObjectInputStream和ObjectOutputStream

版权声明:正在学习中,有错误欢迎指出。感谢比❤❤~ https://blog.csdn.net/gx17864373822/article/details/79972205

将对象进行序列化:implements Seriaizable

一个学生对象:

import java.io.*;
import java.util.Arrays;

class Student implements Serializable{//implements将对象进行序列化
    String id;
    String name;
    int age;
    public Student(String id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
    }

}
public class Main {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Student stu = new Student("0001", "abc", 20);

        ObjectOutputStream oos= new ObjectOutputStream(new FileOutputStream("stu.dat"));//输出流写进去:写出对象
        ObjectInputStream ois= new ObjectInputStream(new FileInputStream("stu.dat"));//输入流读出来:读写对象数据

        oos.writeObject(stu);//将学生对象写出去。需要将对象进行序列化!

        Object object = ois.readObject();
        if(object instanceof Student){
            Student stu1 = (Student )object;//还原数据
            System.out.println(stu1);
        }

        oos.close();
        ois.close();
    }

}

学生对象数组:

import java.io.*;
import java.util.Arrays;

class Student implements Serializable{//implements将对象进行序列化
    String id;
    String name;
    int age;
    public Student(String id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
    }

}
public class Main {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Student stu1 = new Student("0001", "abc", 20);
        Student stu2 = new Student("0002", "bcd", 20);
        Student[] students = new Student[]{stu1, stu2};

        ObjectOutputStream oos= new ObjectOutputStream(new FileOutputStream("stu.dat"));//输出流写进去:写出对象
        ObjectInputStream ois= new ObjectInputStream(new FileInputStream("stu.dat"));//输入流读出来:读写对象数据

        oos.writeObject(students);//将学生对象数组写出去。需要将对象进行序列化!

        Object object = ois.readObject();

        if(object instanceof Student[]){
            Student[] stus = (Student[])object;//强制转换,还原数据
            for(Student stu:stus){
                System.out.println(stu);
            }
        }

        oos.close();
        ois.close();
    }

}

ObjectOutputStreamObjectInputStream的问题!

03-14

public static Object dispatch(Object dispatchObject)rn rn String appletURLParameter = "http://192.168.1.1:8080/TestApplet/ServletDispatcher";rnrn Object returnObject = null;rn tryrn rn URL url = new URL(appletURLParameter);rn URLConnection connection = url.openConnection();rn connection.setDoInput(true);rn connection.setDoOutput(true);rnrn //本次连接不用Cachern connection.setUseCaches(false);rn //下次连接不用Cachern connection.setDefaultUseCaches(false);rn connection.setRequestProperty("Content-Type", "application/octet-stream");rnrn ObjectOutputStream oos = new ObjectOutputStream(connection.getOutputStream());rn oos.writeObject(dispatchObject);rn oos.flush();rn oos.close();rnrn ObjectInputStream ois = new ObjectInputStream(connection.getInputStream());rn returnObject = ois.readObject();rn rn catch (ClassNotFoundException ex)rn rn ex.printStackTrace();rn rn catch (MalformedURLException ex)rn rn ex.printStackTrace();rn rn catch (IOException ex)rn rn ex.printStackTrace();rn rn return returnObject;rn rnrnrn 我的做法是把一个序列化的对象传递给一个SERVLET,它的名字叫ServletDispatcher,然后在SERVLET里做一系列操作,返回对象给applet,但rnrn ObjectInputStream ois = new ObjectInputStream(connection.getInputStream());rn returnObject = ois.readObject();rnrn connection.getInputStream()为什么为unknow resource呢?rnrn ObjectOutputStream oos = new ObjectOutputStream(connection.getOutputStream());rn oos.writeObject(dispatchObject);rn oos.flush();rn oos.close();rnrn 把上述四行代码注掉就正常了?rnrnrn rn

创建ObjectOutputStreamObjectInputStream对象顺序

09-27

在试着写一个聊天程序。rn客户端连接到服务器后,发送一个Order类的对象,其中包含一个字符串,然后在服务器的标准输出中打印出该字符串。 rn运行时发现在创建ObjectOutputStream对象和ObjectInputStream对象时,如果先创建ObjectOutputStream对象则程序正常进行,反之若先创建ObjectInputStream对象则程序会卡在创建ObjectInputStream对象时。请高手指教。rn下面是截取的一部分代码rnrnOrder.javarn[code=Java]import java.io.Serializable;rnrnpublic class Order implements Serializablernrn private static final long serialVersionUID = 1234567890123L;rn private String context;rn public Order(String context)rn rn this.context = context;rn rn rn public String getContext()rn rn return context;rn rn[/code]rnrnServer.javarn[code=Java]import java.io.InputStream;rnimport java.io.OutputStream;rnimport java.io.ObjectInputStream;rnimport java.io.ObjectOutputStream;rnimport java.net.ServerSocket;rnimport java.net.Socket;rnrnpublic class Serverrnrn private InputStream is;rn private ObjectInputStream ois;rn private ObjectOutputStream oos;rn private OutputStream os;rn rn public Server()rn rn tryrn rn ServerSocket server = new ServerSocket(6000);rn Socket client = server.accept();rn is = client.getInputStream();rn os = client.getOutputStream();rn rn /*rn oos = new ObjectOutputStream(os);rn ois = new ObjectInputStream(is);rn */rn rn ois = new ObjectInputStream(is);////////////////////////////这两行若交换位置,则程序会正常运行rn oos = new ObjectOutputStream(os);////////////////////////////rnrn Order order = (Order)ois.readObject();rn System.out.println(order.getContext());rn rn catch (Exception e)rn rn e.printStackTrace();rn rn rn rn public static void main(String[] args)rn rn new Server();rn rn[/code]rnrnrnClient.javarn[code=Java]import java.io.InputStream;rnimport java.io.OutputStream;rnimport java.io.ObjectInputStream;rnimport java.io.ObjectOutputStream;rnimport java.net.Socket;rnimport java.net.InetAddress;rnrnpublic class Clientrnrn private InputStream is;rn private ObjectInputStream ois;rn private ObjectOutputStream oos;rn private OutputStream os;rn rn public Client()rn rn tryrn rn Socket client = new Socket(InetAddress.getByName("localhost"), 6000);rn is = client.getInputStream();rn os = client.getOutputStream();rn ois = new ObjectInputStream(is);rn oos = new ObjectOutputStream(os);rn rn oos.writeObject(new Order("asfd"));rn rn catch (Exception e)rn rn e.printStackTrace();rn rn rn rn public static void main(String[] args)rn rn new Client();rn rnrn[/code]

一个简单ObjectInputStream ObjectOutputStream的问题

04-20

下面这段代码总是调不通,请教各位。多谢rn[b][color=#0000FF]Client:[/color][/b]rn[code=Java]import java.io.IOException;rnimport java.io.ObjectInputStream;rnimport java.io.ObjectOutputStream;rnimport java.net.Socket;rnimport java.util.logging.Level;rnimport java.util.logging.Logger;rnrnpublic class Main rnrn public static void main(String[] args) rn try rn Socket socket = new Socket("localhost", 2222);rn ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());rn ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());rnrn Request req = new Request("login");rn req.setData("UserName", "名字");rn req.setData("Password", "密码");rnrn oos.writeObject(req);rn oos.flush();rn catch (IOException ex) rn Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);rn rn rn[/code]rnrn[code=Java]import java.io.Serializable;rnimport java.util.Properties;rnrnpublic class Request implements Serializable rnrn private String type;//请求类型rn private Properties parameters;//请求所带的参数rn private static final long serialVersionUID = 800120791203985L;rnrn public Request(String type) rn parameters = new Properties();rn this.type = type;rn rnrn public void setData(String key, String value) rn this.parameters.setProperty(key, value);rn rnrn public String getData(String key) rn return this.parameters.getProperty(key);rn rnrn public Properties getAllParameters() rn return this.parameters;rn rnrn public String getType() rn return type;rn rn[/code]rnrn[b][color=#0000FF]Server:[/color][/b]rn[code=Java]import java.io.IOException;rnimport java.io.ObjectInputStream;rnimport java.io.ObjectOutputStream;rnimport java.net.ServerSocket;rnimport java.net.Socket;rnimport java.util.logging.Level;rnimport java.util.logging.Logger;rnrnpublic class Main rnrn public static void main(String[] args) rn try rn ServerSocket socket = new ServerSocket(2222);rn Socket sock = socket.accept();rn ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());rn ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());rnrn Request req;rn req=(Request)ois.readObject(); //#1rnrn catch (IOException ex) rn Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); //#2rn catch (ClassNotFoundException ex) rn Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);rn rn rn[/code]rnrn[code=Java]import java.io.Serializable;rnimport java.util.Properties;rnrnpublic class Request implements Serializable rn private String type;//请求类型rn private Properties parameters;//请求所带的参数rnrn public Request(String type) rn parameters = new Properties();rn this.type = type;rn rnrn public void setData(String key, String value) rn this.parameters.setProperty(key, value);rn rnrn public String getData(String key) rn return this.parameters.getProperty(key);rn rnrn public Properties getAllParameters() rn return this.parameters;rn rnrn public String getType() rn return type;rn rn[/code]rnrn连接的时候Server端#1处会抛出错误到#2处。rn请问是什么原因?谢谢了

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭