问题描述
求各位大牛发一个程序里面有集合与io对象流的混合运用的程序!
解决方案
实体类package cn.com.wangxiuwei.javaeye.entity;import java.io.Serializable;public class JavaEyeUser implements Serializable{ //Serializable 对象序列化必须要实现的接口private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}测试类:package cn.com.wangxiuwei.javaeye.test;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.util.ArrayList;import java.util.List;import cn.com.wangxiuwei.javaeye.entity.JavaEyeUser;public class JavaEyeTest {/** * @param args * @throws IOException * @throws FileNotFoundException * @throws ClassNotFoundException */public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {List<JavaEyeUser> list= new ArrayList<JavaEyeUser>();JavaEyeUser jeu=new JavaEyeUser();jeu.setAge(11);jeu.setName("hadoop");JavaEyeUser jeu1=new JavaEyeUser();jeu1.setAge(15);jeu1.setName("net");JavaEyeUser jeu2=new JavaEyeUser();jeu2.setAge(15);jeu2.setName("C");list.add(jeu);list.add(jeu1);list.add(jeu2);ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("D:" +File.separator+"testObjectIO.txt"));oos.writeObject(list);oos.close();ObjectInputStream ois=new ObjectInputStream(new FileInputStream("D:" +File.separator+"testObjectIO.txt"));List<JavaEyeUser> listJEU=(List<JavaEyeUser>)ois.readObject();System.out.println("姓名:"+"t"+"年纪");for(JavaEyeUser us:listJEU){System.out.println(us.getName()+"t"+us.getAge());}}}测试 能写能读出来
解决方案二:
楼主,集合和流两个没什么关联,想怎么使用都可以。你可以就具体的使用场景写出来,可能更具体些,也方便网友们回复。比如流一般用来读写数据,你说的对象流可以用来序列化和反序列化对象。而集合是用来存储一系列的东西的。放手用吧