Object的操作 ——————1

package FruitFile;




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


public class FileDemo{
public static List<Fruit>fruitList;//初始化链表
public static void init(){
fruitList=new ArrayList<Fruit>();//添加元素
Fruit fruit1=new Apple("Apple",0.2,"red");
Fruit fruit2=new Apple("Apple",0.1,"yellow");
Fruit fruit3=new Pear("Pear",0.2,"green");
Fruit fruit4=new Pear("Pear",0.1,"yellow");
Fruit fruit5=new Pear("Pear",0.5,"yellow");
Fruit fruit6=new Orange("Orange",0.1,"yellow");

fruitList.add(fruit1);
fruitList.add(fruit2);
fruitList.add(fruit3);
fruitList.add(fruit4);
fruitList.add(fruit5);
fruitList.add(fruit6);

}
public static void saveFruitsDatas(){
ObjectOutputStream oo=null;
try{
oo=new ObjectOutputStream(
new FileOutputStream("E:\\z_01\\Fruit.txt"));
oo.writeObject(fruitList);//清空缓存,写入文件
//oo.flush();
}catch(Exception ex){
ex.printStackTrace();
}
finally{
try{
oo.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
// @SuppressWarnings("unchecked")
@SuppressWarnings({ "unchecked", "resource" })
public static void readFruitsDatas() throws FileNotFoundException, IOException, ClassNotFoundException{
List<Fruit> fList=new ArrayList<Fruit>();
ObjectInputStream oi=null;
oi=new ObjectInputStream(new FileInputStream("E:\\z_01\\Fruit.txt"));
   fList=(List<Fruit>) oi.readObject();
   System.out.println(fList.get(2));
  }
public static void main(String[] args) throws FileNotFoundException, ClassNotFoundException, IOException{
init();//生成水果箱
saveFruitsDatas();//水果箱--->文件Fruits.txt
readFruitsDatas();//文件Fruits.txt--->临时水果箱--->屏幕上显示(List集合的遍历)
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值