码迷,mamicode.com
首页 > 编程语言 > 详细

JavaIO流学习总结-ObjectOutputStream和ObjectInputStream基本操作练习

时间:2020-03-30 23:20:14      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:对象   sys   inpu   ade   学习总结   array   www   add   dex   

package io;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
/*
  * 修改日期:2020/03/30
  * 修改人:牟松
 * ObjectOutputStream和ObjectInputStream基本操作练习
 */
class Person implements Serializable{             // 实现序列化接口
    /**
  *
  */
 private static final long serialVersionUID = 1L;
 private String name;
    private int age;
    public Person() {
    }
    public Person(String name, int age) {
        this.name = name;
        this.age = 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;
    }
   
    public String toString() {
        return "Person{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ‘}‘;
    }
}
class Tool1 {
 ObjectOutputStream outputstream=null;
 ObjectInputStream intputstream=null;
 
 //读文件中内容
 public void read() throws IOException
 {  
  ArrayList<Person> list = new ArrayList<Person>();
  intputstream=new ObjectInputStream(new FileInputStream("D:\\Ceshi.txt")); 
     try {
      /* for(;;)  //针对写入时是List集合
      list.addAll((ArrayList<Person>) intputstream.readObject());*/
      for(;;)  //针对写入时是单个对象
             list.add((Person)intputstream.readObject());   
  } catch (ClassNotFoundException e1) {
   e1.printStackTrace();
  }        
     
     finally {
     intputstream.close();        
        for(Person person:list){ 
             System.out.println(person.getName());
        }
     }
 
 }
 
 //往文件中写内容
 public void write() throws IOException
 {
 //1.创建ObjectOutputStream对象,构造方法中传递字节输出流
  outputstream=new ObjectOutputStream(new FileOutputStream("D:\\Ceshi.txt"));
 //2.使用ObjectOutputStream对象中的方法writeObject,把对象写入到文件中
  ArrayList<Person> list = new ArrayList<Person>();
  list.add(new Person("牟松",18));
  list.add(new Person("张三",18));
  //List集合写入
  //outputstream.writeObject(list);
  //单个对象写入
  outputstream.writeObject(new Person("牟松",18));
  outputstream.writeObject(new Person("张三",18));
        //3.释放资源
  outputstream.close();
 }
 
 //追加内容
    public void appendObj() throws Exception     {        
       
     outputstream=new ObjectOutputStream(new FileOutputStream("D:\\Ceshi.txt",true)) 
     {          
      // 重写writeStreamHeader()方法,空实现            
      protected void writeStreamHeader(){};        
     };        
  // 写入数据         
  ArrayList<Person> list = new ArrayList<Person>();
  list.add(new Person("牟松",18));
  list.add(new Person("张三",18));
  outputstream.writeObject(list);
  outputstream.close(); 
     }
    }
public class Ceshi1 {
   public static void main(String[] args) throws Exception
     {
        Tool1 tool=new Tool1();
        try {
           tool.read();
         //tool.write();
         //tool.appendObj();
  } catch (IOException e) {
   e.printStackTrace();
  }
     }
}
 
参考连接:
https://www.cnblogs.com/niujifei/p/11499074.html

JavaIO流学习总结-ObjectOutputStream和ObjectInputStream基本操作练习

标签:对象   sys   inpu   ade   学习总结   array   www   add   dex   

原文地址:https://www.cnblogs.com/musong1998/p/12601806.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!