当前位置: 代码迷 >> Android >> 安卓支持ObjectInputStream的JAVA流么,该怎么处理
  详细解决方案

安卓支持ObjectInputStream的JAVA流么,该怎么处理

热度:9   发布时间:2016-04-28 03:47:29.0
安卓支持ObjectInputStream的JAVA流么
求解答,字数还有要求。。。
------解决思路----------------------
引用:
可以讲我存在文件夹里的List<User>对象集合给读出来么?

当然可以。

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;
}
  相关解决方案