对象的序列化和反序列化

@PostMapping("/")
    public String test( Dog dog) throws InstantiationException, IllegalAccessException {
        try {
            //序列化对象
//            FileOutputStream stream = new FileOutputStream("test.txt");
//            ObjectOutputStream outputStream = new ObjectOutputStream(stream);
//            outputStream.writeObject(dog);
//            outputStream.close();
//            stream.close();

            //反序列化
            FileInputStream stream = new FileInputStream("test.txt");
            ObjectInputStream inputStream = new ObjectInputStream(stream);
            Object o = inputStream.readObject();
            inputStream.close();
            stream.close();
            System.out.println(o+"====");//Dog(name=11111)====
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return dog.getName();
    }

d

你可能感兴趣的:(python,java,服务器)