求解答,字数还有要求。。。
------解决思路----------------------
当然可以。
class User {
private String name;
private int age;
public void writeToOutputStream(ObjectOutputStream output) throws IOException {
output.writeUTF(name);
output.writeInt(age);
}
public void readFromInputStream(ObjectInputStream input) throws IOException {
name = input.readUTF();
age = input.readInt();
}
}
------解决思路----------------------
public static void writeUserListToOutputStream(List<User> users, ObjectOutputStream output) throws IOException {
output.writeInt(users.size());
for (User user : users) {
user.writeToOutputStream(output);
}
}
public static List<User> readUserListFromInputStream(ObjectInputStream input) throws IOException {
List<User> users = new LinkedList<User>();
int count = input.readInt();
for (int i = 0; i < count; i++) {
User user = new User();
user.readFromInputStream(input);
users.add(user);
}
return users;
}